瀏覽代碼

使用js制作轮播图滚动效果

Cathy 4 年之前
父節點
當前提交
554eeea265
共有 9 個文件被更改,包括 99 次插入11 次删除
  1. 13 1
      pinyougou/css/index.css
  2. 7 10
      pinyougou/index.html
  3. 12 0
      pinyougou/js/animate.js
  4. 67 0
      pinyougou/js/index.js
  5. 二進制
      pinyougou/upload/focus (1).jpg
  6. 二進制
      pinyougou/upload/focus (2).jpg
  7. 二進制
      pinyougou/upload/focus1.jpg
  8. 二進制
      pinyougou/upload/focus2.jpg
  9. 二進制
      pinyougou/upload/focus3.jpg

+ 13 - 1
pinyougou/css/index.css

@@ -7,15 +7,27 @@
 }
 .focus {
 	position: relative;
-	width: 720px;
+	width: 721px;
 	height: 100%;
+	overflow: hidden;
+}
+.focus ul {
+	position: absolute;
+	top: 0;
+	left: 0;
+	width: 600%;
+}
+.focus ul li {
+	float: left;
 }
 .arrow-l,
 .arrow-r {
+	display: none;
 	position: absolute;
 	width: 24px;
 	height: 40px;
 	top: 50%;
+	z-index: 2;
 	margin-top: -20px;
 	background: rgba(0, 0, 0, .3);
 	text-align: center;

+ 7 - 10
pinyougou/index.html

@@ -12,6 +12,8 @@
 	<link rel="stylesheet" href="css/index.css">
 	<link rel="shortcut icon" href="favicon.ico">
 	<link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
+	<script src="js/animate.js"></script>
+	<script src="js/index.js"></script>
 </head>
 <body>
 	<!-- 顶部导航start  -->
@@ -197,20 +199,15 @@
 	<div class="w">
 		<div class="main">
 			<div class="focus fl">
-				<a href="#" class="arrow-l">&lt;</a>
-				<a href="#" class="arrow-r">&gt;</a>
+				<a href="javascript:;" class="arrow-l">&lt;</a>
+				<a href="javascript:;" class="arrow-r">&gt;</a>
 				<ul>
 					<li><a href="#"><img src="upload/focus.jpg" alt=""></a></li>
+					<li><a href="#"><img src="upload/focus1.jpg" alt=""></a></li>
+					<li><a href="#"><img src="upload/focus2.jpg" alt=""></a></li>
+					<li><a href="#"><img src="upload/focus3.jpg" alt=""></a></li>
 				</ul>
 				<ol class="circle">
-					<li></li>
-					<li class="current"></li>
-					<li></li>
-					<li></li>
-					<li></li>
-					<li></li>
-					<li></li>
-					<li></li>
 				</ol>
 			</div>
 			<div class="newsflash fr">

+ 12 - 0
pinyougou/js/animate.js

@@ -0,0 +1,12 @@
+function animate(obj, target, callback) {
+  clearInterval(obj.timer);
+  obj.timer = setInterval(() => {
+    var step = (target - obj.offsetLeft) / 10;
+    step = step > 0 ? Math.ceil(step): Math.floor(step);
+    obj.style.left = obj.offsetLeft + step + 'px';
+    if(obj.offsetLeft == target){
+      clearInterval(obj.timer);
+      callback && callback();
+    }
+  }, 15);
+}

+ 67 - 0
pinyougou/js/index.js

@@ -0,0 +1,67 @@
+window.onload = function () {
+  var focus = document.querySelector('.focus');
+  var arrow_l = document.querySelector('.arrow-l');
+  var arrow_r = document.querySelector('.arrow-r');
+  var ul = focus.querySelector('ul');
+  var ol = focus.querySelector('.circle');
+  focus.addEventListener('mouseenter', function () {
+    arrow_l.style.display = 'block';
+    arrow_r.style.display = 'block';
+    clearInterval(timer);
+  });
+  focus.addEventListener('mouseleave', function () {
+    arrow_l.style.display = 'none';
+    arrow_r.style.display = 'none';
+    timer = setInterval(() => {
+      arrow_r.click();
+    }, 2000);
+  });
+  for(var i = 0; i < ul.children.length; i++) {
+    var li = this.document.createElement('li');
+    li.setAttribute('index', i);
+    ol.appendChild(li);
+    li.addEventListener('click', function () {
+      var index = this.getAttribute('index');
+      var left = -1 * index * focus.offsetWidth;
+      // 每点一个小li,就要滚动图片的位置
+      animate(ul, left);
+      liChange(this);
+      num = index;
+    });
+  }
+  ol.children[0].className = 'current';
+  // 循环图片,克隆第一张放到尾部
+  ul.appendChild(ul.children[0].cloneNode(true));
+  var num = 0;
+  arrow_l.addEventListener('click',  function () {
+    if(num == 0){
+      num = ul.children.length-1;
+      ul.style.left = -1 * num * focus.offsetWidth + 'px';
+    }
+    num--;
+    var left = -1 * num * focus.offsetWidth;
+    animate(ul, left);
+    liChange(ol.children[num%ol.children.length]);
+  });
+  arrow_r.addEventListener('click',  function () {
+    if(num == ul.children.length-1){
+      num = 0;
+      ul.style.left = 0;
+    }
+    num++;
+    var left = -1 * num * focus.offsetWidth;
+    animate(ul, left);
+    liChange(ol.children[num%ol.children.length]);
+  });
+  var timer = setInterval(() => {
+    arrow_r.click();
+  }, 2000);
+
+  function liChange(obj) {
+    // 排他思想,给点击的小li加上样式
+    for(var i = 0; i < ol.children.length; i++){
+      ol.children[i].className = '';
+    }
+    obj.className = 'current';
+  }
+}

二進制
pinyougou/upload/focus (1).jpg


二進制
pinyougou/upload/focus (2).jpg


二進制
pinyougou/upload/focus1.jpg


二進制
pinyougou/upload/focus2.jpg


二進制
pinyougou/upload/focus3.jpg