gpt4 book ai didi

Jquery slider 计时器

转载 作者:行者123 更新时间:2023-12-01 01:15:57 29 4
gpt4 key购买 nike

我需要使用 jquery 为网站创建自定义 slider 。到目前为止,如果我单击按钮,我已经设法让它按照我想要的方式运行,但我想实现一个自动计时器,以便幻灯片在 5 秒后切换。

这是我的 JS 代码:

function MasterSlider() {
//store the current button ID
var current_button = $(this).attr('id');

//reset all the items
$('#slider ul a').removeClass('slider-button-active');
//set current item as active
$(this).addClass('slider-button-active');


//scroll it to the right position
$('.mask').scrollTo($(this).attr('rel'), 850);


//Check which button is pressed and fade the text accordingly
if(current_button == "slider_item1")
{
$('#slider h3').fadeOut().removeClass('caption_active');
$('#slider_caption1').fadeIn().addClass('caption_active');
}
else if(current_button == "slider_item2")
{
$('#slider h3').fadeOut().removeClass('caption_active');
$('#slider_caption2').fadeIn().addClass('caption_active');
}
else if(current_button == "slider_item3")
{
$('#slider h3').fadeOut().removeClass('caption_active');
$('#slider_caption3').fadeIn().addClass('caption_active');
}

//disable click event
return false;
}

//append click event to the UL list anchor tag
$('#slider ul a').click(MasterSlider);

提前致谢!

最佳答案

您可以在 setInterval 中动态触发 a 元素上的 click()!

var intv;
var current = 0; // STARTING SLIDE(<li>element button) INDEX
var slN = $('#slider li').length; // get number of slides(buttons.. all the same)


// your function here with
// clearInterval(intv);
// in the second line.
// In third line put:
// current = $(this).closest('li').index();

// AUTO SLIDE

function auto(){
intv = setInterval(function() {
$('#slider ul li').eq( current++%slN ).find('a').click();
}, 5000 );
}
auto(); // to start immediately auto-slide

// PAUSE ON MOUSEOVER

$('#slider').on('mouseenter mouseleave', function( e ){
var onMouEnt = e.type=='mouseenter' ? clearInterval(intv) : auto() ;
});
  • 感谢 current++%slN,一旦触发最后一个按钮,auto 函数就会循环
  • PAUSE 会检测图库上的 mouseentermouseleave 事件,并使用三元运算符执行以下操作:

$('#slider').on('mouseenter mouseleave', function( e ){ // catch eVENT
var onMouEnt = e.type=='mouseenter' ? // if e IS mouseenter
clearInterval(intv) : // clear autoslide interval
auto() ; // else (is mouseleave) run 'auto' fn
});

关于Jquery slider 计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556122/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com