zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    前端接收后端返回数据中【后端返回数据
    JavaSE 文件部分常用功能示例
    springboot controller路径名设置
    springboot 文档学习记录
    controller 方法路径、参数的使用
    Vue制作音乐播放器_基于网易云音乐的接口
    Java复习_枚举
    Java复习_static用例_单例模式_懒汉式
    c++和python中的sort
    通过portainer来管理容器和镜像
  • 原文地址:https://www.cnblogs.com/xinlinux/p/4442175.html
Copyright © 2011-2022 走看看