当前位置: 代码网 > it编程>编程语言>Javascript > 支持IE,firefxo,chrome浏览器下鼠标拖动和拖拽的鼠标指针特效

支持IE,firefxo,chrome浏览器下鼠标拖动和拖拽的鼠标指针特效

2024年05月15日 Javascript 我要评论
核心代码:<!doctype html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http

核心代码:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>鼠标拖动和拖拽的鼠标指针特效</title>
<style>
#box{width:100px;height:100px;background:#966;position:absolute;left:100px;top:100px;cursor:pointer;}
</style>
</head>
<body>
<div id="box"></div>
<script>
var doc=document;
function getviewport(){
  return {width:math.max(document.documentelement.clientwidth,document.documentelement.scrollwidth),
      height:math.max(document.documentelement.clientheight,document.documentelement.scrollheight)
  };
}
function comparenum(x,minnum,maxnum){
 switch(true){
 case x<minnum:x=minnum;break;
 case x>maxnum:x=maxnum;break;
 default:x;
 }
 return x;
 }

box.style.left='100px'
box.style.top='100px'

box.onmousedown=function(e){
 var e=e||window.event;
 var maxl=getviewport().width-100,
   maxt=getviewport().height-100;
 this.startl=parseint(this.style.left);
 this.startt=parseint(this.style.top);
 //alert(e.clientx)
 this.startx=e.clientx;
 this.starty=e.clienty;
 //alert(maxl+':'+maxt+':'+l+':'+t)
 doc.onmousemove=function(e){
 posxy(e,0,0,maxl,maxt);
 };
 doc.onmouseup=function(){
  doc.onmousemove=null;
 doc.onmouseup=null;
 };
 return false;
}
function posxy(e,minl,mint,maxl,maxt){
  var e=e||window.event;
 var x=box.startl-(box.startx-e.clientx);
 var y=box.startt-(box.starty-e.clienty);
 box.style.left=comparenum(x,minl,maxl)+'px'
 box.style.top=comparenum(y,mint,maxt)+'px'
}
</script>
</body>
</html>

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com