JS实现的自定义网页拖动类
作者:bea
本文实例讲述了JS实现的自定义网页拖动类。分享给大家供大家参考,具体如下: 先来看运行效果截图如下: 在线演示地址如下: http://demo./js/2015/js-zdy-web-drug-pic-style-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitio
本文实例讲述了JS实现的自定义网页拖动类。分享给大家供大家参考,具体如下:
先来看运行效果截图如下:
在线演示地址如下:
http://demo./js/2015/js-zdy-web-drug-pic-style-codes/
具体代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>自写的拖动类……</title>
<script type="text/javascript">
var d=document;//给document对象一个通用的事件侦听方法
d.addListener=function(e,f,b){
this.attachEvent?this.attachEvent('on'+e,f):this.addEventListener(e,f,b);
}
d.removeListener=function(e,f,b){
this.detachEvent?this.detachEvent('on'+e,f):this.removeEventListener(e,f,b);
}
function $(){//接收一个id参数,返回带有startDrag方法的对象
var o=document.getElementById(arguments[0]);
o.addListener=function(e,f,b){
this.attachEvent?this.attachEvent('on'+e,f):this.addEventListener(e,f,b);
}
o.removeListener=function(e,f,b){
this.detachEvent?this.detachEvent('on'+e,f):this.removeEventListener(e,f,b);
}
o.startDrag=function(obj){//参数obj默认为o本身,可以传其它参数以确定要移动的对象
var obj=obj?obj:o;
var sx,sy;
o.style.cursor="move";
o.addListener("mousedown",function(e){
e||event;
if(e.button==1||e.button==0){
sx=e.clientX-obj.offsetLeft;sy=e.clientY-obj.offsetTop;
d.addListener("mousemove",move,false);
d.addListener("mouseup",stopDrag,false);
}
},false);
var stopDrag=function(){
d.removeListener("mousemove",move,false);
d.removeListener("mouseup",stopDrag,false);
}
var move=function(e){
e||event;
window.getSelection ? window.getSelection().removeAllRanges() :
document.selection.empty();
if(e.preventDefault)e.preventDefault();//这两句便是解决firefox拖动问题的.
with (obj.style){
position="absolute"
left=e.clientX-sx+"px";
top=e.clientY-sy+"px";
}
}
}
return o;
}
window.onload=function(){$("ok").startDrag($("os"))}//本例中拖动ok元素,移动其父元素
</script>
<style type="text/css">
*{margin:0;padding:0}
#ok{width:215px;height:170px;background:url(images/sample1.gif)}
#os{width:400px;height:300px;background:#09f;left:300px}
#os2{width:400px;height:300px;background:#f90;}
</style>
</head>
<body>
<div id="os"><p id="ok"></p></div>
<div id="os2"></div>
</body>
</html>
希望本文所述对大家JavaScript程序设计有所帮助。
有用 | 无用
先来看运行效果截图如下:
在线演示地址如下:
http://demo./js/2015/js-zdy-web-drug-pic-style-codes/
具体代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>自写的拖动类……</title>
<script type="text/javascript">
var d=document;//给document对象一个通用的事件侦听方法
d.addListener=function(e,f,b){
this.attachEvent?this.attachEvent('on'+e,f):this.addEventListener(e,f,b);
}
d.removeListener=function(e,f,b){
this.detachEvent?this.detachEvent('on'+e,f):this.removeEventListener(e,f,b);
}
function $(){//接收一个id参数,返回带有startDrag方法的对象
var o=document.getElementById(arguments[0]);
o.addListener=function(e,f,b){
this.attachEvent?this.attachEvent('on'+e,f):this.addEventListener(e,f,b);
}
o.removeListener=function(e,f,b){
this.detachEvent?this.detachEvent('on'+e,f):this.removeEventListener(e,f,b);
}
o.startDrag=function(obj){//参数obj默认为o本身,可以传其它参数以确定要移动的对象
var obj=obj?obj:o;
var sx,sy;
o.style.cursor="move";
o.addListener("mousedown",function(e){
e||event;
if(e.button==1||e.button==0){
sx=e.clientX-obj.offsetLeft;sy=e.clientY-obj.offsetTop;
d.addListener("mousemove",move,false);
d.addListener("mouseup",stopDrag,false);
}
},false);
var stopDrag=function(){
d.removeListener("mousemove",move,false);
d.removeListener("mouseup",stopDrag,false);
}
var move=function(e){
e||event;
window.getSelection ? window.getSelection().removeAllRanges() :
document.selection.empty();
if(e.preventDefault)e.preventDefault();//这两句便是解决firefox拖动问题的.
with (obj.style){
position="absolute"
left=e.clientX-sx+"px";
top=e.clientY-sy+"px";
}
}
}
return o;
}
window.onload=function(){$("ok").startDrag($("os"))}//本例中拖动ok元素,移动其父元素
</script>
<style type="text/css">
*{margin:0;padding:0}
#ok{width:215px;height:170px;background:url(images/sample1.gif)}
#os{width:400px;height:300px;background:#09f;left:300px}
#os2{width:400px;height:300px;background:#f90;}
</style>
</head>
<body>
<div id="os"><p id="ok"></p></div>
<div id="os2"></div>
</body>
</html>
希望本文所述对大家JavaScript程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- jquery实现简易的移动端验证表单
- jquery UI Datepicker时间控件的使用方法(终结版)
- jquery UI Datepicker时间控件的使用方法(加强版)
- jquery UI Datepicker时间控件的使用方法(基础版)
- jquery验证邮箱格式并显示提交按钮
- jquery判断当前浏览器的实现代码
- jQuery插件开发精品教程(让你的jQuery更上一个台阶)
- js识别uc浏览器的代码
- 详解javascript数组去重问题
- 基于JavaScript实现仿京东图片轮播效果
- 实现非常简单的js双向数据绑定
- 浅析javascript中的事件代理
- 详解javascript中的事件处理
- jQuery插件实现静态HTML验证码校验
- jQuery Real Person验证码插件防止表单自动提交
- jQuery实现非常实用漂亮的select下拉菜单选择效果
- javascript如何实现暂停功能
- JavaScript实现带缓冲效果的随屏滚动漂浮广告代码
- jQuery实现可关闭固定于底(顶)部的工具条菜单效果