Jquery可移动的DIV
2016-05-19 16:48
453 查看
做这个不难,主要是理解了计算的公式吧,鼠标移动的位置-(鼠标按下的位置-要移动div与浏览器的边距)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>有问题的代码</title> <style> * { margin: 0; padding: 0; } .header p { position: absolute; right: 20px; top: 10px; } #header-login-box { display: none; width: 200px; height: 200px; border: 1px solid #000; margin-left: 200px; margin-top: 200px; } .login-title { padding: 10px 0; border-bottom: 1px solid #dddddd; cursor: move; } </style> </head> <body> <div class="header"> <p id="login">登陆</p> </div> <div id="header-login-box"> <div class="login-title">可以拖动的div</div> </div> </body> <script src="jquery.min.js"> </script> <script type="text/javascript"> $(document).ready(function () { //div的位置 var divX = 0; var divY = 0; //鼠标的位置 var mouseX = 0; var mouseY = 0; //鼠标和div的距离 var difX; var difY; var isDown = false; $("#login").on("click", function () { $("#header-login-box").show(); $(".login-title").mousedown(function (e) { isDown = true; divX = $("#header-login-box").offset().left; divY = $("#header-login-box").offset().top; difX = e.pageX - divX; difY = e.pageY - divY; }) $(document).mousemove(function (e) { if (isDown == true) { newX = e.pageX - difX; newY = e.pageY - difY; $("#header-login-box").css("margin-left", newX + "px").css("margin-top", newY + "px"); } }).mouseup(function (e) { isDown = false; }) }) }); </script> </html>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- JavaScript演示排序算法
- 2015-2016网页设计趋势分析 Web Design of Trends
- jQuery Ajax 跨域调用
- 移动端的长按事件
- jquery教程靠边站,一分钱不花让你免费学会jquery
- JQuery+Strusts1.x无刷新登录
- $.ajax()方法详解
- jQuery ajax - ajax() 方法
- JavaScript 各种遍历方式详解
- 数组方法汇总
- jQuery Html控件基本操作(日常收集整理)
- jQuery插件实现文字无缝向上滚动效果代码
- jQuery菜单插件用法实例
- JQuery 初体验(建议学习jquery)
- 基于Jquery和CSS3制作数字时钟附源码下载(CSS3篇)
- Jquery实现的table最后一行添加样式的代码
- jQuery实现向下滑出的平滑下拉菜单效果
- jQuery 练习[一] 学习jquery的准备工作
- jquery获得页面元素的坐标值实现思路及代码