javascript - 试图让我的照片幻灯片不断循环播放所有照片

标签 javascript html css slideshow transition

我的网站在标题下方的其中一个部分中有照片幻灯片。当幻灯片播放到第三张照片时停止。我想知道如何将第三张照片转换回第一张照片,然后在所有照片中使用相同的动画继续循环?动画是下一张照片从当前照片的右侧滑入。我的代码如下。

HTML
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href= "Logo.png" type="img/SVG" style="width: 100%; height: 100%">
    <style>
        body {
            margin: 0;
        }

        .Header {
            position: fixed;
            z-index:1;
            width: 100%;
            height: 70px;
            background-color: black;
            text-align: right;
        }

        .socialmedia {
            position: fixed;
            right: 100px;
            top: 35px;
            transform: translate(0, -50%);
            display: flex;
            /* add this */
            align-items: center;
            /* add this */
        }

        .preorder button {
            background-color: white;
            border: 0;
            height: 35px;
            width: 110px;
            margin-left: 35px;
        }

        .footer {
            display: flex;
            align-items: center;
            width: 100%;
            height: 90px;
            background-color: black;
        }

        .photoSection {
            position: relative;
            background-color: black;
            overflow: hidden;
        }

        .mySlides~.mySlides {
            position: absolute;
            top: 0;
            left: 100%;
            transition: 0.8s;
        }
    </style>
</head>

<body>

<div class="Header" id="myHeader">
    <a class = "headerLogo">
        <a href="file:///C:/Noah's%20stuff/Home.html" ><h1 style="color:white; font-family: Verdana; font-style: italic; font-size: x-large;
        text-align: center;">Some Title</h1></a>
        <style>
            a{text-decoration: none}
        </style>

    </a>
    <div class="socialmedia">
        <a class = "Facebook">
            <a href="https://www.facebook.com/" target="_blank"><img src = "https://images.seeklogo.net/2016/09/facebook-icon-preview-1.png" width="50px" height="50px"></a>
        </a>
        <a class = "Instagram">
            <a href="https://www.instagram.com/"><img src = "https://images.seeklogo.net/2016/06/Instagram-logo.png"  width="50px" height="50px"></a>
        </a>
        <a class = "Youtube">
            <a href="https://www.youtube.com/channel/" target="_blank"><img src = "https://images.seeklogo.net/2016/06/YouTube-icon.png" width="50px" height="50px"></a>
        </a>
        <a class = preorder>
            <button style = "background-color: white;">Pre-Order</button>

        </a>
    </div>
</div>
    <div class="photoSection">
        <img class="mySlides" src="http://coolwildlife.com/wp-content/uploads/galleries/post-3004/Fox%20Picture%20003.jpg" style="width:100%; height:785px;">
        <img class="mySlides" src="http://ichef-1.bbci.co.uk/news/976/media/images/83351000/jpg/_83351965_explorer273lincolnshirewoldssouthpicturebynicholassilkstone.jpg" style="width:100%; height: 785px">
        <img class="mySlides" src="https://c1.staticflickr.com/2/1520/24330829813_944c817720_b.jpg" style="width:100%; height: 785px">
    </div>

<div class="section1" style = "background-color: black; color: white; padding-top: 150px;" >
    <a class = "header1" style="padding-left: 200px; display:inline-block; width:85.2%">
        <img src = "FoldingIcon.PNG">
        <h1 style = "font-family: Verdana;font-size: x-large; text-align: start;">Some Title</h1>
        <p style = "margin-bottom: 200px; font-family: Verdana; color: gray;">
        </p>
        <img src="Backpack.PNG">
        <h2 style = "font-family: Verdana; font-size: x-large; text-align:start">Some Title</h2>
        <p style = "margin-bottom: 300px"></p>
    </a>
    <p style = "color: gray; background-color: black; color: white; padding-left: 50px; display:inline-block; width:85.2%">
        ____________________________________________________________________________________________________________________________________________________________</p>
</div>

<div class = "section2" style = "background-color: black; color: white; padding-bottom: 500px;">

</div>
<script>
    var myIndex = 0;
    carousel();

    function carousel() {
        var i;
        var x = document.getElementsByClassName("mySlides");
        myIndex++;
        if (myIndex > x.length) {
            myIndex = 1
            x[myIndex - 1].style.right;
        }
        else {
            x[myIndex - 1].style.left = "0";
        }

        // you may want to add transition-delay , z-index, ... to tune sliding effect
        setTimeout(carousel, 4000); // Change image every 2 seconds
    }
</script>

最佳答案

  • 添加一个名为 .no_trans 的 css 类,它将禁用转换。该类将具有以下规则

    .no_trans{
      transition:none !important;
        -webkit-transition:none !important;
        -moz-transition:none !important;
        -ms-transition:none !important;
        -o-transition:none !important;
    }
    

  • 当计数器大于号时。您禁用的图像删除过渡类并反转图像的位置(这将很快)
  • 然后计数器以编号开始。小于号自您将其重置为 0 后的元素数量
  • 进程重启

    .no_trans {
      transition: none !important;
      -webkit-transition: none !important;
      -moz-transition: none !important;
      -ms-transition: none !important;
      -o-transition: none !important;
    }
    HTML
    <!DOCTYPE html>
    
    <head>
      <meta charset="UTF-8">
      <title></title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" href="Logo.png" type="img/SVG" style="width: 100%; height: 100%">
      <style>
        body {
          margin: 0;
        }
        
        .Header {
          position: fixed;
          z-index: 1;
          width: 100%;
          height: 70px;
          background-color: black;
          text-align: right;
        }
        
        .socialmedia {
          position: fixed;
          right: 100px;
          top: 35px;
          transform: translate(0, -50%);
          display: flex;
          /* add this */
          align-items: center;
          /* add this */
        }
        
        .preorder button {
          background-color: white;
          border: 0;
          height: 35px;
          width: 110px;
          margin-left: 35px;
        }
        
        .footer {
          display: flex;
          align-items: center;
          width: 100%;
          height: 90px;
          background-color: black;
        }
        
        .photoSection {
          position: relative;
          background-color: black;
          overflow: hidden;
        }
        
        .mySlides~.mySlides {
          position: absolute;
          top: 0;
          left: 100%;
          transition: 0.8s;
        }
      </style>
    </head>
    
    <body>
    
      <div class="Header" id="myHeader">
        <a class="headerLogo">
          <a href="file:///C:/Noah's%20stuff/Home.html">
            <h1 style="color:white; font-family: Verdana; font-style: italic; font-size: x-large;
            text-align: center;">Some Title</h1>
          </a>
          <style>
            a {
              text-decoration: none
            }
          </style>
    
        </a>
        <div class="socialmedia">
          <a class="Facebook">
            <a href="https://www.facebook.com/" target="_blank"><img src="https://images.seeklogo.net/2016/09/facebook-icon-preview-1.png" width="50px" height="50px"></a>
          </a>
          <a class="Instagram">
            <a href="https://www.instagram.com/"><img src="https://images.seeklogo.net/2016/06/Instagram-logo.png" width="50px" height="50px"></a>
          </a>
          <a class="Youtube">
            <a href="https://www.youtube.com/channel/" target="_blank"><img src="https://images.seeklogo.net/2016/06/YouTube-icon.png" width="50px" height="50px"></a>
          </a>
          <a class=p reorder>
            <button style="background-color: white;">Pre-Order</button>
    
          </a>
        </div>
      </div>
      <div class="photoSection">
        <img class="mySlides" src="http://coolwildlife.com/wp-content/uploads/galleries/post-3004/Fox%20Picture%20003.jpg" style="width:100%; height:785px;">
        <img class="mySlides" src="http://ichef-1.bbci.co.uk/news/976/media/images/83351000/jpg/_83351965_explorer273lincolnshirewoldssouthpicturebynicholassilkstone.jpg" style="width:100%; height: 785px">
        <img class="mySlides" src="https://c1.staticflickr.com/2/1520/24330829813_944c817720_b.jpg" style="width:100%; height: 785px">
      </div>
    
      <div class="section1" style="background-color: black; color: white; padding-top: 150px;">
        <a class="header1" style="padding-left: 200px; display:inline-block; width:85.2%">
          <img src="FoldingIcon.PNG">
          <h1 style="font-family: Verdana;font-size: x-large; text-align: start;">Some Title</h1>
          <p style="margin-bottom: 200px; font-family: Verdana; color: gray;">
          </p>
          <img src="Backpack.PNG">
          <h2 style="font-family: Verdana; font-size: x-large; text-align:start">Some Title</h2>
          <p style="margin-bottom: 300px"></p>
        </a>
        <p style="color: gray; background-color: black; color: white; padding-left: 50px; display:inline-block; width:85.2%">
          ____________________________________________________________________________________________________________________________________________________________</p>
      </div>
    
      <div class="section2" style="background-color: black; color: white; padding-bottom: 500px;">
    
      </div>
      <script>
        var myIndex = 0;
        carousel();
    
        function carousel() {
          var i;
          var x = document.getElementsByClassName("mySlides");
          myIndex++;
          //console.log(myIndex);
          if (myIndex > x.length) {
            for (var y = 0; y < x.length; ++y) {
              x[y].classList.add("no_trans");
              x[y].style.left = "100%";
              myIndex = 0;
            }
          } else {
            x[myIndex - 1].classList.remove("no_trans");
            x[myIndex - 1].style.left = "0";
          }
    
          // you may want to add transition-delay , z-index, ... to tune sliding effect
          setTimeout(carousel, 4000); // Change image every 2 seconds
        }
      </script>

  • 关于javascript - 试图让我的照片幻灯片不断循环播放所有照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43009705/

    相关文章:

    javascript - 如何使用 onclick 函数删除数组中的项目?

    html - 没有固定高度的水平滚动 div,所有包含的 div 最高 child 的高度

    javascript - 移动文本框动画

    javascript - 斐波那契数列 - 计算位数 - JavaScript

    javascript - 从列表中选择项目并使用 Javascript AngularJS 将其发送回服务器

    html - haml 文件中的 yaml 前端内容

    javascript - 编辑值时输入失去焦点。使用 ngFor 和 ngModel。 Angular 5

    forms - angular dart 形式的 ng-valid 和 ng-invalid 类不再暴露

    html - Rails active_link_to img 按钮 CSS 显示文件名而不是 img

    javascript - react + typescript : Property * is missing in type *