简体   繁体   English

使用jQuery显示/隐藏列表项

[英]Using jQuery to show/hide list items

All of this is code is here: http://jsfiddle.net/yrgK8/ 所有这些都是代码在这里: http//jsfiddle.net/yrgK8/

I have a "news" section which is composed of the following: 我有一个“新闻”部分,其中包含以下内容:

<ul id="news">

      <li><p>asfdsadfdsafdsafdsafdsafdsafdsafdsa</p></li>
      <li><p><a href="http://google.com">google.com link</a</p></li>                   
 </ul>                          
                <div id="newsNav">
                    <span id="newsRight"><a href="#"><img src="http://engineercreativity.com/samples/einav/images/newsleft.png" alt="&gt;" /></a></span>
                    <span id="newsLeft"><a href="#"><img src="http://engineercreativity.com/samples/einav/images/newsright.png" alt="&lt;" /></a></span>
                </div><!-- /#newsNav -->

And I have the following CSS code (simple, just pretty much overlay the li's on top of each other with absolute positioning): 我有以下CSS代码(简单,只是将绝对定位叠加在彼此之上):

ul#news { list-style-type: none; position:relative; height:101px; color: black; }
ul#news > li { font-size: 11px; margin: 10px; margin-right: 15px; position: absolute; top: 0; right:0; opacity: 0; filter:alpha(opacity=0);  color: black; }

And finally, this is the jQuery code that makes it all happen. 最后,这是使这一切成为现实的jQuery代码。 Basically what it does is it animates the opacity of one LI to 0, and then animates the opacity of the next LI to 1. 基本上它的作用是将一个LI的不透明度设置为0,然后将下一个LI的不透明度设置为1。

$('ul#news li:first').addClass('active').addClass('firstNews');

$('ul#news > li').css({opacity:0.0}).filter('ul#news  li.firstNews').css({opacity:1.0});
$('#newsLeft').click(function() {

    var $active = $('ul#news li.active');
    var $next = $active.next().length ? $active.next() : $('ul#news li.firstNews');

        $active.animate({opacity:0.0}, 300, function() {
            //when done animating out, animate next in
            $next.css({opacity:0.0})
                .animate({opacity: 1.0}, 400, function() {
                $active.removeClass('active');
                $next.addClass('active');
        });
    });

    return false; 
}); //clickRight

$('#newsRight').click(function() {

    var $active = $('ul#news li.active');
    var $previous = $active.prev().length ? $active.prev() : $('ul#news li.firstNews');

        $active.animate({opacity:0.0}, 300, function() {
            //when done animating out, animate next in
            $previous.css({opacity:0.0})
                .animate({opacity: 1.0}, 400, function() {
                $active.removeClass('active');
                $previous.addClass('active');
        });
    });

    return false; 
}); //clickRight

So what's the problem? 所以有什么问题? The problem is that they're stacked on top of each other. 问题在于它们彼此堆叠在一起。 This works if the LIs only contain text. 如果LI仅包含文本,则此方法有效。 However, some of them contain links. 但是,其中一些包含链接。 This becomes a problem when there's an LI with opacity of 0 that's stacked on top of your LI with opacity of 1 and which contains a link. 如果LI的不透明度为0且堆叠在LI的顶部,且不透明度为1且包含链接,则会出现问题。 You can't click the link. 您无法单击该链接。

Anyone know how I can fix this? 有谁知道如何解决这个问题?

Thanks a lot, 非常感谢,

Amit 阿米特

将活动项的z-index设置为某个高值,因此它将高于它们所有,并且链接不应再是问题。

I would think setting display:none at the end of the transition would fix your problem. 我认为设置display:none转换结束时display:none可以解决您的问题。

However, it seems like it would be better to use jQuery's built-in .fadeOut() , since this would handle the opacity as well as setting the display to none . 但是,使用jQuery的内置.fadeOut()似乎会更好,因为这会处理不透明度以及将display设置为none

If you don't want to use that, you should set display to none yourself using css. 如果您不想使用它,则应使用css将display设置为none

尝试在设置不透明度的同时设置li元素的z-index。

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

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