简体   繁体   English

如何添加时间函数然后单击javascript中的按钮

[英]How can I add a time function then click a button in javascript

I am building a website for my school and I just finished a slideshow.我正在为我的学校建立一个网站,我刚刚完成了一个幻灯片。 To change between pictures on the slideshow a user is required to click the forward or previous button.要在幻灯片上的图片之间切换,用户需要单击前进或上一个按钮。 Is their something that I can add to make it automatically move forward to the next slide every 5 seconds?我可以添加它们以使其每 5 秒自动前进到下一张幻灯片吗?

My code:我的代码:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      * {box-sizing: border-box}
      body {font-family: Verdana, sans-serif; margin:0}
      .mySlides {display: none} 
      img {vertical-align: middle;}

      /* Slideshow container */
      .slideshow-container {
        max-width: 1000px;
        position: relative;
        margin: auto;
      }

      /* Next & previous buttons */
      .prev, .next {
        cursor: pointer;
        position: absolute;
        top: 50%;
        width: auto;
        padding: 16px;
        margin-top: -22px;
        color: white;
        font-weight: bold;
        font-size: 18px;
        transition: 0.6s ease;
        border-radius: 0 3px 3px 0;
        user-select: none;
      }

      /* Position the "next button" to the right */
      .next {
        right: 0;
        border-radius: 3px 0 0 3px;
      }

      /* On hover, add a black background color with a little bit see-through */
      .prev:hover, .next:hover {
        background-color: rgba(0,0,0,0.8);
      }


      /* Number text (1/3 etc) */
      .numbertext {
        color: #f2f2f2;
        font-size: 12px;
        padding: 8px 12px;
        position: absolute;
        top: 0;
      }

      /* The dots/bullets/indicators */
      .dot {
        cursor: pointer;
        height: 15px;
        width: 15px;
        margin: 0 2px;
        background-color: #bbb;
        border-radius: 50%;
        display: inline-block;
        transition: background-color 0.6s ease;
      }

      .active, .dot:hover {
        background-color: #717171;
      }

      /* Fading animation */
      .fade {
        -webkit-animation-name: fade;
        -webkit-animation-duration: 1.5s;
        animation-name: fade;
        animation-duration: 1.5s;
      }

      @-webkit-keyframes fade {
        from {opacity: .4} 
        to {opacity: 1}
      }
    
      @keyframes fade {
        from {opacity: .4} 
        to {opacity: 1}
      }
    
      /* On smaller screens, decrease text size */
      @media only screen and (max-width: 300px) {
        .prev, .next,.text {font-size: 11px}
      }
    </style>
  </head>
  <body>

    <div class="slideshow-container">

    <div class="mySlides fade">
      <div class="numbertext">1 / 3</div>
      <img src="img_nature_wide.jpg" style="width:100%">
    </div>
 
    <div class="mySlides fade">
      <div class="numbertext">2 / 3</div>
      <img src="img_snow_wide.jpg" style="width:100%">
    </div>

    <div class="mySlides fade">
      <div class="numbertext">3 / 3</div>
      <img src="img_mountains_wide.jpg" style="width:100%">
    </div>

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    </div>
    <br>

    <div style="text-align:center">
      <span class="dot" onclick="currentSlide(1)"></span> 
      <span class="dot" onclick="currentSlide(2)"></span> 
      <span class="dot" onclick="currentSlide(3)"></span> 
    </div>

    <script>
      var slideIndex = 1;
      showSlides(slideIndex);

      function plusSlides(n) {
        showSlides(slideIndex += n);
      }

      function currentSlide(n) {
        showSlides(slideIndex = n);
      }

      function showSlides(n) {
        var i;
        var slides = document.getElementsByClassName("mySlides");
        var dots = document.getElementsByClassName("dot");
        if (n > slides.length) {slideIndex = 1}    
        if (n < 1) {slideIndex = slides.length}
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";  
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex-1].style.display = "block";  
        dots[slideIndex-1].className += " active";
      }
    </script>

  </body>
</html> 

I just need to move to the next slide every 5 seconds我只需要每 5 秒移到下一张幻灯片

You can use setInterval to call a particular function every few secondas and in that function you can move slide to next page.您可以使用setInterval每隔几秒调用一次特定函数,在该函数中您可以将幻灯片移至下一页。 Something like:就像是:

function myFunc() {
   if (lastSlide) {
      clearInterval(slideMoveInterval); // <- clean up
   } else {
      goToNextSlide();
   }
}

let slideMoveInterval = setInterval(myFunc, 5000); // <- called every 5 seconds

I have excluded functions but you can replace psuedo code with your functions.我已经排除了函数,但您可以用您的函数替换伪代码。

MDN reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval MDN 参考: https : //developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

You may use the javascript API setInterval () as below.您可以使用 javascript API setInterval () 如下。 This will invoke the call_AnyFunction_Here function every 3 seconds.这将每 3 秒调用一次call_AnyFunction_Here函数。

setInterval(function(){ call_AnyFunction_Here() }, 3000);

Put your slide navigation function in place of this function用你的幻灯片导航功能代替这个功能

you can use setInterval which runs the function specified at given timeinterval .您可以使用setInterval运行在给定timeinterval指定的函数。

 var slideIndex = 1; var timeGap = 5000; // time gap in milliseconds showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; } setInterval(function() { showSlides(slideIndex); slideIndex++ }, timeGap)
 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * { box-sizing: border-box } body { font-family: Verdana, sans-serif; margin: 0 } .mySlides { display: none } img { vertical-align: middle; } /* Slideshow container */ .slideshow-container { max-width: 1000px; position: relative; margin: auto; } /* Next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add a black background color with a little bit see-through */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* Number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* The dots/bullets/indicators */ .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } /* Fading animation */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from { opacity: .4 } to { opacity: 1 } } @keyframes fade { from { opacity: .4 } to { opacity: 1 } } /* On smaller screens, decrease text size */ @media only screen and (max-width: 300px) { .prev, .next, .text { font-size: 11px } } </style> </head> <body> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="img_nature_wide.jpg" style="width:100%"> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="img_snow_wide.jpg" style="width:100%"> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="img_mountains_wide.jpg" style="width:100%"> </div> <a class="prev" onclick="plusSlides(-1)">&#10094;</a> <a class="next" onclick="plusSlides(1)">&#10095;</a> </div> <br> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div> </body> </html>

暂无
暂无

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

相关问题 单击按钮后,如何将图像添加到 Javascript? - How can I add an image to Javascript after a button click? 如何单击按钮并在webdriver中触发javascript函数? - How can I click on a button and trigger a javascript function in webdriver? 如何将一次性点击事件添加到函数中? - How can I add an event for a one time click to a function? 如何将功能添加到按钮上,以便单击和下一步单击,应在javascript中将其禁用 - how can i add functioning to the button for one click and for next click it should be disabled in javascript 如何通过单击按钮调用功能? - How can I call a function at a click of a button? 如何在使用JavaScript构建的按钮上添加点击事件? - How can I add a click event to a button I'm building in javascript? 单击提交按钮后,如何一次绑定功能? 对于jQuery Ajax形式 - how can i bind function one time once i click submit button? for jquery ajax form 如何使 JavaScript 按钮点击功能在时间限制后起作用? - How to enabling JavaScript button click function to work after a time limit? 如何在使用javascript函数单击按钮时将选择列表框中的多个值添加到另一个列表框中? - How do I add multiple values from a select listbox to another listbox on click of a button using javascript function? 如何在60秒后将自动点击功能添加到按钮并使用javascript执行该操作? - How can I add automatic click functionality after 60second to button and perform that operation using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM