Javascript編的隨意拖拽圖片的網頁

來源:互聯網
上載者:User

這個本是  web程式設計  課程中的一個實驗.感覺挺有意思的.發上來分享下..

實現效果:使用滑鼠讓元素可以在整個文檔顯示範圍內移動,當移動最下一張圖片的時候,其他兩張沒有反映,但當移動中間一張或者最上一張的時候,最後一張也會跟隨著移動..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<script language="javascript">  
  function   beginDrag(elementToDrag,event){  
   
  var   delatX=event.clientX-parseInt(elementToDrag.style.left);  
  var   delatY=event.clientY-parseInt(elementToDrag.style.top);  
  document.attachEvent("onmousemove",moveHandler);  
  document.attachEvent("onmouseup",upHandler);  
  event.cancelBubble=true;  
  event.returnValue=false;  
  // event.z-index=100;  
  function   moveHandler(e)     {  
  if(!e)   e=window.event;  
  elementToDrag.style.left=(e.clientX-delatX)+"px";  
  elementToDrag.style.top=(e.clientY-delatY)+"px";  
  e.cancelBubble=true; 

  }  
   
  function   upHandler(e)   {  
  if(!e)   e=window.event;  
  document.detachEvent("onmouseup",upHandler);  
  document.detachEvent("onmousemove",moveHandler);  
  e.cancelBubble=true;  
  bao.style.left=elementToDrag.style.left;  
  bao.style.top=elementToDrag.style.top;
   
          }  
  }  
   
  </script>  
 
<style type="text/css">
<!--
body,td,th {
 color: #9900FF;
}
body {
 background-color: #FFFFCC;
}
-->
</style></head>

<body>

<input   type="image"   src="圖片1"   id=bao  
  style="position:absolute; left:227px; top:10px; z-index:0; width: 237px; height: 219px;"  
  onmousedown="beginDrag(this,event);">
<input name="image2"   type="image"   id=image22    src="圖片2"  
  style="position:absolute; left:281px; top:63px; z-index:0; width: 253px; height: 215px;"  
  onmousedown="beginDrag(this,event);">
  <input name="image"   type="image"   id=image2    src="圖片3"  
  style="position:absolute; left:357px; top:206px; z-index:0; width: 236px; height: 200px;"  
  onmousedown="beginDrag(this,event);">

</body>
</html>
 

這樣就完成了.把上面代碼中的"圖片1","圖片2","圖片3"分別改成你想要使用的圖片的名稱.並且在同一目錄下,然後儲存一下..用瀏覽器開啟就可以看到有趣的效果了^_^

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.