利用Jquery实现页面上div的拖动及位置保存
2015-10-16 17:46
866 查看
<script src="js/jquery.js.js" type="text/javascript"></script> <script src="js/jquery-ui-1.9.1.custom.min.js" type="text/javascript"></script>
<script type="text/javascript"> $(function(){ $("div.containment-wrapper").draggable(); }); function setObjectInitPos(selector) { var cookies = getCookies(); $(selector).each(function(){ var left = parseFloat(cookies[$(this).attr("id")+"_Left"]); var top = parseFloat(cookies[$(this).attr("id")+"_Top"]); if(!isNaN(left)) $(this).css("left", left + "px"); if(!isNaN(top)) $(this).css("top", top + "px"); }); $(selector).draggable({ start:function(){}, drag:function(){}, stop:function(){ setCookie($(this).attr("id")+"_Left", $(this).css("left").replace("px",""),365); setCookie($(this).attr("id")+"_Top", $(this).css("top").replace("px",""),365); } }); } function setCookie(name, value, daysToLive) { var cookie = name + "=" + encodeURIComponent(value); if (typeof daysToLive === "number") cookie += "; max-age=" + (daysToLive*60*60*24); document.cookie = cookie; } function getCookies() { var cookies = {}; var all = document.cookie; if (all === "") return cookies; var list = all.split("; "); for(var i = 0; i < list.length; i++) { var cookie = list[i]; var p = cookie.indexOf("="); var name = cookie.substring(0,p); var value = cookie.substring(p+1); value = decodeURIComponent(value); cookies[name] = value; } return cookies; } </script>
<div id="div1" style="position:absolute;border:1px solid green;width:200px;height:30px;left:0px;top:0px;"> <img src="https://www.baidu.com/img/bdlogo.png" style="width:200px;height:30px;" /> </div> <div id="div2" style="position:absolute;border:1px solid green;width:200px;height:30px;left:0px;top:40px;"> <img src="https://www.baidu.com/img/bdlogo.png" style="width:200px;height:30px;" /> </div> <div id="div3" style="position:absolute;border:1px solid green;width:200px;height:30px;left:0px;top:80px;"> <img src="https://www.baidu.com/img/bdlogo.png" style="width:200px;height:30px;" /> </div>
下面是另一个自己写的方案,兼容性有一点问题:
<script type="text/javascript"> $(function(){ setObjectMovable(document.getElementById("div1")); setObjectMovable(document.getElementById("div2")); setObjectMovable(document.getElementById("div3")); setCookie("a","1",1); setCookie("b","2",1); }); function setObjectMovable(obj) { $(obj).bind("mousedown",function(){ obj.startX = event.clientX; obj.startY = event.clientY; obj.drag = true; obj.setCapture(); }); $(obj).bind("mousemove",function(){ if(obj.drag) { var chgX = event.clientX - obj.startX; var chgY = event.clientY - obj.startY; obj.startX = event.clientX; obj.startY = event.clientY; var newLeft = parseFloat($(obj).css("left").replace("px","")) + parseFloat(chgX); var newTop = parseFloat($(obj).css("top").replace("px","")) + parseFloat(chgY); $(obj).css("left", newLeft + "px"); $(obj).css("top", newTop + "px"); } }); $(obj).bind("mouseup",function(){ obj.drag = false; obj.releaseCapture(); }); } function setCookie(name, value, daysToLive) { var cookie = name + "=" + encodeURIComponent(value); if (typeof daysToLive === "number") cookie += "; max-age=" + (daysToLive*60*60*24); document.cookie = cookie; } function getCookies() { var cookies = {}; var all = document.cookie; if (all === "") return cookies; var list = all.split("; "); for(var i = 0; i < list.length; i++) { var cookie = list[i]; var p = cookie.indexOf("="); var name = cookie.substring(0,p); var value = cookie.substring(p+1); value = decodeURIComponent(value); cookies[name] = value; } return cookies; } </script>
相关文章推荐
- iOS中jQuery 的delegate 事件监听无效解决办法
- 使用 jQuery.i18n.properties 实现 Web 前端的国际化
- 使用 jQuery.i18n.properties 实现 Web 前端的国际化
- 2015-10-13 jQuery5实例
- 超实用的jQuery分页插件
- 引入jquery插件时的版本冲突解决
- JQuery 常用积累(五)Datetimepicker和Lodop
- Jquery连续两次相同AJAX请求时,默认不执行本次;不同时终止上一次未完成的AJAX请求
- jquery tmpl 详解
- 自己用jQuery写一个瀑布流
- jquery-validate验证表单
- 利用jQuery和CSS将背景图片拉伸
- jquery获取html元素的绝对位置和相对位置
- 基于jQuery实现的菜单切换效果
- jquery判断复选框是否被选中的方法
- 基于jquery实现复选框全选,反选,全不选等功能
- jquery的图片播放插件 - colorbox
- jquery对javascript事件的封装一览
- jquery插件select2事件不起作用(select2-3.5.4)
- 利用jquery实现百度新闻导航菜单滑动动画