如何找出 next() 何时到达末尾,然后转到第一项
我使用 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”语句,表示“如果没有剩余的‘下一个’元素,则找到第一个。
谢谢!
I am displaying a series of elements using the next() function. Once I reach the end though, I want to go to the first element. Any ideas?
Here's the code:
//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);
});
Basically I need an "if" statement that says "if there are no remaining 'next' elements, then find the first one.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过检查
length
属性提前确定.next()
。顺便说一句,您可以将
.parent().parent().parent()
替换为.closest('.newsSingle')
(如果您的标记允许)。编辑:我更正了
thisHeight
以使用我们引用的$next
元素。Determine the
.next()
ahead of time by checking itslength
property.By the way, you could replace
.parent().parent().parent()
with.closest('.newsSingle')
(if your markup allows it).EDIT: I corrected the
thisHeight
to use the$next
element that we referenced.作为有用的参考,以下是您可以编写和包含的函数:
代替:
它将是:
参考: http://www.mattvanandel.com/999/jquery-nextorfirst-function-guarantees-a-selection/
As a useful reference, the following is a function you can write and include:
Instead of:
It would be:
Reference: http://www.mattvanandel.com/999/jquery-nextorfirst-function-guarantees-a-selection/
根据 jquery 文档,一个空的 jquery 对象将返回 .length 0。
所以你需要做的是在调用 .next 时检查返回,然后调用 :first
http://api.jquery.com/next/
according to the jquery documentation, an empty jquery object will return of .length 0.
so what you need to do is check for the return when you call .next, and then call :first
http://api.jquery.com/next/
您可以使用这些函数来查看当前项目是否是第一个/最后一个子项。
You can use these functions to see if the current item is the first/last child.