e 1 år sedan
förälder
incheckning
ea1997bd66
9 ändrade filer med 163 tillägg och 0 borttagningar
  1. BIN
      img/1.jpg
  2. BIN
      img/2.jpg
  3. BIN
      img/3.jpg
  4. BIN
      img/4.jpg
  5. BIN
      img/5.jpg
  6. BIN
      img/6.jpg
  7. BIN
      img/7.jpg
  8. 67 0
      js/js初阶/DOM/6.轮播.js
  9. 96 0
      js/js初阶/DOM/6.轮播图.html

BIN
img/1.jpg


BIN
img/2.jpg


BIN
img/3.jpg


BIN
img/4.jpg


BIN
img/5.jpg


BIN
img/6.jpg


BIN
img/7.jpg


+ 67 - 0
js/js初阶/DOM/6.轮播.js

@@ -0,0 +1,67 @@
+var imgBox = document.querySelectorAll("img");
+var points = document.querySelectorAll("#list li");
+var prev = document.querySelector(".prev");
+var next = document.querySelector(".next");
+var container = document.getElementById("container");
+var currentIndex = 0; 
+
+// 封装切换方法
+function autoPlay(ind) {
+    // 清空所有样式
+    for(var i=0;i<points.length;i++) {
+        imgBox[i].className = "choose";
+        points[i].className = "";
+    }
+    // 添加点击下标的样式
+    imgBox[ind].className = "choose active";
+    points[ind].className = "selected";
+}
+
+// 点击按钮切换图片
+for(var i=0;i<points.length;i++) {
+    points[i].index = i;
+    points[i].onclick = function() {
+        console.log("点击")
+        currentIndex = this.index;
+        autoPlay(currentIndex);
+    }
+}
+
+// 上一页
+prev.onclick = function() {
+    currentIndex--;
+    if(currentIndex < 0) {
+        currentIndex = imgBox.length - 1;
+    }
+    autoPlay(currentIndex);
+}
+
+// 下一页
+next.onclick = function() {
+    currentIndex++;
+    if(currentIndex == imgBox.length) {
+        currentIndex = 0;
+    }
+    autoPlay(currentIndex);
+}
+
+// 自动轮播
+var timer = setInterval(function() {
+    next.onclick();
+},1000)
+
+// 鼠标划入事件
+container.onmousemove = function() {
+    prev.style.display = 'block';
+    next.style.display = 'block';
+    clearInterval(timer)
+}
+
+// 鼠标划出事件
+container.onmouseout = function() {
+    prev.style.display = 'none';
+    next.style.display = 'none';
+    timer = setInterval(function() {
+        next.onclick();
+    },1000)
+}

+ 96 - 0
js/js初阶/DOM/6.轮播图.html

@@ -0,0 +1,96 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <title>Document</title>
+    <style>
+      * {
+        margin: 0;
+        padding: 0;
+        list-style: none;
+        text-decoration: none;
+        box-sizing: border-box;
+      }
+      #container {
+        width: 590px;
+        height: 470px;
+        position: relative;
+        margin: 100px auto;
+      }
+      .choose {
+        display: none;
+      }
+      .active {
+        display: block;
+      }
+      #list {
+        overflow: hidden;
+        position: absolute;
+        right: 10px;
+        bottom: 10px;
+      }
+      #list li {
+        width: 25px;
+        height: 25px;
+        color: #fff;
+        background: #00f;
+        border-radius: 50%;
+        margin-left: 10px;
+        text-align: center;
+        line-height: 25px;
+        font-size: 12px;
+        float: left;
+      }
+      .selected {
+        color: #ff0 !important;
+        background: #f00 !important;
+      }
+      .prev,.next {
+        width: 50px;
+        height: 40px;
+        text-align: center;
+        line-height: 40px;
+        background: plum;
+        color: #fff;
+        display: none;
+      }
+      .prev {
+        position: absolute;
+        left: 0;
+        top:200px
+      }
+      .next {
+        position: absolute;
+        right: 0;
+        top:200px
+      }
+    </style>
+  </head>
+  <body>
+    <div id="container">
+      <div id="imgBox">
+        <img class="choose active" src="../../../img/1.jpg" alt="" />
+        <img class="choose" src="../../../img/2.jpg" alt="" />
+        <img class="choose" src="../../../img/3.jpg" alt="" />
+        <img class="choose" src="../../../img/4.jpg" alt="" />
+        <img class="choose" src="../../../img/5.jpg" alt="" />
+      </div>
+      <ul id="list">
+        <li class="selected">1</li>
+        <li>2</li>
+        <li>3</li>
+        <li>4</li>
+        <li>5</li>
+      </ul>
+      <!-- &nbsp; -->
+      <div class="prev">
+        <span>&lt;</span>
+      </div>
+      <div class="next">
+        <span>&gt;</span>
+      </div>
+    </div>
+    <script src="./6.轮播.js"></script>
+  </body>
+</html>