jQuery实现拖动效果的实例代码

这篇文章给大家介绍了jquery实现拖动效果的简单代码,非常不错,具有参考借鉴价值,需要的朋友参考下吧

jQuery实现拖动效果的实例代码,具体代码如下所示:

<!DOCTYPE html> <html> <head> <style> div{ width:100px; height:100px; background:red; position:absolute;} </style> <script type="text/javascript" src="https://www.jb51.net/js/jquery-1.11.3.js"></script> <script> $(function(){ var disX = 0; var disY = 0; $('div').mousedown(function(ev){ disX = ev.pageX - $(this).offset().left;//获取鼠标到元素的left,top位置 disY = ev.pageY - $(this).offset().top; $(document).mousemove(function(ev){ $('div').css('left',ev.pageX - disX);//获取移动后鼠标的位置,并重新赋值给元素 $('div').css('top',ev.pageY - disY); }); $(document).mouseup(function(){ $(document).off(); }); return false; }); }); </script> </head> <body> <div></div> </body> </html>

以上所述是小编给大家介绍的jQuery实现拖动效果的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyfsdx.html