JS判断鼠标从哪个方向进入DIV容器

摘要:
它还不够大。别介意。。。

   写的不够高大上 , 不要介意哦。。。

Js:

    
//进去
$(".flash").bind("mouseenter",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:
        $('.showD').css({
            'top':'-200px',
            'left':'0',
            'opacity':'0',
        }).stop().animate({
            'top':'0',
            'left':'0',
            'opacity':'1',
        },300)
      /** animations from the TOP **/
     break;
     case 1:
        $('.showD').css({
            'top':'0',
            'left':'200px',
            'opacity':'0',
        }).stop().animate({
            'top':'0',
            'left':'0',
            'opacity':'1',
        },300)
      /** animations from the RIGHT **/
     break;
     case 2:
        $('.showD').css({
            'top':'200px',
            'left':'0',
            'opacity':'0',
        }).stop().animate({
            'left':'0',
            'top':'0',
            'opacity':'1',
        },300)
      /** animations from the BOTTOM **/
     break;
     case 3:
        $('.showD').css({
            'top':'0',
            'left':'-200px',
            'opacity':'0',
        }).stop().animate({
            'left':'0',
            'right':'0',
            'opacity':'1',
        },300)
      /** animations from the LEFT **/
     break;
     
}});

//出来    
$(".flash").bind("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:
        $('.showD').css({
            'right':'0',
        }).stop().animate({
            'right':'0',
            'top':'-200px',
        },300)
      /** animations from the TOP **/
     break;
     case 1:
        $('.showD').css({
            'top':'0',
        }).stop().animate({
            'top':'0',
            'left':'200px',
        },300)
      /** animations from the RIGHT **/
     break;
     case 2:
        $('.showD').css({
            'top':'0',
            'left':'0',
        }).stop().animate({
            'left':'0',
            'top':'200px',
        },300)
      /** animations from the BOTTOM **/
     break;
     case 3:
        $('.showD').css({
            'top':'0',
            'left':'0',
        }).stop().animate({
            'left':'-200px',
            'right':'0',
        },300)
      /** animations from the LEFT **/
     break;
     
}});

HTML:

        <div class="flash">
             <img class="pic_bg" src="http://img0.imgtn.bdimg.com/it/u=261025820,3584077840&fm=21&gp=0.jpg" style="100%;height:100%"/>
             <div class="showD">
             </div>
        </div>

Css:

.flash{
    200px;
    height:200px;
    margin-left:30%;
    background-color:red;
    position:relative;
    overflow:hidden;
}
.showD{
    background: #469acb;
    position: absolute;
    100%;
    height:200px;
}

免责声明:文章转载自《JS判断鼠标从哪个方向进入DIV容器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SpringBoot返回html页面click和mousedown的区别下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue中&amp;lt;template&amp;gt;中v-for的使用以及&amp;lt;template&amp;gt;多层嵌套问题

一、在template中使用v-for没有效果 如上面代码所示想循环展示子菜单时使用的是v-for命令,但执行后却没有效果,子菜单的数据获取不到。 原因:v-for是循环指令,它返回多个值,而这里的template是根节点,根节点只有一个,根节点不能有多个,所以v-for写在根节点上就有问题,行不通。 解决:在其外面可以包裹一层div,使其不是根节点即可...

js 添加天数

//日期加上天数得到新的日期 //dateTemp 需要参加计算的日期,days要添加的天数,返回新的日期,日期格式:YYYY-MM-DD function getNewDay(dateTemp, days) { var dateTemp = dateTemp.split("-"); var nDate = new...

JS 语法之--函数,异常

1、函数 2、函数表达式 使用表达式定义函数,表达式中的函数名可以省略,如果这个函数名不省略,也只能用在此函数内部。 测试:匿名函数 + 函数表达式 1 //匿名函数 2 const add = function(x, y) { 3 return x +y; 4 }; 5 console.log(typeof(add)) // function...

Js基础知识4-函数的三种创建、四种调用(及关于new function()的解释)

在js中,函数本身属于对象的一种,因此可以定义、赋值,作为对象的属性或者成为其他函数的参数。函数名只是函数这个对象类的引用。 函数定义 1 //函数的三种创建方法(定义方式) 2 function one(){ //函数声明语句,不属于任何对象,始终默认为全局对象 3 console.log(...

【转】百度统计js被劫持用来DDOS Github

原文链接:http://drops.wooyun.org/papers/5398 今天中午刷着全国最大的信息安全从业人员同性交友社区zone.wooyun.org的时候,忽然浏览器每隔2秒就不断的弹窗: malicious javascript detected on this domain 我第一反应就是不知道哪个调皮的基友又把zone给XSS了,...

【JS笔记】5.3 Date类型

Date类型存储的信息:从UTC(1970年1月1日0时)开始经过的毫秒数 创建Date对象: 使用构造函数Date() 表示当前毫秒数的Date对象:var now = new Date();//不用参数时默认为当前毫秒数 表示指定毫秒数的Date对象:var date1 = new Date(milliseconds); 获取milliseconds方...