AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > JavaScript

jQuery实现div随意拖动的实例代码(通用代码)

51自学网 http://www.wanshiok.com
div随意拖动,jquery实现div拖动

注意js放的位置,要放的靠近,若被其他覆盖,则无法移动。

比如:

<div id="move">可移动的DIV</div>

引入jquery.js, jquery-ui.js,

<script scr="http://code.jquery.com/jquery-1.10.2.js"></script><script scr="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

一句:

$("#move").draggable();

如希望,点住时鼠标变手形:

$("#move").mousedown(function(){$(this).css("cursor","pointer");}).mouseup(function(){$(this).css("cursor","default");});

下面给大家分享一段通用代码jquery实现拖动div的通用方法

<script type="text/javascript"><!-- $(document).ready(function() { $(".show").mousedown(function(e)//e鼠标事件 { $(this).css("cursor","move");//改变鼠标指针的形状 var offset = $(this).offset();//DIV在页面的位置 var x = e.pageX - offset.left;//获得鼠标指针离DIV元素左边界的距离 var y = e.pageY - offset.top;//获得鼠标指针离DIV元素上边界的距离 $(document).bind("mousemove",function(ev)//绑定鼠标的移动事件,因为光标在DIV元素外面也要有效果,所以要用doucment的事件,而不用DIV元素的事件 { $(".show").stop();//加上这个之后 var _x = ev.pageX - x;//获得X轴方向移动的值 var _y = ev.pageY - y;//获得Y轴方向移动的值 $(".show").animate({left:_x+"px",top:_y+"px"},10); }); }); $(document).mouseup(function() { $(".show").css("cursor","default"); $(this).unbind("mousemove"); }) }) // --></script> 


div随意拖动,jquery实现div拖动  
上一篇:基于javascript实现动态显示当前系统时间  下一篇:jQuery+css实现炫目的动态块漂移效果