【问题标题】:How to implement this jquery example correctly. Show/Hide Div on Hover, navigation如何正确实现这个 jquery 示例。悬停时显示/隐藏 Div,导航
【发布时间】:2011-11-01 06:53:22
【问题描述】:

我正在尝试执行此已回答问题Jquery show div on hover then when user hovers out of div hide it help? 中的说明,然后添加更多功能。这是我将问答代码组合成的:

$(document).ready(function(){
//when user hovers over plans the mainnavbottom is shown
    $(".plans").hover(
    $('.mainnavbottom').bind('mouseenter mouseleave', function(event) {
        switch(event.type) {
            case 'mouseenter':
               // when user enters the div
               $(".mainnavbottom").show("fast");
            break;
            case 'mouseleave':
              // leaves
              $(".mainnavbottom").hide("slow");
            break;
        }
    });

 });

我无法让它工作。我确定这是因为我是 javascript n00b,所以任何建议都将不胜感激。

我也尝试过jQuery hoverIntent Plugin show / hide div on parent hover, but keep showing when hovered,但还是不开心。

也许我在头部错误地添加了答案代码?以我引用的两个例子为例,任何人都可以向我展示头部的完整 javascript 代码吗?我觉得我在这里犯了一个简单的错误。

假设我可以让它工作,我想向它添加另一个维度。一旦上述代码中的 div “出现”,它包含更多链接,然后用户可以将每个“按钮”悬停并查看更多信息(文本和可能的图像)。如果可能的话,我希望该文本和图像成为链接的可点击部分,但如果没有它也可能存在。

可能是,一旦我让上面的代码工作,我可以将相同的概念应用到辅助悬停,但我不确定,因为上面的代码没有成功。谁能发现我的(可能很明显)错误?我提到的附加功能听起来会不会是个问题。

在此先感谢您提供任何帮助。

编辑 - 有人要求我发布所有代码

    <html>
<head>
<script src="jquery.js"></script>
<script language="Javascript">
$(document).ready(function(){
//when user hovers over plans the mainnavbottom is shown
    $("#plans").hover(
    $('#mainnavbottom').bind('mouseenter mouseleave', function(event) {
        switch(event.type) {
            case 'mouseenter':
               // when user enters the div
               $("#mainnavbottom").show("fast");
            break;
            case 'mouseleave':
              // leaves
              $("#mainnavbottom").hide("slow");
            break;
        }
    });

 });
</script>
</head>

<body>
       <div id="plans"> // some stuff </div>
       <div id="mainnavbottom"> 
          // some other stuff that I want to 
          // keep showing if users hover over it 
       </div>
</body>
</html>

【问题讨论】:

  • 好的,完成。根据下面的 cmets 从 class 更改为 id。
  • 你也可以发布 CSS 吗?或者有没有?
  • 还没有 CSS,我更关心的是在设计之前能否获得我想要的功能。

标签: javascript jquery navigation


【解决方案1】:

根据我的经验,jquery 不擅长使用对象类。我会尝试将所有 (".mainnavbottom") 更改为 ("#mainnavbottom") 并确保有问题的 div 具有 id="mainnavbottom"

编辑:(".plans") 也是如此

【讨论】:

  • 这有什么帮助? jQuery 完全能够按类名选择元素。
  • 有效的 HTML 应该只有一个 ID;如果他有多个,他应该使用类;否则,ID 是更好的选择。
  • 这个和这个没关系,jquery比较擅长按类选择,是jquery的核心特性。
【解决方案2】:

请参阅下面的编辑:

最好这样写(请原谅格式,我在我的 iTouch 上):

$(function(){
    $(".plans").hover(function(){
               $(".mainnavbottom").show("fast");
},function(){
              $(".mainnavbottom").hide("slow");
 });
});

||编辑||

保持 mainnavbottom 可见是很棘手的。我个人会选择使用 CSS 样式和 :hover 值而不是使用 jQuery。但是,我希望这对你有用。我必须设置变量,并测试鼠标是否悬停在计划或 mainnavbottom div 上。我需要在 mainnavbottom 上使用 .mouseleave() 函数。这是我使用的jsFiddle,下面是相关代码:

var nav = $('#mainnavbottom');
var plans = $('#plans');
    plans.mouseover(function(){     
        nav.show("fast");
    }).mouseout(function(){
        if(nav.is(':hover')){
            nav.mouseleave(function(){
                if(plans.is(':hover')){
                    return;
                        }else{
                                     nav.hide('slow');
                        }
            });
        }else{
         nav.hide('slow');
        }
    });

【讨论】:

  • 感谢您的快速回复。这个功能,但是一旦它出现(它只是消失),你就不能将鼠标悬停在 div 上,这就是我需要的。
  • 也许你可以把 .stop().delay(3000) 放在 hide 函数之前。
  • 或者更好的是,将 .mainnavbottom 添加到事件处理程序中。我马上更新。
  • 实际上我会在 .show() 动画中添加条件 if(nav.is(':not(":animated")')) 以防止排队。
  • 我发现 slideUp() 和 slideDown() 的动画效果更好。
【解决方案3】:

你的牙套坏了。

.mouseenter 和 mouseleave 不是必需的,因为 .hover() 会为您执行此操作。

这是我想出的(出于某种原因,它不喜欢在 doc.ready 中,但我有有限的时间来研究它)

(仅供参考,您的代码(以及我建议的修复)的编写方式,“#mainnavbottom”最初不会被隐藏。只有在您第一次将鼠标悬停在它上面时它才会被隐藏 - 但我离题了..)

$("#plans").hover(
    function() {
        $("#mainnavbottom").show("fast");
    },
    function() {
       $("#mainnavbottom").hide("slow");
    }
);

这是一个小提琴。

http://jsfiddle.net/KevinGabbert/pcJt8/


但是,如果您仍然想要这些事件,那么您示例中的大括号仍然搞砸了,这是我尝试做的最好的尝试。 (基本上,我把你所做的事情合法化了。我知道有时我需要可视化,所以我会去那里。)

但同样,这不是好的代码,因为它可能会发生一些邪恶的事件疯狂......

(不要在家里尝试)

 $("#plans").hover(
        function() {
            alert("hover");

            $('#mainnavbottom').bind('mouseenter mouseleave', function() {
                //yada yada

            });
        },
        function() {
            alert("unhover");

            $('#mainnavbottom').bind('mouseenter mouseleave', function() {
                 //yada yada
            }); 
        }
    );

另外,详细说明 Artur 上面所说的:您要使用 id “#plans”而不是类“.plans”的原因是因为页面上只有 1 个 id。 JQuery 的遍历会在找到后停止。

当使用类选择器“.plans”时,它会遍历整个页面来寻找该类的所有元素,如果你有一个大的或复杂的页面,这可能会使事情变得混乱,并使调试更加困难.

【讨论】:

  • 谢谢,我喜欢小提琴(顺便说一句,它很酷,不知道)但不幸的是,我需要将鼠标悬停在出现的 div 上。我将回顾人们今天晚些时候提出的建议,以便可能解决问题。一旦我审查了所有内容,我会回复。
【解决方案4】:
    $("#plans").hover(
      function() {
        $("#mainnavbottom").show("fast");               
        $('#mainnavbottom').bind('mouseenter mouseleave', function(event) {
          switch(event.type) {
            case 'mouseenter':
              // when user enters the div
              $("#mainnavbottom").show("fast");
            break;
           case 'mouseleave':
             // leaves
             $("#mainnavbottom").hide("slow");
           break;
          }
        });
      },
      function() {
        $("#mainnavbottom").hide("slow");               
        $('#mainnavbottom).bind('mouseenter mouseleave', function(event) {
          switch(event.type) {
            case 'mouseenter':
              $("#mainnavbottom").show("fast");
            break;
            case 'mouseleave':
              $("#mainnavbottom").hide("slow");
            break;
          }
         }); 
       }
     );

【讨论】:

  • 习惯上添加一些对您的代码的解释,它是如何工作的,或者它与 OP 的代码或其他答案的不同之处。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 2011-02-08
  • 1970-01-01
  • 2011-07-21
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多