欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-03 09:24:58
核心代码: &...

核心代码:

<!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>


[ctrl+a 全选 注:引入外部js需再刷新一下页面才能执行]
相关标签: 鼠标拖动 拖拽