jQuery控制div层,使其移动、改变位置
2014-02-26 17:00
295 查看
1、移动div
View Code
2、div跟随某个控件的位置改变而改变
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test2.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript"> var b = function(){ $("body").append("<div id='hid' style='background:red;position:absolute;width:880;height:120'>我是可以拖动的半透明div,Firefox下暂时只能获取点击事件,event无解</div>"); $("#hid").fadeTo("slow", 0.5); $("#hid").mousedown(function(event){ var offset=$("#hid").offset(); x1=event.clientX-offset.left; y1=event.clientY-offset.top; $("#hid").mousemove(function(event){ $("#hid").css("left",(event.clientX-x1)+"px"); $("#hid").css("top",(event.clientY-y1)+"px"); /* $("#hid").animate({ left : _x + "px", top : _y + "px" }, 10); */ }); $("#hid").mouseup(function(event){ $("#hid").unbind("mousemove"); }); }); } </script> </head> <body> <input type=button value="点我显示div" onclick="b()"> </body> </html>
View Code
2、div跟随某个控件的位置改变而改变
var offset = $("#selectInfoDiv").offset(); //获取相对控件的偏移位置 var left=offset.left; var top=offset.top; var width = $("#selectInfoDiv").width(); //该控件的宽度和高度 var height= $("#selectInfoDiv").height(); top=(top+(height-206)/2)<200?200:(top+(height-206)/2); $("#addDiv").css({left:left+((width-666)/2),top:top}); //addDiv是跟随selectInfoDiv的位置变化而变化
相关文章推荐
- jquery过滤选择常用radio,select,checkbox,input 等标签
- 读jQuery之十(事件模块概述)
- 读jQuery之八(包装事件对象)
- 读jQuery之七(判断点击了鼠标哪个键)
- 读jQuery之六(缓存数据)
- jQuery插件之清空区域内容
- 读jQuery之三(构建选择器)
- 读jQuery之五(取DOM元素)
- 读jQuery之四(优雅的迭代)
- jQuery四种事件绑定方式.bind(),.live(),.delegate(),on()的区别
- jQuery开发插件的两个方法
- JQuery的extend方法详解
- jquery的ajax重写
- Jquery获取select,dropdownlist,checkbox下拉列表框的值
- jquery live()调用不存在的解决方法
- jquery鼠标跟随简单效果
- jQuery Mobile 表单选择菜单
- jQuery判断文本框是否为空
- action跳转过后,jquery失效
- 简单选项卡 js和jquery制作方法分享