简体   繁体   English

jQuery在计时器上切换div

[英]Jquery switch divs on timer

I'm trying to make a header that switches on a timer but its just not coming to me. 我正在尝试制作一个可以打开计时器的标头,但是它没有出现在我身上。

Here are the headers. 这是标题。

HTML: HTML:

<!-- Header Section -->
<header id="mainHeader" class="container" style="display:initial">
<div class="row">
    <div class="col-md-12">
        Makato School of Martial Arts
    </div>
</div>
</header>
<!-- End Header Section -->

<!-- Header2 Section -->
<header id="mainHeader2" class="container" style="display:none">
<div class="row">

    <div class="col-md-4">

        <figure class="center">
            <img src="images/icons/Sincerity.jpg">
            <figcaption>Sincerity</figcaption>
        </figure>           

    </div>

    <div class="col-md-4">

        <figure class="center">
            <img src="images/icons/honesty.jpg">
            <figcaption>Honesty</figcaption>
        </figure>           

    </div>

    <div class="col-md-4">

        <figure class="center">
            <img src="images/icons/integrity.jpg">
            <figcaption>Integrity</figcaption>
        </figure>           

    </div>

</div>
</header>
<!-- End Header2 Section -->

I would like the page to load with the first header and then for it to rotate to the second header. 我希望页面加载第一个标头,然后旋转到第二个标头。 If it's not too hard I'd love for the captions to fade in just slightly after the images. 如果不太难的话,我希望字幕在图像之后稍稍消失。

could someone point me in the right direction? 有人可以指出我正确的方向吗?

EDIT: 编辑:

This is as far as I got before I realized I was lost. 这是我意识到自己迷路之前所获得的一切。

    $(document).ready(function(){

    setInterval(function() {
       if($('#mainHeader').css('display') == 'initial'){

            $("#mainHeader").hide();
            $("#mainHeader2").show();

       } else {

        $("#mainHeader2").hide();
        $("#mainHeader").show();

       }
    }, 5 * 1000);
});

I don't find using jquery in some parts of this situation particularly useful, so I'd recommend: 我发现在这种情况下的某些部分使用jquery并不是特别有用,所以我建议:

$(document).ready(function()
{
  setInterval(function() 
  {
    $('#mainHeader-Container').toggleClass('alternate');
  }, 5 * 1000);
});

CSS: CSS:

#mainHeader { display: none; }
#mainHeader2 { display: block; }
.alternate #mainHeader { display: block; }
.alternate #mainHeader2 { display: none; }

Html: HTML:

<div id="mainHeader-Container">
  <div id="mainHeader">
  </div>
  <div id="mainHeader2">
  </div>
</div>

The advantage is that other elements can rely on a parent having or not having alternate class, which is by far easier to detect then which header is visible. 优点是其他元素可以依赖具有或不具有alternate类的父类,这比检测哪个标头可见要容易得多。

Will this jquery script work for you? 这个jQuery脚本会为您工作吗?

$(document).ready(function(){
    setInterval(function() {
        $("#mainHeader").fadeToggle(5 * 1000);
        $("#mainHeader2").fadeToggle(5 * 1000);
       }, 5 * 1000);
});

You'll have to change the display of mainHeader to "display: visible". 您必须将mainHeader的显示更改为“ display:visible”。

Here's a JSFiddle . 这是一个JSFiddle

EDIT: 编辑:

Updated script to wait to for div to hide before showing next. 更新了脚本,等待div隐藏后再显示下一个脚本。

$(document).ready(function(){
        setInterval(function() {
            if ($("#mainHeader").css('display') == 'none')
            {
                $("#mainHeader2").toggle(function() {
                    $("#mainHeader").toggle();
                });
            }
            else
            {
               $("#mainHeader").toggle(function() {
                    $("#mainHeader2").toggle();
                }); 
            }
           }, 5 * 1000);
    });

Updated JSFiddle . 更新了JSFiddle

EDIT 2: 编辑2:

Actually can make the code even easier by doing: 实际上,可以通过执行以下操作使代码更容易:

$(document).ready(function(){
setInterval(function() {
    $("[id^='mainHeader']").toggle();
}, 5 * 1000);

}); });

Updated JSFiddle . 更新了JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM