JQuery Animate 延迟问题

发布于 2024-08-20 21:44:54 字数 2671 浏览 5 评论 0原文

下面是 HTML 和我有的 JQuery 代码:

HTML 我有:

<div class="announcement">
    <img id="imgBanner" style="cursor: pointer;" onmouseover="showPopup();" />
</div>
<div id="divArrow" class="arrow">
    <img id="imgExpandContract" style="cursor: pointer;" alt="Arrow" border="0"onmouseover="showPopup();" />
</div>
<div id="window">
    <!-- some html content acting as a fly-out -->
</div>

JS 代码:

var imgBanner = "xyx.png";
var imgArrowExpand = "xyx.png";
var imgArrowContract = "xyx.png";
var isDone = false;

function showPopup() {
    try {
        var imgWidth = $("#imgExpandContract").width();
        var posX = document.getElementById("imgExpandContract").offsetLeft + imgWidth;
        if (!$("#window").is(":animated")) {
            $("#window").animate({ left: posX }, 800, 'easeOutSine',
                                function() {
                                    isDone = true;
                                    $("#imgExpandContract").attr("src", imgArrowContract);
                                    $("#window").bind("mouseenter", function() { $("#window").bind("mouseleave", function() { closePopup(); }); });
                                }
            );
        }
    }
    catch (error) {
        //disable the banner, arrow images
        document.getElementById("imgBanner").style.visibility = 'hidden';
        document.getElementById("imgExpandContract").style.visibility = 'hidden';
    }
}

function closePopup() {
    try {
        if (isDone) {
            var posY = $("#window").width();
            $("#window").animate({ left: -posY }, 600, 'linear',
                            function() {
                                isDone = false;
                                $("#imgExpandContract").attr("src", imgArrowExpand);
                                $("#window").unbind("mouseenter");
                                $("#window").unbind("mouseleave");
                            }
            );
        }
    }
    catch (error) {
        //disable the banner, arrow images
        document.getElementById("imgBanner").style.visibility = 'hidden';
        document.getElementById("imgExpandContract").style.visibility = 'hidden';
    }
}

我当前遇到 2 个问题:

  1. 当我将鼠标移开 #window div 时,在调用 mouseleave 之前会有很小的延迟。我怎样才能让它消失?在导致 mouseleave 之前,它会停留几毫秒。

  2. mouseleave 事件有时不会触发...偶尔发生,但它确实发生了。我必须将鼠标移到 #window div 上然后向后移动(基本上必须执行两次)?谁能告诉我为什么会发生这种情况以及我该如何处理?

除了在 JQuery 中使用 animate() 之外,还有更好的解决方案吗?很乐意接受所有人/任何建议。我正在尝试对 div 内的内容执行 Fly-out/slideIn 效果。

非常感谢您的帮助

Below is the HTML & JQuery code I have:

HTML I have:

<div class="announcement">
    <img id="imgBanner" style="cursor: pointer;" onmouseover="showPopup();" />
</div>
<div id="divArrow" class="arrow">
    <img id="imgExpandContract" style="cursor: pointer;" alt="Arrow" border="0"onmouseover="showPopup();" />
</div>
<div id="window">
    <!-- some html content acting as a fly-out -->
</div>

JS Code:

var imgBanner = "xyx.png";
var imgArrowExpand = "xyx.png";
var imgArrowContract = "xyx.png";
var isDone = false;

function showPopup() {
    try {
        var imgWidth = $("#imgExpandContract").width();
        var posX = document.getElementById("imgExpandContract").offsetLeft + imgWidth;
        if (!$("#window").is(":animated")) {
            $("#window").animate({ left: posX }, 800, 'easeOutSine',
                                function() {
                                    isDone = true;
                                    $("#imgExpandContract").attr("src", imgArrowContract);
                                    $("#window").bind("mouseenter", function() { $("#window").bind("mouseleave", function() { closePopup(); }); });
                                }
            );
        }
    }
    catch (error) {
        //disable the banner, arrow images
        document.getElementById("imgBanner").style.visibility = 'hidden';
        document.getElementById("imgExpandContract").style.visibility = 'hidden';
    }
}

function closePopup() {
    try {
        if (isDone) {
            var posY = $("#window").width();
            $("#window").animate({ left: -posY }, 600, 'linear',
                            function() {
                                isDone = false;
                                $("#imgExpandContract").attr("src", imgArrowExpand);
                                $("#window").unbind("mouseenter");
                                $("#window").unbind("mouseleave");
                            }
            );
        }
    }
    catch (error) {
        //disable the banner, arrow images
        document.getElementById("imgBanner").style.visibility = 'hidden';
        document.getElementById("imgExpandContract").style.visibility = 'hidden';
    }
}

I am currently having 2 problems:

  1. When I mouse off the #window div, there is small delay before which the mouseleave is being called. How can I make this go away? It kinda stays for couple of millinseconds before cauing mouseleave.

  2. The mouseleave event sometimes does not fire... Happens occasionally, but it happens. I have to move my mouse on the #window div and move back (have to do it twice essentially)? Can anyone let me know why this happens and how i can handle?

Are there better solutions to do this other than using animate() in JQuery? Would be happy to all/any suggestions. I am trying to do a fly-out/slideIn effect with content inside a div.

Thank you very much for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-08-27 21:44:54

好吧,我不知道这会解决您所说的问题,但有很多事情会对您的代码整体有所帮助。例如我的第一印象是:

“好吧,他正在使用 jquery...等等,我以为他正在使用 jquery...WTF?”。

  1. 为什么使用getElementById?使用$('#myid')
  2. 为什么要使用style.visibility?使用 $(selector).css('visibility', value);
  3. 不要使用元素属性来绑定事件...使用 jquery $(selector).hover(openPopup, closePopup );

好的,当 mouseleave 无法触发时,您确定鼠标离开了该区域吗?我的意思是你确定 div 的尺寸比你想象的要大一点吗?如果情况并非如此,则可能只是执行该函数所需的时间,或者可能是动画尚未完成(即 isDone = false)。在我看来,我不会尝试检测某些动画是否已完成,而是调用 $(element).stop(true); 在其端点处停止动画,然后继续执行其他操作动画片。这应该是非常安全的。我还相信,如果您使用 false 调用它或完全省略参数,它将准确地停止在它所在的位置...允许您从当前位置激活输出动画,而无需计算位置。

另外,我不知道这实际上是什么样子,但可能您甚至不需要使用 animate 您可能可以使用内置效果之一,例如 slide 或一些东西——你可能也想看看那些。

Well i dont know that this will sove your stated problems but there are a number of things that are going to help your code overall. For example my first impression was:

"Ok hes using jquery... wait i thought he was using jquery... WTF?".

  1. Why use getElementById? use $('#myid').
  2. Why use style.visibility? use $(selector).css('visibility', value);
  3. Don't use the element attributes to bind events... use jquery $(selector).hover(openPopup, closePopup);

Ok with that out of the way are you when the mouseleave fails to fire are you sure the mouse left the area? I mean are you sure the div's dimensions are a little larger than you think? If that isnt the case it could simple be the time it takes to execute the function or it could be that it isnt done animating (ie. isDone = false). In my opinion rather than trying to detect whether some thing is done animating i would jsut call $(element).stop(true); to stop the animation at its endpoint, and then continue on with your other animation. That should be pretty failsafe. Also i believe if you call it with false or omit the argument entirely it will stop it exactly where it is... allowing you to activate your out animation from the current place without having to calculate position.

Additionally i dont know what this actually looks like but it may be that you dont even need to use animate you may be able to use one of the built in effects like slide or something - you might wan tto look in to those as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文