【问题标题】:Is it possible to cancel hover event based on condition while still hovering?是否可以在仍然悬停时根据条件取消悬停事件?
【发布时间】:2014-06-03 23:06:25
【问题描述】:

我需要让执行函数的悬停事件根据 margin-top 的条件停止运行该函数。

这是我的小提琴 - http://jsfiddle.net/Joe_Foster/LwrVv/

$(document).ready(function(){

function moveUp(){
$('#a').animate({
    "margin-top" : "-=" + 30},200, "linear", moveUp);
};
/*  $('.up').hover(function(){
    if ($m =='0px'){
    alert('stop');
    }
    else{
        moveUp();
    }
});
*/
$('.up').hover(function(){
    moveUp();
},
function(){
    $('#a').stop();
});


//---------------------------------------

function moveDown(){
$('#a').animate({
    "margin-top" : "+=" + 30},200, "linear", moveDown);
};
$('.down').hover(function(){
    moveDown();
},
function(){
    $('#a').stop();
});

});

在我眼睛里的血管爆裂之前请帮帮我:)

谢谢。

【问题讨论】:

  • “基于margin-top的条件”是什么意思?
  • 我的函数会导致我的(黑色)div 向上或向下移动我的容器('#a')(通过调整它的'margin-top'),具体取决于您悬停在哪个黑色 div 上。我需要的是让悬停停止执行函数,例如,“margin-top”为“0px”或“margin-top”为“450px”(为了参数)。
  • “mouseleave”停止了该功能,但我不知道如何在仍然悬停时根据条件停止该功能(mouseenter)。

标签: javascript jquery function hover


【解决方案1】:

我只是在看你的 JS Fiddle,虽然我不确定你希望停止什么值,但这是我用来使它工作的代码:

JS:

    var upperThreshold = -607;
    var lowerThreshold = 0;
    function moveUp()
    {
        if ($("#a").css("marginTop").replace(/px/,'') <= upperThreshold)
        {
            console.log($("#a").css("marginTop"));
            $('#a').stop();
            return false;
        }
        else
        {
            $('#a').animate({"margin-top" : "-=" + 30},200, "linear", moveUp);
        }
    }

    function moveDown()
    {
        if ($("#a").css("marginTop").replace(/px/,'') >= lowerThreshold)
        {
            console.log($("#a").css("marginTop"));
            $('#a').stop();
            return false;
        }
        else
        {
            $('#a').animate({"margin-top" : "+=" + 30},200, "linear", moveDown);
        }        
    }

$(document).ready(function()
                  {

                      $('.down').hover(function()
                                       {
                                           moveDown();
                                       },
                                       function()
                                       {
                                           $('#a').stop();
                                       }
                                      );

                      $('.up').hover(function()
                                     {
                                        moveUp();
                                     },
                                     function()
                                     {
                                        $('#a').stop();
                                     }
                                    );
});

JSFiddle:http://jsfiddle.net/irishgeek82/ePdE5/

如果这对你有用,请告诉我! :)

【讨论】:

  • 这个设计有一些问题,因为使用了魔法值来知道何时开始和停止。
  • 明确定义为变量的阈值远非“神奇”数字。如果我只是输入 -607 和 0 内联,我会认为您的评论有效。此外,选择的值是通过利用个人 jsFiddle 进行的实验选择的。我明确表示我不知道个人希望在哪里停止动画,所以我让它从示例列表的末尾停止一帧。提出问题的人可以根据自己的喜好调整阈值逻辑。但是,答案符合请求的标准。
  • IrishGeek82 - 是的!谢谢!逻辑就像我需要的那样工作,我现在可以专注于使它更具视觉吸引力。再一次 - 我不能感谢你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多