简体   繁体   English

如何使用jquery使悬停仅在具有半径边框的div中的圆形区域上触发动画

[英]How to make the hovering trigger an animation only on a circle area in a div with radius border with jquery

I am not a programmer, and I am trying my best to solve the situation but after several hours and headache I give up and I am asking for help. 我不是程序员,我正在尽力解决问题,但是经过几个小时的头痛之后,我放弃了,我正在寻求帮助。

I have a circular logo (a div with enough radius px to become a circle and some text in it), and I have an animation that come out from behind the logo when I hover on it. 我有一个圆形徽标(半径px足够大的div变成一个圆形,并且其中包含一些文本),当我将鼠标悬停在徽标上时,我会从徽标后面看到一个动画。

I noticed that my animation it triggers on the "empty area" between the circular logo and the div that hold the logo (that it is still a square). 我注意到我的动画在圆形徽标和包含徽标的div之间的“空白区域”(它仍然是正方形)上触发。 At the moment my script it is this: 目前,我的脚本是这样的:

$("#logo").hover(function(event){     // Hovering
    myHover = "transition";
    $("#black").stop().animate({"top":"-200px"}, speed/2, function(){
        myHover = 1;
    });
},function(event){      // Finish hovering
    myHover = "transition";
    $("#black").stop().animate({"top":"0px"}, speed/2, function(){
        myHover = 0;
    });
});

I tried looking on the web and on stack overflow to find something that will help me, and the nearest thing that I found it is this: 我尝试在网络上和堆栈上查找内容,以找到对我有帮助的东西,而我发现的最接近的东西是这样的:

http://jsbin.com/oqewo - from this other question Accurately detect mouseover event for a div with rounded corners http://jsbin.com/oqewo-来自另一个问题准确检测具有圆角的div的鼠标悬停事件

I tried to implement it and I did come out with something that it is not smooth enough as animation (I tried to debug trying to move back and forward with the mouse on the logo to see the reaction of the script): 我尝试实现它,但确实出现了动画效果不够平滑的东西(我尝试调试,以使鼠标在徽标上来回移动以查看脚本的反应):

$(".myCircle").hover(
    // when the mouse enters the box do...
    function(){
        var $box = $(this),
        offset = $box.offset(),
        radius = $box.width() / 2,
        circle = new SimpleCircle(offset.left + radius, offset.top + radius, radius);

        $box.mousemove(function(e){
            if(circle.includesXY(e.pageX, e.pageY) && myHover != "transition"){
                $(this).css({"cursor":"pointer"});
                myHover = "transition";
                $("#black").stop().animate({"top":"-200px"}, speed/2, function(){
                    myHover = 1;
                });
            }else if(!circle.includesXY(e.pageX, e.pageY)){
                $(this).css({"cursor":"default"});
                myHover = "transition";
                $("#black").animate({"top":"0px"}, speed/2, function(){
                    myHover = 0;
                });
            }
       });

    },
    // when the mouse leaves the box do...
    function() {       
        //alert("in")
       //$(this).includesXY(e.pageX, e.pageY)
        myHover = "transition";
        $(this).css({"cursor":"default"});
        $("#black").stop().animate({"top":"0px"}, speed/2, function(){
            myHover = 0;
        });
    }
)

I inserted a variable myHover = 0; 我插入了变量myHover = 0; at the start of my functions because I needed a variable that would let me know when the animation is completed, it is hidden behind the logo, or in transition. 在执行功能之初,因为我需要一个变量,该变量会让我知道动画何时完成,它隐藏在徽标后面或转换中。

And I don't know WHEN and HOW to use the .unbind property so I will not suck enough cpu. 而且我不知道何时和如何使用.unbind属性,因此我不会吸收足够的cpu。 Is there anything better than mouseenter event? 有什么比mouseenter事件更好的了吗? It triggers various time, and only when I move the mouse on the logo, and not when I have the mouse on the logo during the animation. 它会触发各种时间,只有在将鼠标移到徽标上时才会触发,而不会在动画过程中将鼠标移到徽标上时触发。 Anyway any suggestion or revision of any kind on approach this problem it is more than welcome :) 无论如何,关于此问题的任何建议或修订都值得欢迎:)

========================== =========================

UPDATE 更新

I might find a way, it seems to work, but I am not sure if it is possible to optimise/clean it, or if I am using unbind properly, someone can check my code? 我可能会找到一种方法,它似乎可以工作,但是我不确定是否可以优化/清理它,或者如果我使用的解除绑定正确,有人可以检查我的代码吗?

$(".myCircle").hover(
        // when the mouse enters the box do...
        function(){
            var $box = $(this),
            offset = $box.offset(),
            radius = $box.width() / 2,
            circle = new SimpleCircle(offset.left + radius, offset.top + radius, radius);

            $box.mousemove(function(e){
            if(circle.includesXY(e.pageX, e.pageY) && myHover != "transition1"){
                $(this).css({"cursor":"pointer"});
                myHover = "transition1";
                $("#black").stop().animate({"top":"-200px"}, speed/2, function(){
                    myHover = 1;
                });
            }

            else if(!circle.includesXY(e.pageX, e.pageY)){
                $(this).css({"cursor":"default"});
                if(myHover == 1 || myHover == "transition1"){
                    myHover = "transition0";
                    $("#black").stop().animate({"top":"0px"}, speed/2, function(){
                        myHover = 0;
                    });
                }
            }
       });

    },
    // when the mouse leaves the box do...
    function() {       
        if(myHover == 1 || myHover == "transition1"){
            myHover = "transition0";
            $(this).css({"cursor":"default"});
            $("#black").stop().animate({"top":"0px"}, speed/2, function(){
                myHover = 0;
            })
        };
        $("#container").unbind('mousemove');
    }
)

The SimpleCircle class used within this code, from the demo mentioned above, is defined as: 在上面提到的演示中,此代码中使用的SimpleCircle类定义为:

function SimpleCircle(x, y, r) {
  this.centerX = x;
  this.centerY = y;
  this.radius = r;
}

SimpleCircle.prototype = {
  distanceTo: function(pageX, pageY) {
    return Math.sqrt(Math.pow(pageX - this.centerX, 2) + Math.pow(pageY - this.centerY, 2));
  },
  includesXY: function(x, y) {
    return this.distanceTo(x, y) <= this.radius;
  }
};

With regard to your update, it all looks good. 关于您的更新,一切看起来都不错。

You may get a slight performance upgrade by reversing the order of the two if() parameters so that myHover != "transition1" is first. 通过反转两个if()参数的顺序,可以使性能稍有提升,从而使myHover != "transition1"首先出现。 The && is short-circuit, so if myHover != "transition1" is false, the expensive circle inclusion check does not need to be called. &&短路,因此如果myHover != "transition1"为false,则不需要调用昂贵的圆包含检查。

Also on the else if() might be worth having some variable set to something that says you have already set the cursor to stop that getting called continuously. 同样在else if()上, else if()可能值得将一些变量设置为某些值,则表示您已经设置了光标以停止连续调用该变量。

Looking at the SimpleCircle class, the only expensive operations it makes are two power calls and a square root ( Math.pow() x 2 + Math.sqrt() ). 看一下SimpleCircle类,它进行的唯一昂贵的操作是两个幂调用和一个平方根( Math.pow() x 2 + Math.sqrt() )。 Whether or not it is worth trying to get that any faster is debatable, only optimisation I can think of there is to check if the coordinates are within the square within the circle which is four quick comparisons, this covers 50% of the interior points, but obviously slows down the other 50% of points. 是否值得尝试使速度更快是值得商,的,只有我能想到的最优化是检查坐标是否在圆的正方形内,这是四个快速比较,覆盖了50%的内部点,但显然会减慢其他50%的点数。 To see if it improved matters you would have to test it. 要查看它是否有所改善,您必须对其进行测试。

内方圆内方圆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM