简体   繁体   English

如何将鼠标悬停在一个列表的元素上并显示另一列表的元素?

[英]How to hover over elements of one list and display elements of another list?

I have this HTML 我有这个HTML

<ul class="nav">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>

<ul class="output">
<li>Content and output here 1</li>
<li>Content and output here 2</li>
<li>Content and output here 3</li>
</ul>

and this Javascript 和这个Javascript

 $(function(){

        $(".nav li").hover(function(){

            $(this).addClass("hover");
            $('.output li').css('visibility', 'visible');

        }, function(){

            $(this).removeClass("hover");
            $('.output li').css('visibility', 'hidden');

        });

    });

I'm trying to make it work where hovering over Link 1 shows output 1 and hovering over Link 2 shows output 2, etc. But right now it doesn't matter which nav link I hover over, output 1 always shows. 我试图使其工作在将鼠标悬停在链接1上显示输出1而将鼠标悬停在链接2上显示输出2,依此类推。但是现在我悬停在哪个导航链接上都没关系,输出1总是显示。 Also, I can't change the markup because I don't have access to the template, I can only change CSS/JS. 另外,我无法更改标记,因为我无权访问模板,只能更改CSS / JS。 I think I'm missing something simple in my script so that Link 1 matches with Output 1, Link 2 with Output 2, etc. but I can't figure it out. 我想我的脚本中缺少一些简单的东西,因此链接1与输出1匹配,链接2与输出2匹配,依此类推,但我无法弄清楚。 Sort of new to scripting. 脚本编写的新功能。 Thanks. 谢谢。

Also do I need to do anything to make sure that each Link and corresponding Output line up with each other? 另外,我是否需要做任何事情以确保每个链接和相应的输出彼此对齐? (Output underneath the link on hover) I'm trying to make it like a drop down menu. (悬停链接下方的输出)我试图使其像一个下拉菜单。

It is because $('.output li').css('visibility', 'visible') will select all the li elements inside .output and make them visible. 这是因为$('.output li').css('visibility', 'visible')将选择.output所有li元素并使它们可见。 You have to basically make only the corresponding li visible and hide all others. 您必须基本上只使相应的li可见并隐藏所有其他li。 Check the code i have fixed it and also added the comments for your understanding. 检查我已修复的代码,并添加注释以供您理解。

$(function(){
    $(".nav li").hover(function(){
        $(this).addClass("hover");
        $('.output li')
        .css('visibility', 'hidden')//Hide all the li's
        .eq($(this).index())//Get the li at same index which triggered hover
        .css('visibility', 'visible');//Make it visible
    }, function(){
        $(this).removeClass("hover");
        $('.output li').css('visibility', 'hidden');
    });

});

.index() method returns an integer indicating the position of the first element within the jQuery object relative to its sibling elements. .index()方法返回一个整数,该整数指示jQuery对象中第一个元素相对于其同级元素的位置。

.eq(index) reduces the set of matched elements to the one at the specified index. .eq(index)将匹配元素的集合减少到指定索引处的元素。

Working Demo 工作演示

If you can't change the markup at all you'll need to make use of the .index() method to find out the index of the hovered element and then relate that to the index of the element you want to show. 如果您根本无法更改标记,则需要使用.index()方法来查找悬停元素的索引,然后将其与要显示的元素的索引关联。

Note that if you're trying to make a drop-down menu effect you don't want to hide the output part when you move the mouse away from the main menu item or you won't ever be able to click on the sub menu. 请注意,如果您要创建下拉菜单效果,则不想在将鼠标移离主菜单项时隐藏输出部分,否则将无法单击子菜单。

With that in mind this is virtually the same requirement as that in a question I answered a few days ago . 考虑到这一点,这实际上与我几天前回答的问题中的要求相同。 Here is a version of the code I posted for that question, which keeps the sub-menu in view for a moment after you stop hovering over the main menu, so that you have time to move the mouse over the sub-menu before it disappears: 这是我为该问题发布的代码的一个版本,在您停止将鼠标悬停在主菜单上之后,可以使子菜单停留一会儿,以便您有时间将鼠标移到子菜单上,然后消失:

var timerId,
    $mainMenuItems = $(".nav li"),
    $subMenus = $(".output li");

$mainMenuItems.hover(
    function(){
        clearTimeout(timerId);
        $subMenus.hide();
        $($subMenus[$mainMenuItems.index(this)]).hide()
                                                .removeClass('hidden')
                                                .show();
    }, function(){
        var i = $mainMenuItems.index(this);
        timerId = setTimeout(function(){$($subMenus[i]).hide();},500);
    }
);
$subMenus.hover(
    function() {
       clearTimeout(timerId);
    },
    function() {
       $(this).hide();
    }
);

Working demo: http://jsfiddle.net/4mgXh/ 工作演示: http : //jsfiddle.net/4mgXh/

Rather than explicitly setting the visibility property I'm just using .hide() and .show() . 而不是明确地设置visibility财产我只是用.hide().show()

(I realise this goes beyond what you asked, but I figure it will solve your next problem and also I can't be bothered pulling the timeout stuff out of the code. For a more detailed explanation of how the code works look at my other answer ) (我意识到这超出了您的要求,但我认为它可以解决您的下一个问题,而且我也不必费心将超时内容从代码中取出。有关代码如何工作的更详细说明,请参见我的其他代码。 回答

One problem I've noticed with the other approaches is that they result in the output li being displayed as they appear within the list. 我注意到其他方法的一个问题是,它们导致输出li在列表中出现时被显示。 I find it unlikely that this would be the desired behavior of the code. 我发现这不太可能是代码的预期行为。

So to get the hover working, you would style ".output li": 因此,要使悬停工作,您可以将样式设置为“ .output li”:

.output li {display:none;}

And script using show() and hide() instead of changing the visibility style: 并使用show()和hide()而不是更改可见性样式的脚本:

$(function(){
    $('.nav li').hover(function(){
        $(this).addClass('hover');
        var linkId = $(this).index();
        $($('.output li')[linkId]).show();
    }, function () {
        $(this).removeClass('hover');
        var linkId = $(this).index();
        $($('.output li')[linkId]).hide();
    });
});

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

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