[英]Javascript / jQuery - search for a numbered class and jump to nearest number if class not found
I have a list, which represents some hours of a day. 我有一个列表,它代表一天中的某些小时。 Like so:
像这样:
<ol>
<li class="9">Item</li>
<li class="10">Item</li>
<li class="11">Item</li>
<li class="13">Item</li>
<!-- and so on .... -->
</ol>
And I have a "Now" button which gets the current hour and then animates to find that hour in the list, like this: 我有一个“现在”按钮,它获取当前时间,然后进行动画处理以在列表中找到该时间,如下所示:
$('#jump-to-now').click(function () {
// new date object
var date = new Date(),
// give us the hour
theHour = date.getHours();
scrollToHour(theHour);
});
function scrollToHour(hourAsNumber) {
// use the hour number and match it against a day with a class of that number
var theListingItem = listingDays.filter('.active-day').find('li').filter('.' + hourAsNumber),
// get the offset relative to the document
offsetTop = theListingItem.offset().top;
$('html, body').animate({
scrollTop: offsetTop
}, slideAnimationTime);
}
The only problem here is that if there is no list item with a class of that hour, nothing happens. 这里唯一的问题是,如果没有带有该小时课程的列表项,则什么也不会发生。 What I'd like to do, is go to the nearest hour after the current hour.
我想做的是转到当前时间之后最近的一个小时。
For example, it's 7am and the user clicks the button, the code finds that the nearest listing item match after 7am is 9am and it jumps to that list item. 例如,现在是早上7点,用户单击按钮,代码发现早上7点之后最匹配的列表项是9am,然后跳转到该列表项。
How can I do this? 我怎样才能做到这一点?
The logic, roughly (you may need to tweak): 大致的逻辑(您可能需要调整):
function scrollToHour(hourAsNumber) {
var lis = listingDays.filter('.active-day').find('li');
// use the hour number and match it against a day with a class of that number
var theListingItem = lis.filter('.' + hourAsNumber);
// if no item for the specified hour, find nearest
if(theListingItem.length == 0) {
var delta = 1;
while(theListingItem.length == 0 && delta <= lis.length) {
// check next
theListingItem = lis.filter('.' + (hourAsNumber + delta) );
// check previous (uncomment if needed)
/*
if(theListingItem.length == 0) {
theListingItem = lis.filter('.' + (hourAsNumber - delta) );
}
*/
delta++;
}
}
// get the offset relative to the document
var offsetTop = theListingItem.offset().top;
$('html, body').animate({
scrollTop: offsetTop
}, slideAnimationTime);
}
Premising that a class should not start with a number, this is an example jsbin with valid classname : http://jsbin.com/ibeteb/3/edit 假设一个类不应以数字开头,这是一个具有有效类名的示例jsbin: http ://jsbin.com/ibeteb/3/edit
<ol>
<li class="h9">Item</li>
<li class="h10">Item</li>
<li class="h14">Item</li>
<li class="h19">Item</li>
<!-- and so on .... -->
</ol>
var hourAsNumber = 12;
var theListingItem = $('ol li').filter(function(i, el) {
var h = +($(el).attr('class').substring(1));
if (h >= hourAsNumber) return $(el);
})[0];
console.log(theListingItem); // jump to this element (li.h14 in the example)
Note that theListingItem
will be undefined
when there's no item available (eg when in the example hourAsNumber = 20
). 请注意,当没有可用的项目时(例如,在示例
hourAsNumber = 20
), theListingItem
将是undefined
的。
So before checking the offset()
and starting the scroll, be sure that the element exists, eg 因此,在检查
offset()
并开始滚动之前,请确保该元素存在,例如
if (theListingItem.length) {
/* an available hour was found - here you scroll */
}
else {
/* say the user "you arrived too late. try again tomorrow" :)
* or do nothing
*/
}
you should not start a class name with a number. 您不应以数字开头课程名称。 change your class like
改变你的班级
<ol>
<li class="h9">Item</li>
<li class="h10">Item</li>
<li class="h11">Item</li>
<li class="h13">Item</li>
<!-- and so on .... -->
</ol>
Then you can try this 那你可以试试看
function scrollToHour(hourAsNumber) {
var hours = listingDays.filter('.active-day').find('li');
var theListingItem = hours.filter('.h' + hourAsNumber);
while(!theListingItem.get(0))
{
if(hourAsNumber<=24)
theListingItem = hours.filter('.h' + (++hourAsNumber));
}
// get the offset relative to the document
var offsetTop = theListingItem.offset().top;
$('html, body').animate({
scrollTop: offsetTop
}, slideAnimationTime);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.