下拉框不断闪烁 - JQuery 和 CSS

发布于 2024-08-05 09:31:26 字数 523 浏览 3 评论 0原文

我在一些非常基本的事情上遇到了困难。我正在尝试用 JQuery 和 CSS 实现一个下拉框。我不明白为什么当我将鼠标悬停在“产品”链接上时,我的下拉框不断闪烁。

这是我正在做的事情的演示。 http://174.132.101.73/~ree/header/

如果有任何帮助,我将不胜感激。

编辑

好吧,可以通过查看我给出的链接的源代码找到代码。您可能需要显示/隐藏下拉框的主要部分是这样的,以及 CSS:

 $(document).ready(function(){
  $('li.headlink').hover(
   function() { $('ul', this).css('display', 'block'); },
   function() { $('ul', this).css('display', 'none'); });
 });

I am having difficulty with something really basic. I am trying to implement a drop down box with JQuery and CSS. I can not figure out why my drop down box keeps flickering when I hover over the "Products" link.

Here is a demo of what I am working on. http://174.132.101.73/~ree/header/

I would appreciate any help on this.

Edit

Well the code can be found by looking at the source code of the link I gave. The main part you might need that displays/hides the drop-down box is this, alongside CSS:

 $(document).ready(function(){
  $('li.headlink').hover(
   function() { $('ul', this).css('display', 'block'); },
   function() { $('ul', this).css('display', 'none'); });
 });

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

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

发布评论

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

评论(2

记忆消瘦 2024-08-12 09:31:26

#header_wrapper 的宽度固定为 950px,#logo#nav 浮动在其中。由于 #logo 为 250 像素,因此为 #nav 中的内容留下了 700 像素(其元素也是浮动的)。

一切都很好,直到“产品”子菜单变得可见。然后,由于额外的内容,它的 .headlink 变得更宽,这将 #nav 的整体大小推到了被迫向下到下一个“行”的位置,如下所示#logo。这就是浮动元素的工作方式;它们水平地填充空间,直到某个块太大并被推到第一个块下方。

闪烁是由于 :hover 不再处于活动状态且子菜单被隐藏所致。然后一切又回到原来的样子。只是鼠标指针仍然在那里,因此现在 :hover 再次处于活动状态。重复。

我不确定如何解决它,但在我看来,这就是问题所在。也许您可以改变元素的嵌套方式,或者不要浮动导航部分。为了快速修复,您可以将 #header_wrapper 宽度更改为更大的值,例如 1450px,只是为了查看“产品”子菜单的行为方式,并解决其问题。

#header_wrapper has width fixed at 950px, and #logo and #nav are floated within it. Since #logo is 250px, that leaves 700px for what's in #nav (whose elements are also floated).

Everything fits just fine until the 'Products' submenu becomes visible. Then its .headlink becomes much wider, due to the extra content, which pushes the overall size of #nav to where it is forced down to the next "line", below #logo. This is the way floated elements work; they fill up the space horizontally until some block is too big and gets pushed down below the first one.

The flicker results from the fact that :hover is no longer active and the submenu gets hidden. Then everything goes back to the way it was. Except that the mouse pointer is still there, and so now :hover is active again. Repeat.

I'm not sure how I'd fix it, but that appear to me to be what's wrong. Maybe you could alter the way your elements are nested, or don't float the nav section. For a quick fix, you could change the #header_wrapper width to something much bigger, such as 1450px, just to see how the 'Products' submenu is behaving, and work out its kinks.

丑丑阿 2024-08-12 09:31:26

好吧,过去 15 分钟我一直在解决这个问题,终于找到了实际的解决方案。

我过去编写过下拉菜单,很好,并且想知道为什么我现在使用可靠的代码和超时而遇到此闪烁问题。

好吧,我发现我使用了错误的 Jquery 事件。不要使用 jquery HOVER 事件,而是使用 jquery mouseover 事件和 mouseout 事件。这将解决与 HOVER 相关的问题。

接下来设置隐藏超时,显示时清除超时,隐藏前清除超时。将其设置为您希望菜单在滚动时显示的时间。

如果您在悬停时使用超时,但仍然出现闪烁,请务必使用 mouseover 事件。

$(".btn").mouseover(function() {
    clearTimeout(this.timeout);
    $(this).addClass("hover"); 
    /* can add class, or manually set menu visible, i prefer using CSS class */
}).mouseout(function() {
    clearTimeout(this.timeout);
    /* clear timeout in case 2 mouseout events fire at same time */
    this.timeout = setTimeout($.proxy(function() {
        $(this).removeClass("hover");
    }, 300));
});

此代码应该可重复用于页面中的所有下拉菜单。

Well, I have been tackling this issue for the last 15 minutes and have finally found the ACTUAL solution.

I have coded dropdowns in the past, fine, and was wondering why I was now getting this flickering issue as I was using solid code and timeouts.

Well, I figured out I was using the wrong Jquery event. Do not use the jquery HOVER event, instead use the jquery mouseover event and mouseout event. This will solve the HOVER related issues.

Next set up a timeout for hiding and, when showing, clear the timeout and, before hiding, clear the timeout. Set this for however long you want the menu to be there when you scroll out.

If you use the timeout with the hover and you still get flicker, be sure to use mouseover event.

$(".btn").mouseover(function() {
    clearTimeout(this.timeout);
    $(this).addClass("hover"); 
    /* can add class, or manually set menu visible, i prefer using CSS class */
}).mouseout(function() {
    clearTimeout(this.timeout);
    /* clear timeout in case 2 mouseout events fire at same time */
    this.timeout = setTimeout($.proxy(function() {
        $(this).removeClass("hover");
    }, 300));
});

This code should be re-usable to ALL drop downs in your page.

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