jQuery点击函数和超时

发布于 2024-11-17 19:16:29 字数 1355 浏览 4 评论 0原文

我构建了这个函数来滚动浏览画廊。图库应每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-11-24 19:16:29

您将两个函数传递给 click() 方法,该方法不受支持。另外,您似乎想要 setInterval() 而不是 setTimeout(),并且您没有将 cornerimgfocus 类添加到下一张幻灯片时计时器滴答作响。尝试:

var timerp = null;

$('.thumbs').click(function() {
    if (timerp) {
        window.clearInterval(timerp);
    }
    $('.cornerimgfocus').removeClass('cornerimgfocus');
    $('#P' + $(this).attr('id')).addClass('cornerimgfocus');
    gallery();
});

function gallery()
{
    timerp = window.setInterval(function() {
        var islide = $('.cornerimgfocus');
        islide.removeClass('cornerimgfocus');
        var nextSlide;
        if (islide[0] == $('.cornerimg:last')[0]) {
            nextslide = $('.cornerimg').first();
        } else {
            nextslide = islide.next();
        }
        nextSlide.addClass('cornerimgfocus');
    }, 6000);
}

gallery();  // Start the animation.

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:

var timerp = null;

$('.thumbs').click(function() {
    if (timerp) {
        window.clearInterval(timerp);
    }
    $('.cornerimgfocus').removeClass('cornerimgfocus');
    $('#P' + $(this).attr('id')).addClass('cornerimgfocus');
    gallery();
});

function gallery()
{
    timerp = window.setInterval(function() {
        var islide = $('.cornerimgfocus');
        islide.removeClass('cornerimgfocus');
        var nextSlide;
        if (islide[0] == $('.cornerimg:last')[0]) {
            nextslide = $('.cornerimg').first();
        } else {
            nextslide = islide.next();
        }
        nextSlide.addClass('cornerimgfocus');
    }, 6000);
}

gallery();  // Start the animation.
蓝海似她心 2024-11-24 19:16:29

似乎您尝试选择一个元素两次:

var islide = $('.cornerimgfocus');
$(islide).removeClass('cornerimgfocus');

在第二行您可以使用:

islide.removeClass('cornerimgfocus');

但不知道这是否导致了您的问题,

Max

Seems like you try to select an element twice :

var islide = $('.cornerimgfocus');
$(islide).removeClass('cornerimgfocus');

On the second line you could use :

islide.removeClass('cornerimgfocus');

Don't know if that caused your problem though,

Max

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文