鼠标的位置控制

div的onmouseout事件让div消失时,会出现这样的情况,就是当鼠标移至div中的其它内容时,此时也判定为离开div,会触发onmouseout事件,这样div中的内容就不能操作了。解决的办法是当触发onmouseout事件时,先判断鼠标是否在div内,如果在,说明鼠标并没有离开div,就不删除div,否则,删除之。OK,现在问题解决了。 
就是找到该div左上角和右下角坐标,判断鼠标的坐标是否在这一区域
    div.οnmοuseοut=function(event){  
                        var div = document.getElementById("test");  
                    <span style="color:#FF6600;">var x=event.clientX;  
                        var y=event.clientY;  
                        var divx1 = div.offsetLeft;  
                        var divy1 = div.offsetTop;  
                        var divx2 = div.offsetLeft + div.offsetWidth;  
                        var divy2 = div.offsetTop + div.offsetHeight;  </span>  
                        if( x < divx1 || x > divx2 || y < divy1 || y > divy2){  
                                        //如果离开,则执行。。  
                                    }  

后面为一些常用属性方便查找: 
clientHeight     获取对象的高度,不计算任何边距、边框、滚动条,但包括该对象的补白。   
clientLeft     获取    offsetLeft     属性和客户区域的实际左边之间的距离。 
clientTop     获取    offsetTop     属性和客户区域的实际顶端之间的距离。 
clientWidth     获取对象的宽度,不计算任何边距、边框、滚动条,但包括该对象的补白。 
offsetHeight     获取对象相对于版面或由父坐标    offsetParent     属性指定的父坐标的高度。 
offsetLeft     获取对象相对于版面或由    offsetParent     属性指定的父坐标的计算左侧位置。 
offsetParent     获取定义对象    offsetTop     和    offsetLeft     属性的容器对象的引用。 
offsetTop     获取对象相对于版面或由    offsetTop     属性指定的父坐标的计算顶端位置。 
offsetWidth     获取对象相对于版面或由父坐标    offsetParent     属性指定的父坐标的宽度。 
offsetX     设置或获取鼠标指针位置相对于触发事件的对象的    x     坐标。 
offsetY     设置或获取鼠标指针位置相对于触发事件的对象的    y     坐标。 
clientX,clientY   鼠标当前相对于网页的位置,当鼠标位于页面左上角时clientX=0, clientY=0 
screenX, screenY是相对于用户显示器的位置 
网页可见区域宽: document.body.clientWidth 
网页可见区域高: document.body.clientHeight 
网页可见区域宽: document.body.offsetWidth    (包括边线的宽) 
网页可见区域高: document.body.offsetHeight   (包括边线的宽) 
网页正文全文宽: document.body.scrollWidth 

网页正文全文高: document.body.scrollHeight 

网页被卷去的高: document.body.scrollTop 
网页被卷去的左: document.body.scrollLeft 
网页正文部分上: window.screenTop 
网页正文部分左: window.screenLeft 
屏幕分辨率的高: window.screen.height 
屏幕分辨率的宽: window.screen.width 
屏幕可用工作区高度: window.screen.availHeight 

屏幕可用工作区宽度:window.screen.availWidth


var wrap = document.getElementById('wrap');
var hoverDir = function(e){
    var w=wrap.offsetWidth;
    var h=wrap.offsetHeight;
    var x=(e.clientX - wrap.offsetLeft - (w / 2)) * (w > h ? (h / w) : 1);
    var y=(e.clientY - wrap.offsetTop - (h / 2)) * (h > w ? (w / h) : 1);
    var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4;
    var eventType = e.type;
    var dirName = new Array('上方','右侧','下方','左侧');
    if(e.type == 'mouseover' || e.type == 'mouseenter'){
        wrap.innerHTML=dirName[direction]+'进入';
    }else{
        wrap.innerHTML=dirName[direction]+'离开';
    }
}
if(window.addEventListener){
    wrap.addEventListener('mouseover',hoverDir,false);
    wrap.addEventListener('mouseout',hoverDir,false);
}else if(window.attachEvent){
    wrap.attachEvent('onmouseenter',hoverDir);
    wrap.attachEvent('onmouseleave',hoverDir);
}
$(".overlayLink").bind("mouseenter mouseleave",function(e){

/** the width and height of the current div **/
var w = $(this).width();
var h = $(this).height();

/** calculate the x and y to get an angle to the center of the div from that x and y. **/
/** gets the x value relative to the center of the DIV and "normalize" it **/
var x = (e.pageX - this.offsetLeft - (w/2)) * ( w > h ? (h/w) : 1 );
var y = (e.pageY - this.offsetTop  - (h/2)) * ( h > w ? (w/h) : 1 );

/** the angle and the direction from where the mouse came in/went out clockwise (TRBL=0123);**/
/** first calculate the angle of the point, 
 add 180 deg to get rid of the negative values
 divide by 90 to get the quadrant
 add 3 and do a modulo by 4  to shift the quadrants to a proper clockwise TRBL (top/right/bottom/left) **/
var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180 ) / 90 ) + 3 )  % 4;


/** do your animations here **/ 
switch(direction) {
 case 0:
  /** animations from the TOP **/
 break;
 case 1:
  /** animations from the RIGHT **/
 break;
 case 2:
  /** animations from the BOTTOM **/
 break;
 case 3:
  /** animations from the LEFT **/
 break;
}});
var direction =  Math.round( Math.atan2(y, x) / 1.57079633 + 5 ) % 4

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值