jQuery 旋转画廊中的列表项
这可能已经被回答过,我已经知道它应该如何工作,但由于某种原因它不是。我想这可能就是我循环元素的方式。
$(document).ready(function() {
var element = '#gallery ul#gallery-container';
var idx=0;
var timeout = 3000;
var number = $(element + ' li').length;
function changeSlide() {
$(element + ' li:eq(' + idx + ')').fadeOut();
idx = idx + 1;
if (idx == number) {
idx=0;
}
$(element + ' li:eq(' + idx + ')').fadeIn().delay(timeout).delay(0, function() {
changeSlide();
});;
}
$(element + ' li').hide();
$(element + ' li:first').fadeIn().delay(timeout).delay(0, function() {
changeSlide();
});
});
然后列表是这样的:
<div id="gallery">
<ul id="gallery-container">
<li><img src="media/images/screen-shot-02.jpg" width="173" height="258" alt=" "></li>
<li><img src="media/images/screen-shot-01.jpg" width="173" height="258" alt=" "></li>
</ul>
</div>
我试图让它在延迟后逐个循环元素,以便列表项调用函数并隐藏自身,然后计数器递增,然后显示当前索引。 我怀疑罪魁祸首是这样的,就好像我在调用它的函数中放置了一个警报:
$(element + ' li:eq(' + idx + ')').fadeOut();
This has probably been answered before and I already know how this should work, but for some reason it is not. I think it may be how I am looping through the elements.
$(document).ready(function() {
var element = '#gallery ul#gallery-container';
var idx=0;
var timeout = 3000;
var number = $(element + ' li').length;
function changeSlide() {
$(element + ' li:eq(' + idx + ')').fadeOut();
idx = idx + 1;
if (idx == number) {
idx=0;
}
$(element + ' li:eq(' + idx + ')').fadeIn().delay(timeout).delay(0, function() {
changeSlide();
});;
}
$(element + ' li').hide();
$(element + ' li:first').fadeIn().delay(timeout).delay(0, function() {
changeSlide();
});
});
Then the list is like this:
<div id="gallery">
<ul id="gallery-container">
<li><img src="media/images/screen-shot-02.jpg" width="173" height="258" alt=" "></li>
<li><img src="media/images/screen-shot-01.jpg" width="173" height="258" alt=" "></li>
</ul>
</div>
I was trying to get it to loop through the elements one by one, after a delay so the list item calls the function and hides itself, then the counter is incremented and then the current index is shown.
I suspect the culprit to be this as if I put an alert in the function it is called:
$(element + ' li:eq(' + idx + ')').fadeOut();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要问题是,正如评论所述,
delay
并没有按照您的想法进行操作 - 您应该查看本机setTimeout
函数。除此之外,还有多个地方可以提高效率。看看这个:我们尝试不使用动态生成的选择器调用 jQuery 函数,而是操作包含开始时缓存的所有幻灯片的 jQuery 对象的单个实例。我们还使用
fade
函数提供的回调函数在当前幻灯片淡出后淡入下一张幻灯片。请参阅 http://www.jsfiddle.net/b3Lf5/1/ 进行简单演示
The main problem is, as the comment states,
delay
does not do what you think it does - you should be looking at the nativesetTimeout
function instead. In addition to that, there are multiple places where this could be made more efficient. Have a look at this:We try not to evoke the jQuery function with a dynamically generated selector, but instead manipulate a single instance of a jQuery object containing all the slides cached at the start. We also use the callback function provided by the
fade
functions to fade in the next slide after the current one has faded out.See http://www.jsfiddle.net/b3Lf5/1/ for a simple demo
我会这样做:
或者更好的是,将其包装在插件中:
编辑:编辑插件代码以使其符合良好实践。
I would do it something like this:
Or better still, wrap it in a plugin:
edit: Edited the plugin code to bring it into line with good practice.