我使用 next() 函数显示一系列元素。不过,一旦到达终点,我想转到第一个元素。有任何想法吗?
这是代码:
//Prev / Next Click
$('.nextSingle').click( function() {
//Get the height of the next element
var thisHeight = $(this).parent().parent().parent().next('.newsSingle').attr('rel');
//Hide the current element
$(this).parent().parent().parent()
.animate({
paddingBottom:'0px',
top:'48px',
height: '491px'
}, 300)
//Get the next element and slide it in
.next('.newsSingle')
.animate({
top:'539px',
height: thisHeight,
paddingBottom:'100px'
}, 300);
});
基本上我需要一个“if”语句,表示“如果没有剩余的‘下一个’元素,则找到第一个元素。
Thanks!
确定.next()
通过提前检查其length
财产。
$('.nextSingle').click( function() {
// Cache the ancestor
var $ancestor = $(this).parent().parent().parent();
// Get the next .newsSingle
var $next = $ancestor.next('.newsSingle');
// If there wasn't a next one, go back to the first.
if( $next.length == 0 ) {
$next = $ancestor.prevAll('.newsSingle').last();;
}
//Get the height of the next element
var thisHeight = $next.attr('rel');
//Hide the current element
$ancestor.animate({
paddingBottom:'0px',
top:'48px',
height: '491px'
}, 300);
//Get the next element and slide it in
$next.animate({
top:'539px',
height: thisHeight,
paddingBottom:'100px'
}, 300);
});
顺便说一下,你可以替换.parent().parent().parent()
with .closest('.newsSingle')
(如果您的标记允许)。
EDIT:我更正了thisHeight
使用$next
我们引用的元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)