简体   繁体   English

用于旋转横幅的同一HTML页面上的多个脚本

[英]Multiple Scripts on same HTML page for rotating banner

Trying to make a kind of auto rotating banner for content in a footer. 尝试为页脚中的内容制作一种自动旋转横幅。 The issue is that whilst they update the slide, the first two stop after changing once, and the third updates faster than it should be doing. 问题在于,当他们更新幻灯片时,前两个在更改一次后停止,第三个更新比应该更快。 Each of the parts have unique class names, and I can't seem to find the problem. 每个部分都有唯一的类名,我似乎无法找到问题所在。 I believe the issue is either in the HTML or the JS scripts, however this is really the first time ive used multiple scripts on one page. 我认为问题出在HTML或JS脚本中,但这是我第一次在一个页面上使用多个脚本。 (This is for a project) HTML (这是一个项目)HTML

<footer>
      <div class = "grid-container" id = "footer-grid">
        <div class = "grid-100 mobile-grid-100 grid-parent" id = "slideshow">
          <div class = "Slide1 grid-33 mobile-grid-33">
            <h1>Slide 1</h1>
          </div>
          <div class = "Slide1 grid-33 mobile-grid-33">
            <h1>Slide 2</h1>
          </div>
          <div class = "Slide1 grid-33 mobile-grid-33">
            <h1>Slide 3</h1>
          </div>
          <div class = "Slide1 grid-33 mobile-grid-33">
            <h1>Slide 4</h1>
          </div>
          <div class = "Slide1 grid-33 mobile-grid-33">
            <h1>Slide 5</h1>
          </div>

          <div class = "Slide grid-33 mobile-grid-33">
            <h1>Slide 1</h1>
          </div>
          <div class = "Slide grid-33 mobile-grid-33">
            <h1>Slide 2</h1>
          </div>
          <div class = "Slide grid-33 mobile-grid-33">
            <h1>Slide 3</h1>
          </div>
          <div class = "Slide grid-33 mobile-grid-33">
            <h1>Slide 4</h1>
          </div>
          <div class = "Slide grid-33 mobile-grid-33">
            <h1>Slide 5</h1>
          </div>

          <div class = "mySlides3 grid-33 mobile-grid-33">
            <h1>Slide 1</h1>
          </div>
          <div class = "mySlides3 grid-33 mobile-grid-33">
            <h1>Slide 2</h1>
          </div>
          <div class = "mySlides3 grid-33 mobile-grid-33">
            <h1>Slide 3</h1>
          </div>
          <div class = "mySlides3 grid-33 mobile-grid-33">
            <h1>Slide 4</h1>
          </div>
          <div class = "mySlides3 grid-33 mobile-grid-33">
            <h1>Slide 5</h1>
          </div>
        </div>
        <div class = "clear"></div>
        <div class = "clear"></div>
        <div class = "grid-100" id = "footer-info">
          <p>some stuff might go here</p>
        </div>
      </div>
    </footer>

    <!-- SCRIPTS -->
    <script>
     var slideIndex = 0;
    carousel();

    function carousel() {
      var i;
      var x = document.getElementsByClassName("Slide1");
      for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
      }
      slideIndex++;
      if (slideIndex > x.length) {slideIndex = 1}
      x[slideIndex-1].style.display = "block";
      setTimeout(carousel, 1000); // Change image every 2 seconds
    }
    </script>
    <script>
     var slideIndex = 0;
    carousel();

    function carousel() {
      var i;
      var x = document.getElementsByClassName("Slide");
      for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
      }
      slideIndex++;
      if (slideIndex > x.length) {slideIndex = 1}
      x[slideIndex-1].style.display = "block";
      setTimeout(carousel, 1000); // Change image every 2 seconds
    }
    </script>
    <script>
     var slideIndex = 0;
    carousel();

    function carousel() {
      var i;
      var x = document.getElementsByClassName("mySlides3");
      for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
      }
      slideIndex++;
      if (slideIndex > x.length) {slideIndex = 1}
      x[slideIndex-1].style.display = "block";
      setTimeout(carousel, 1000); // Change image every 2 seconds
    }
    </script>

CSS CSS

Slide1, .Slide, .mySlides3 {
    display: none;
    border: 1px solid black;
    text-align: center;
    padding-top:10px;
    margin-bottom:10px;
    }

Any help is appreciated, this is the only real issue i've had using javascript at this point. 任何帮助表示赞赏,这是我此时使用javascript的唯一真正问题。 I can't seem to find why this is occurring, I have tried changing class names to be more distinct. 我似乎无法找到为什么会发生这种情况,我尝试将类名更改为更加清晰。

When you create a new function in global scope you are basically doing the following: 在全局范围内创建新函数时,基本上执行以下操作:

window.carousel = function () { ... }

You have 3 scripts parts. 你有3个脚本部分。 When first is executed, it rotates the first element. 首次执行时,它会旋转第一个元素。 But after that, it's overwritten by second script block. 但在那之后,它被第二个脚本块覆盖了。 So your code is something like this currently: 所以你的代码目前是这样的:

window.carousel = function () { /* logic for elm 1 */ }
window.carousel()
window.carousel = function () { /* logic for elm 2 */ }
window.carousel()
window.carousel = function () { /* logic for elm 3 */ }
window.carousel()

In short, 3 script tags share one scope, hence your problem. 简而言之,3个脚本标签共享一个范围,因此您的问题。 Solution might be creating a carousel function which will take id as an argument. 解决方案可能是创建一个轮播函数,它将id作为参数。 And this is better coding practice anyways 无论如何,这是更好的编码实践

You can consolidate your javascript right now and make this much easier on yourself. 您现在可以合并您的JavaScript,让自己更轻松。 You should only need 1 carousel function for all of your calls. 您只需要1个轮播功能即可完成所有通话。 The only things that changes between each carousel function call is the class name. 每个轮播函数调用之间唯一的变化是类名。 So add that as a parameter for your carousel function. 因此,将其添加为轮播功能的参数。

    <!-- SCRIPTS -->
    <script>
        var slideIndex = 0;
        carousel("Slide1"); 
        carousel("Slide");  
        carousel("mySlides3");

        function carousel(slideName) {
            var i;
            var x = document.getElementsByClassName(slideName);
            for (i = 0; i < x.length; i++) {
                x[i].style.display = "none";
            }
            slideIndex++;
            if (slideIndex > x.length) {
                slideIndex = 1
            }
            x[slideIndex - 1].style.display = "block";
            setTimeout(function() { carousel(slideName) }, 1000); // Change image every 2 seconds 
        }
    </script>

PS... You are missing a "." PS ...你错过了一个“。” on Slide1 in your css as well. 在你的CSS中的Slide1上。

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

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