[英]jQuery: next() and prev() with a wrapper?
I have the following html: 我有以下html:
<article class="layer" id="one"> </article>
<article class="layer" id="two"> </article>
<article class="layer" id="three"> </article>
<section class="cat" id="something">
<article class="layer" id="four"> </article>
<article class="layer" id="five"> </article>
</section>
<article class="layer" id="six"> </article>
…
I want to navigate with my keyboard through all articles. 我想用键盘浏览所有文章。 Like so … 像这样……
var view,
next,
prev;
$(document).keydown(function(e) {
view = $('.layer:in-viewport');
next = view.next().attr('id');
prev = view.prev().attr('id');
switch (e.keyCode) {
case 38: // Up
if ( prev != undefined )
scroll($("#"+prev), true, 0);
break;
case 40: // Down
if ( next != undefined )
scroll($("#"+next), true, 0);
break;
}
});
This would work fine if all articles would be in the same container. 如果所有文章都放在同一个容器中,则可以正常工作。 However as you can see above I have a also sections that wrap those articles. 但是,正如您在上面看到的那样,我还有一些包装这些文章的部分。 I simply want to make it work as if there were no such sections and article#three
jumps straight to article#four
我只是想让它像没有这样的部分一样工作,而article#three
article#four
直接跳到article#four
Any idea how I could make that work? 知道我该怎么做吗?
edit: 编辑:
The thing causing the bug in Firefox … 导致Firefox错误的原因...
if ( top.location.hash ) {
hash = top.location.hash.substring(3);
catHash = hash.split('/')[1];
if ( catHash !== undefined )
scroll($("#"+catHash), false, 0);
else
scroll($("#"+hash), false, 0);
if ( hash != "index" && !isiPhone() )
$('#top').slideDown();
}
Where only those 4 lines cause the bug … 只有这四行会导致错误……
if ( catHash !== undefined )
scroll($("#"+catHash), false, 0);
else
scroll($("#"+hash), false, 0);
This few lines only check on page-load if a hash is existing in the current top.location. 这几行仅在页面加载时检查当前top.location中是否存在哈希。 So if the url is looking like this www.something.com/#!/category/idOfElement
I want to jump to that location on the page. 因此,如果该网址看起来像www.something.com/#!/category/idOfElement
我想跳到页面上的该位置。
Why could those 4 lines cause this bug only in firefox? 为什么这4行仅在firefox中导致此错误?
The easiest way to select the "real" next <article>
elements is by using the .index
and .eq
methods. 选择“真实的”下一个<article>
元素的最简单方法是使用.index
和.eq
方法。
var allArticles = $("article.layer");
var index = allArticles.index(view); // Zero-based indexes. Not found = -1
var prev = index <= 0 ? $() : allArticles.eq(index-1);
var next = allArticles.eq(index + 1);
Get a collection of all the articles, then on keypress find out which article is the current one and calculate next/prev based on their relative position(index) from the current article. 获取所有文章的集合,然后在按键上找出哪个文章是当前文章,并根据它们在当前文章中的相对位置(索引)计算下一个/上一个。 You'll need to be careful in handling the first/last article. 在处理第一篇/最后一篇文章时,您需要格外小心。
var $articles = $('.article');
$(document).keydown(function(e) {
var view = $('.layer:in-viewport'),
idx = $articles.index(view),
prev = $articles.eq(Math.max(0,idx-1)),
next = $articles.eq(Math.min(idx+1,$articles.length-1));
switch (e.keyCode) {
case 38: // Up
if ( prev != undefined )
scroll($("#"+prev), true, 0);
break;
case 40: // Down
if ( next != undefined )
scroll($("#"+next), true, 0);
break;
}
});
You could also implement wrapping at the ends if you wanted. 如果需要,也可以在末尾实现环绕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.