具有自动切换功能的jQuery选项卡

jQuery Tabs with auto-switching capability

本文关键字:jQuery 选项 功能      更新时间:2023-09-26

我不太懂javascript,但多亏了google god,我才有了一个标签区。

单击选项卡按钮,显示相应的内容。我希望再增加一个函数,如下所示。

  • 即使没有点击标签按钮,每5秒切换到下一个标签。
  • 如果当前活动选项卡是tab 4,并且5秒过去了,切换回tab 1。

下面是我当前的代码。

/* CSS for Tabs */

.tabs { list-style: none; margin-top: 30px; }
.tabs li { display: inline; }
.tabs li a { min-width: 20%; color: black; float: left; display: block; padding: 1%; margin-left: 1%; margin-right: 1%; position: relative; text-decoration: none; text-align: center; border-top: 1px solid #333; border-bottom: 1px solid #333; }
.tabs li a:hover { font-weight: bold; background: #fff; border-top: 1px solid #333; border-bottom: 1px solid #333; }
.tabs li a:active { font-weight: bold; background: #fff; border-top: 1px solid #333; border-bottom: 1px solid #333; }

/* HTML */

<div id="tabs">
    <ul class="tabs" id="tabsnav">
        <li><a href="#tab-1" class="menu-internal">Tab 1</a></li>
        <li><a href="#tab-2" class="menu-internal">Tab 2</a></li>
        <li><a href="#tab-3" class="menu-internal">Tab 3</a></li>
        <li><a href="#tab-4" class="menu-internal">Tab 4</a></li>
    </ul>
    <div id="tab-1">
        Contents for tab 1
    </div>
    <div id="tab-2">
        Contents for tab 2 
    </div>
    <div id="tab-3">
        Contents for tab 3
    </div>
    <div id="tab-4">
        Contents for tab 4  
    </div>
</div>

/* JAVA */

<script>
jQuery(document).ready(function() {
    jQuery('#tabs > div').hide(); // hide all child divs
    jQuery('#tabs div:first').show(); // show first child div
    jQuery('#tabsnav li:first').addClass('active');
    jQuery('.menu-internal').click(function(){
    jQuery('#tabsnav li').removeClass('active');
    var currentTab = jQuery(this).attr('href');
    jQuery('#tabsnav li a[href="'+currentTab+'"]').parent().addClass('active');
    jQuery('#tabs > div').hide();
    jQuery(currentTab).show();
    return false;
    });
    // Create a bookmarkable tab link
    hash = window.location.hash;
    elements = jQuery('a[href="'+hash+'"]'); // look for tabs that match the hash
    if (elements.length === 0) { // if there aren't any, then
    jQuery("ul.tabs li:first").addClass("active").show(); // show the first tab
    } else { elements.click(); } // else, open the tab in the hash
});
</script> 

我想我需要在上面的脚本中添加更多的代码,例如"在5秒后转到下一个标签,并在标签4后返回标签1"。但是,我不会编程,一窍不通。

请专业人士帮忙。

谢谢。

试试这个

演示

这个差不多就是你想要的。

添加你自己的css

HTML:

 <div id="tabs">
<ul class="tabs" id="tabsnav">
    <li><a href="#tab-1" class="menu-internal">Tab 1</a></li>
    <li><a href="#tab-2" class="menu-internal">Tab 2</a></li>
    <li><a href="#tab-3" class="menu-internal">Tab 3</a></li>
    <li><a href="#tab-4" class="menu-internal">Tab 4</a></li>
</ul>
<div id="tab-1">
    Contents for tab 1
</div>
<div id="tab-2">
    Contents for tab 2 
</div>
<div id="tab-3">
    Contents for tab 3
</div>
<div id="tab-4">
    Contents for tab 4  
</div>
</div>
JAVASCRIPT:

jQuery(document).ready(function() {
jQuery('#tabs > div').hide(); // hide all child divs
jQuery('#tabs div:first').show(); // show first child div
jQuery('#tabsnav li:first').addClass('active');
jQuery('.menu-internal').click(function(){
jQuery('#tabsnav li').removeClass('active');
var currentTab = jQuery(this).attr('href');
jQuery('#tabsnav li a[href="'+currentTab+'"]').parent().addClass('active');
jQuery('#tabs > div').hide();
jQuery(currentTab).show();
return false;
});
// Create a bookmarkable tab link
hash = window.location.hash;
elements = jQuery('a[href="'+hash+'"]'); // look for tabs that match the hash
if (elements.length === 0) { // if there aren't any, then
jQuery("ul.tabs li:first").addClass("active").show(); // show the first tab
} else { elements.click(); } // else, open the tab in the hash
});

希望能有所帮助