DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery實現簡單的拖拽效果實例兼容所有主流浏覽器
jquery實現簡單的拖拽效果實例兼容所有主流浏覽器
編輯:JQuery特效代碼     
最近發現web網頁的拖拽效果,個人覺得是一種不錯的用戶體驗,抽空研究了一下,原理還蠻簡單的,下面貼一下我寫的一個簡單拖拽jquery自定義函數。
jquery代碼:fun.js
. 代碼如下:
jQuery.fn.myDrag=function(){
_IsMove = 0;
_MouseLeft = 0;
_MouseTop = 0;
return $(this).bind("mousemove",function(e){
if(_IsMove==1){
$(this).offset({top:e.pageY-_MouseLeft,left:e.pageX-_MouseTop});
}
}).bind("mousedown",function(e){
_IsMove=1;
var offset =$(this).offset();
_MouseLeft = e.pageX - offset.left;
_MouseTop = e.pageY - offset.top;
}).bind("mouseup",function(){
_IsMove=0;
}).bind("mouseout",function(){
_IsMove=0;
});
}

html代碼:
. 代碼如下:
<html>
<head>
<title>demo.htm</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="myFun.js" type="text/javascript"></script>
<style type="text/css">
.myDiv{
background:#EAEAEA;
width: 100px;
height: 100px;
border: 1px solid;
cursor: pointer;
text-align: center;
line-height: 100px;
}
</style>
<script type="text/javascript">
$(function(){
$("#myDiv").myDrag();
$("#myDiv2").myDrag();
})
</script>
</head>
<body>
<div id="myDiv" class="myDiv">拖拽1</div>
<div id="myDiv2" class="myDiv">拖拽2</div>
</body>
</html>

效果圖1:

效果圖2:
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved