教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 Javascript 拖动效果兼容火狐

Javascript 拖动效果兼容火狐

发布时间:2016-11-28   编辑:jiaochengji.com
教程集为您提供Javascript 拖动效果兼容火狐等资源,欢迎您收藏本站,我们将为您提供最新的Javascript 拖动效果兼容火狐资源
拖动效果在以前是非常常用的一款效果,下面我来给各位分享一款js拖动效果代码,有需要了解的朋友可参考。
提示:您可以先修改部分代码再运行

<textarea name="runcool" rows="28" cols="75" > <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>拖动效果兼容火狐</title> <style type="text/css"> </style> <script type="text/javascript"> /* 十三妖 qq:181907667 msn:wl181907667@hotmail.com 邮箱:thirdteendevil@163.com */ function dragClass(){ this.mouseState = document.all ? 1 : 0 ; this.x; this.y; this.dragId = null; this.index = 100; document.onselectstart = function(){return false;}; } dragClass.prototype = { dragStar:function(dragId,moveId){//拖动入口函数 var _this = this; _this.$(dragId).style.cursor = "move"; _this.$(dragId).onmousedown = function(e){ var e = e ? e : event; //_this.$(moveId).style.zIndex = _this.index ; if(e.button == _this.mouseState) { _this.setDragInfo(e,moveId,moveId); _this.dragPro(moveId); } }, _this.$(moveId).onmousedown = function(e){ _this.$(moveId).style.zIndex = _this.index ; }, _this.$(dragId).onmouseup = function(){ _this.clearDragId(); } document.onmouseup = function(){ _this.clearDragId(); } }, setDragInfo:function(e,dragId,moveId){//拖动初始化 this.x = e.clientX; this.y = e.clientY; this.dragId = dragId; if(this.$(moveId).style.position != "absolute") { this.$(moveId).style.width = this.$(moveId).offsetWidth "px"; this.$(moveId).style.height = this.$(moveId).offsetHeight "px"; this.$(moveId).style.position = "absolute"; this.$(moveId).style.left = this.$(moveId).offsetLeft "px"; this.$(moveId).style.top = this.$(moveId).offsetTop "px"; } }, clearDragId:function(){ //清除拖动ID this.dragId = null; }, dragPro:function(moveId){ var _this = this; document.onmousemove = function(e){ var e = e ? e : event; if(e.button == _this.mouseState && _this.dragId != null) { var x = e.clientX; var y = e.clientY; //_this.$(moveId).style.left = (_this.$(moveId).offsetLeft (x - _this.x)) "px"; //_this.$(moveId).style.top = (_this.$(moveId).offsetTop (y - _this.y)) "px"; _this.$(moveId).style.left = (parseInt(_this.$(moveId).style.left) (x - _this.x)) "px"; _this.$(moveId).style.top = (parseInt(_this.$(moveId).style.top) (y - _this.y)) "px"; _this.x = x; _this.y = y; //alert(_this.$(dragId).style.left); } } }, $:function(o){//获取对象 if(typeof(o) == "string") { if(document.getElementById(o)) { return document.getElementById(o); } else { alert("errId "" o ""!"); return false; } } else { return o; } } } window.onload = function(){ var c = new dragClass(); c.dragStar("b","a"); c.dragStar("d","c"); } </script> </head> <body>
<h1 class="t_rt"> 鼠标拖动层封装类 </h1> <h2> 效果预览 </h2>
拖动层1
    移动层
拖动层2
    移动层
<h2> 源代码 </h2>
<textarea id="sourse"> function dragClass(){ this.mouseState = document.all ? 1 : 0 ; this.x; this.y; this.dragId = null; this.index = 100; document.onselectstart = function(){return false;}; } dragClass.prototype = { dragStar:function(dragId,moveId){//拖动入口函数 var _this = this; _this.$(dragId).style.cursor = "move"; _this.$(dragId).onmousedown = function(e){ var e = e ? e : event; //_this.$(moveId).style.zIndex = _this.index ; if(e.button == _this.mouseState) { _this.setDragInfo(e,moveId,moveId); _this.dragPro(moveId); } }, _this.$(moveId).onmousedown = function(e){ _this.$(moveId).style.zIndex = _this.index ; }, _this.$(dragId).onmouseup = function(){ _this.clearDragId(); } document.onmouseup = function(){ _this.clearDragId(); } }, setDragInfo:function(e,dragId,moveId){//拖动初始化 this.x = e.clientX; this.y = e.clientY; this.dragId = dragId; if(this.$(moveId).style.position != "absolute") { this.$(moveId).style.width = this.$(moveId).offsetWidth "px"; this.$(moveId).style.height = this.$(moveId).offsetHeight "px"; this.$(moveId).style.position = "absolute"; this.$(moveId).style.left = this.$(moveId).offsetLeft "px"; this.$(moveId).style.top = this.$(moveId).offsetTop "px"; } }, clearDragId:function(){ //清除拖动ID this.dragId = null; }, dragPro:function(moveId){ var _this = this; document.onmousemove = function(e){ var e = e ? e : event; if(e.button == _this.mouseState && _this.dragId != null) { var x = e.clientX; var y = e.clientY; //_this.$(moveId).style.left = (_this.$(moveId).offsetLeft (x - _this.x)) "px"; //_this.$(moveId).style.top = (_this.$(moveId).offsetTop (y - _this.y)) "px"; _this.$(moveId).style.left = (parseInt(_this.$(moveId).style.left) (x - _this.x)) "px"; _this.$(moveId).style.top = (parseInt(_this.$(moveId).style.top) (y - _this.y)) "px"; _this.x = x; _this.y = y; //alert(_this.$(dragId).style.left); } } }, $:function(o){//获取对象 if(typeof(o) == "string") { if(document.getElementById(o)) { return document.getElementById(o); } else { alert("errId "" o ""!"); return false; } } else { return o; } } } </textarea>
<h2> 使用方法 </h2>
1.把以上代码引进你的页面 <script type="text/javascript" src="dragClass.js"></script>
2.在页面的"</body>"标签前加入以下代码:
<script type="text/javascript">
window.onload = function(){
var c = new dragClass();//创建对象
c.dragStar("拖动id","移动层id");
}
</script>
其中sd方法中的参数为:
参数一 "拖动id":鼠标经过时候鼠标样式变成移动样式模样的元素的id
参数二 "移动层id":即将移动的元素的id
<span class="red">3.建议移动层是绝对定位(position:absolute;).</span>
十三妖 ThirdteenDevil
[qq:181907667] [msn:wl181907667@hotmail.com] [e-mail:thirdteendevil@163.com]
</body></textarea>
提示:您可以先修改部分代码再运行

您可能感兴趣的文章:
Javascript 拖动效果兼容火狐
js实现拖动层,兼容IEFireFox
火狐ff浏览器代理怎么修改
js禁用tab键(兼容firefox与ie)
js的trim实现方法
基于Jquery的仿Windows Aero弹出窗(漂亮的关闭按钮)
jquery实现简单的拖拽效果实例兼容所有主流浏览器(优化篇)
如何使用HTML5实现多个元素的拖放功能
jquery div拖拽效果(兼容浏览器)
js获取select值(兼容火狐、IE浏览器)

[关闭]
~ ~