[英]Fade LI in and out on click
I have three separate paragraphs stored in three list-items with a continue reading image. 我将三个单独的段落存储在三个列表项中,并继续阅读图像。 I want only the first list-item to show. 我只希望显示第一个列表项。 When you click the image, it should fade and show the next list-item in a fade transition. 当您单击图像时,它应淡入淡出并在淡入淡出过渡中显示下一个列表项。 Once you reach the third one, it should fade back to the first one when you click. 到达第三个后,单击时它应退回到第一个。
My issue is that I am getting weird spacing- where the first list-items fades but still takes up space for a moment and causes the next fading in list item to jump up once the prior LI finally fades. 我的问题是,我的间距越来越怪异,第一个列表项消失了,但仍然占据了一会儿空间,一旦先前的LI最终消失,列表项中的下一个衰落就跳了起来。
Opacity works perfectly with the fading but display collapses the fading out li so that list items stack on top of each other with the fading. 不透明度可与淡入淡出完美配合,但显示会使淡入淡出li折叠起来,因此列表项会随着淡入而彼此叠加。
I could position the list items absolute with the UL position relative but I don't like the inherit lack of responsiveness with that method. 我可以将列表项绝对定位在相对于UL的位置,但是我不喜欢这种方法固有的缺乏响应能力。 I'd like to avoid it if I can help it. 如果可以的话,我想避免它。
Code so far- 到目前为止的代码
<ul class="copy-box">
<li class="first active"><p>first</p></li>
<li class="second"><p>second</p></li>
<li class="last"><p>third</p></li>
</ul>
<a class="next-btn"><img src="#"></a>
.active {
opacity:1 !important;
transition: .5s;
display: block !important;
}
.copy-box li {
opacity:0;
list-style: none;
display: none;
}
.copy-box li:first {
display: inline;
}
Jquery jQuery的
`$('.next-btn').click(function(){
if ( $("li.active").hasClass('last') ) {
$('li.active').removeClass('active');
$('.copy-box li').first('li').addClass('active');
return false;
}
else {
$("li.active").removeClass('active').next().addClass('active');
return false;
}
})`
First thing to keep in mind is - display
property is not animatable, so you cannot expect it to work with animation/transition. 要记住的第一件事是- display
属性不是动画,所以你不能指望它与动画/过渡工作。
As you want fade effect, you just need to change opacity from 0 to 1 and vice versa. 如果需要淡入淡出效果,只需将不透明度从0更改为1,反之亦然。
One issue you will now run into is that all paragraphs fade in/out at their respective positions and keep on taking space even though they have faded out. 您现在会遇到的一个问题是,所有段落在各自的位置淡入/淡出,即使淡出也继续占用空间。 You would normally want them to fade in/out all on same position ie overlapping each other. 通常,您希望它们在相同位置上全部淡入/淡出,即彼此重叠。 For this, you need to set their position to absolute and adjust position as required. 为此,您需要将其位置设置为绝对位置并根据需要调整位置。
$('.next-btn').click(function(){ if ( $("li.active").hasClass('last') ) { $('li.active').removeClass('active'); $('.copy-box li').first('li').addClass('active'); return false; } else { $("li.active").removeClass('active').next().addClass('active'); return false; } });
.copy-box { position: relative; height: 50px; } .copy-box li { opacity:0; list-style: none; position: absolute; top: 0; left: 0; transition: opacity 0.3s ease; } .copy-box li.active { opacity:1; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <ul class="copy-box"> <li class="first active"><p>first</p></li> <li class="second"><p>second</p></li> <li class="last"><p>third</p></li> </ul> <a class="next-btn"><img src="#"></a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.