jQuery点击函数和超时
我构建了这个函数来滚动浏览画廊。图库应每 6000 毫秒自行滚动到下一张图像,但如果用户单击缩略图,则应直接转到该缩略图,然后将计时器重置为 6000 毫秒。
我的代码无法正常工作,因为我认为 JQuery 重新触发功能不正确。不太确定我在做什么。
var timerp = null;
$('.thumbs').click(function() {
clearTimeout(timerp);
$('.cornerimgfocus').removeClass('cornerimgfocus');
$('#P' + $(this).attr('id')).addClass('cornerimgfocus');
}, function gallery() {
clearTimeout(timerp);
timerp = setTimeout(function() {
var islide = $('.cornerimgfocus');
$(islide).removeClass('cornerimgfocus');
if(islide[0]==$('.cornerimg:last')[0]) {
var nextslide = $('.cornerimg').first();
}
else {
var nextslide = $(islide).next();
}
gallery();
}, 6000);
});
有什么想法吗?
太棒了
顺便说一句,鼠标点击工作得很好,只是超时我没有得到正确的结果。
我试过这个。还是什么都没有。点击仍然有效,但超时功能不起作用。
$('.thumbs').click(function() {
clearTimeout(timerp);
$('.cornerimgfocus').removeClass('cornerimgfocus');
$('#P' + $(this).attr('id')).addClass('cornerimgfocus');
timerp = setTimeout(function() {
$('#' + $(this).attr('id')).next().click()
}, 3000)
});
我认为如果我添加 .next().click() 它将重新激活此点击功能,但事实并非如此。我们如何实现这一目标。
I built this function to scroll through a gallery. The gallery should scroll to the next image by itself every 6000ms, but if the user clicks on a thumbnail it should go directly to that thumbnail and then reset the timer for 6000ms.
My code is not working because I do not think I got the JQuery retriggereing function right. Not quite sure what I am doing.
var timerp = null;
$('.thumbs').click(function() {
clearTimeout(timerp);
$('.cornerimgfocus').removeClass('cornerimgfocus');
$('#P' + $(this).attr('id')).addClass('cornerimgfocus');
}, function gallery() {
clearTimeout(timerp);
timerp = setTimeout(function() {
var islide = $('.cornerimgfocus');
$(islide).removeClass('cornerimgfocus');
if(islide[0]==$('.cornerimg:last')[0]) {
var nextslide = $('.cornerimg').first();
}
else {
var nextslide = $(islide).next();
}
gallery();
}, 6000);
});
Any ideas?
Marvellous
By the way, the mouseclick works perfectly it is only the timeout that I have not got correct.
I tried this. Still nothing. The click is still working but the timeout function is not.
$('.thumbs').click(function() {
clearTimeout(timerp);
$('.cornerimgfocus').removeClass('cornerimgfocus');
$('#P' + $(this).attr('id')).addClass('cornerimgfocus');
timerp = setTimeout(function() {
$('#' + $(this).attr('id')).next().click()
}, 3000)
});
I thought that if I added the .next().click() it would reactivate this click function but it doesn't. How do we achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将两个函数传递给 click() 方法,该方法不受支持。另外,您似乎想要 setInterval() 而不是 setTimeout(),并且您没有将
cornerimgfocus
类添加到下一张幻灯片时计时器滴答作响。尝试:You're passing two functions to the click() method, which is not supported. Also, it looks like you want setInterval() instead of setTimeout(), and you're not adding the
cornerimgfocus
class to the next slide when the timer ticks. Try:似乎您尝试选择一个元素两次:
在第二行您可以使用:
但不知道这是否导致了您的问题,
Max
Seems like you try to select an element twice :
On the second line you could use :
Don't know if that caused your problem though,
Max