最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Automatic tab switch in javascript or jquery - Stack Overflow

programmeradmin2浏览0评论

I built a featured tabbing system (4 tabs) with jquery. It works manually but I want to make it loop automatically, i.g. every 5 seconds. I can trigger each tab with the click even but I don't know how to go back to the beginning from 1st tab once it reach the last tab.

The coding too long to past here, so I have made a jsfiddle link to it: /

I just wanna iterate through each tab and click them each, then start from the beginning of the tab again, as in I want an infinite slideshow. But I'm stuck...

I don't want any plugins, I wanna learn how to do this with my own hand, please.

Many thanks

I built a featured tabbing system (4 tabs) with jquery. It works manually but I want to make it loop automatically, i.g. every 5 seconds. I can trigger each tab with the click even but I don't know how to go back to the beginning from 1st tab once it reach the last tab.

The coding too long to past here, so I have made a jsfiddle link to it: http://jsfiddle/ZSPX3/

I just wanna iterate through each tab and click them each, then start from the beginning of the tab again, as in I want an infinite slideshow. But I'm stuck...

I don't want any plugins, I wanna learn how to do this with my own hand, please.

Many thanks

Share asked Apr 2, 2012 at 17:01 ShaozShaoz 10.7k26 gold badges75 silver badges100 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The key part here is to check the index of the currently-on tab and see if there's any tabs beyond it. If not, revert to the first tab. So:

HTML

<ul id='tabs'>
    <li class='on'>tab 1</li>
    <li>tab 2</li>
    <li>tab 3</li>
    <li>tab 4</li>
    <li>tab 5</li>
</ul>

CSS

#tabs { list-style: none; margin: 0; padding: 0; }
#tabs li { float: left; background: #ddd; padding: 6px; }
#tabs li.on { background: #f90; color: #fff; }

JS (jQuery assumed)

$(function() {

    //cache a reference to the tabs
    var tabs = $('#tabs li');

    //on click to tab, turn it on, and turn previously-on tab off
    tabs.click(function() { $(this).addClass('on').siblings('.on').removeClass('on'); });

    //auto-rotate every 5 seconds
    setInterval(function() {

            //get currently-on tab
        var onTab = tabs.filter('.on');

            //click either next tab, if exists, else first one
        var nextTab = onTab.index() < tabs.length-1 ? onTab.next() : tabs.first();
        nextTab.click();
    }, 5000);
});

We pare the (zero-indexed) index of the currently-on tab with the total number of tabs (-1, to account for the zero-indexing). If the former is lower than the latter, there's still some tabs to go; if not, i.e. they're equal, we've reached the end - go back to the start.

Hope this helps.

Here's some code you can use:

jQuery:

setInterval(function() {
    var current = parseInt($('li.selected a').data('links-to').split('_')[1],10);
    var idx=current-1;
    var max = $('.carouselLinks li a').length;
    idx = (current<max) ? (idx+1):0;
    $('a:eq('+idx+')').trigger('click');
}, 3000);

Updated jsFiddle example.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>