1. 程式人生 > >原生JS相容移動端的輪播圖

原生JS相容移動端的輪播圖

關於輪播圖 :

  • 原生JS
  • 相容IE9+等各大瀏覽器
  • 相容移動端
  • 自適應(根據圖片原比例)
  • 左右箭頭切換
  • 點選索引切換
  • 移動端滑動切換
  • 只需修改圖片路徑
    在這裡插入圖片描述
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
>
<title>原生JS相容移動端-輪播圖</title> <style> *, ::before, ::after{ padding: 0; margin: 0; box-sizing: border-box; -webkit-box-sizing: border-box; -webkit-tap-highlight-color: transparent;/*清除移動端點選高亮效果*/ } body{ font-family:Microsoft YaHei,sans-serif;
font-size: 14px; color: #333; } ol,ul{ list-style: none; } /*清除浮動*/ .clearfix::before, .clearfix::after{ content: ""; display: block; height: 0; line-height: 0; visibility: hidden; clear: both; } .carousel{ width: 100%; max-width
: 750px; min-width: 320px; margin: 0 auto; position: relative; } #banner{ width: 100%; overflow: hidden; position: relative; } #banner ul:first-child{ width: 1000%; transform: translateX(-10%); -o-transform: translateX(-10%); -ms-transform: translateX(-10%); /* IE 9 */ -moz-transform: translateX(-10%); -webkit-transform: translateX(-10%); overflow: hidden; } #banner ul:first-child li{ width: 10%; float: left; box-sizing: border-box; } #banner ul:first-child li a{ display: block; width: 100%; position: relative; } #banner ul:first-child li a span{ left: 50%; position: absolute; font-size: 40px; text-align: center; display: block; top: 50%; transform: translate(-50%,-50%); color: red; } #banner ul:first-child li a img{ width: 100%; display: block; } #banner ul:last-child{ position: absolute; bottom: 6px; width: 100%; text-align: center; } #banner ul:last-child li{ width: 6px; height: 6px; border: 1px solid #fff; border-radius: 50%; display: inline-block; margin-left: 10px; } #banner ul:last-child li:first-child{ margin-left: 0; } #banner ul:last-child li.now{ background: red; } /*左右切換*/ .switchBtn{ position: absolute; width: 6%; top: 50%; box-sizing: border-box; cursor: pointer; } .switchBtn img{ display: block; width: 100%; opacity: .35; } .switchBtn:hover img{ opacity: .85; } #prevBtn{ left: 20px; transform: scaleX(-1) translate(0,-50%); -o-transform: scaleX(-1) translate(0,-50%); -ms-transform: scaleX(-1) translate(0,-50%); -moz-transform: scaleX(-1) translate(0,-50%); -webkit-transform: scaleX(-1) translate(0,-50%); } #nextBtn{ right: 20px; transform: translate(0,-50%); -o-transform: translate(0,-50%); -ms-transform: translate(0,-50%); -moz-transform: translate(0,-50%); -webkit-transform: translate(0,-50%); }
</style> </head> <body> <div class="carousel"> <div id="banner"> <ul class="clearfix slide"> <li><a href="#"><img src="images/1.jpg"><span>1</span></a></li> <li><a href="#"><img src="images/2.jpg"><span>2</span></a></li> <li><a href="#"><img src="images/3.jpg"><span>3</span></a></li> <li><a href="#"><img src="images/4.jpg"><span>4</span></a></li> <li><a href="#"><img src="images/5.jpg"><span>5</span></a></li> <li><a href="#"><img src="images/6.jpg"><span>6</span></a></li> <li><a href="#"><img src="images/7.jpg"><span>7</span></a></li> <li><a href="#"><img src="images/8.jpg"><span>8</span></a></li> </ul> <!--左右切換箭頭--> <span class="switchBtn" id="prevBtn"><img src="images/arrow.png" class="prevArrow" alt=""></span> <span class="switchBtn" id="nextBtn"><img src="images/arrow.png" class="nextArrow" alt=""></span> <!--輪播索引的點--> <ul class="ctrl"></ul> </div> </div> <script> window.onload = function(){ var banner = document.getElementById('banner');//輪播盒子 var width = banner.offsetWidth; var imageBox = banner.querySelector('ul.slide'); var imageCount = imageBox.querySelectorAll('li').length; //圖片張數 var pointBox = banner.querySelector('ul.ctrl');//點盒子 var prevBtn = document.getElementById("prevBtn"); //向左按鈕 var nextBtn = document.getElementById("nextBtn"); //向右按鈕 var index = 1; //預設開始的圖片位置 var moveSpeed = 2000;//輪播速度 //動態新增索引 for(var i = 0;i<imageCount;i++){ var li = document.createElement("li"); pointBox.appendChild(li); if(i == 0){ //預設第一個點高亮 li.className="now" } } var points = pointBox.querySelectorAll('li');//所有的點 //在第一張前面新增最後一張,最後一張後面新增第一張,防止切換到首尾時出現空白 var slideFirst = imageBox.querySelector('li:first-child'); var newLast = slideFirst.cloneNode(true);//複製第一張 imageBox.appendChild(newLast);//在最後面新增第一張 var slideLast = imageBox.querySelector('li:nth-last-of-type(2)');//最後一張圖片,此時已經在最後面添加了第一張 var newFirst = slideLast.cloneNode(true);//複製最後一張 prependChild(imageBox,newFirst);//在最前新增最後一張 //在最前新增最後一張的方法 function prependChild(parent,newChild){ if(parent.firstChild){ parent.insertBefore(newChild,parent.firstChild); } else { parent.appendChild(newChild); } return parent; } //加過渡 var addTransition = function(){ imageBox.style.transition = "all 0.3s"; imageBox.style.oTransition = "all 0.3s"; imageBox.style.msTransition = "all 0.3s"; imageBox.style.mozTransition = "all 0.3s"; imageBox.style.webkitTransition = "all 0.3s"; }; //清除過渡 var removeTransition = function(){ imageBox.style.transition = "none"; imageBox.style.oTransition = "none"; imageBox.style.msTransition = "none"; imageBox.style.mozTransition = "none"; imageBox.style.webkitTransition = "none"; }; //定位 var setTranslateX = function(translateX){ imageBox.style.transform = "translateX("+translateX+"px)"; imageBox.style.oTransform = "translateX("+translateX+"px)"; imageBox.style.msTransform = "translateX("+translateX+"px)"; imageBox.style.mozTransform = "translateX("+translateX+"px)"; imageBox.style.webkitTransform = "translateX("+translateX+"px)"; }; // 判斷是否動畫結束 function transitionEnd(dom,callback){ if(!dom || typeof dom != 'object'){ return false; } dom.addEventListener('transitionend', function(){ callback && callback(); }); dom.addEventListener('transitionEnd', function(){ callback && callback(); }); dom.addEventListener('oTransitionEnd', function(){ callback && callback(); }); dom.addEventListener('msTransitionEnd', function(){ callback && callback(); }); dom.addEventListener('mozTransitionEnd', function(){ callback && callback(); }); dom.addEventListener('webkitTransitionEnd', function(){ callback && callback(); }); } var timer = null; // 輪播動畫 function setTime(){ timer = setInterval(function(){ index ++; addTransition(); setTranslateX(-index * width); transitionEnd(imageBox,function(){ if(index == imageCount){ removeTransition(); index = 0; setTranslateX(-index * width); } }); setPoint(); },moveSpeed); } setTime(); //當前圖片對應的點高亮 var setPoint = function(){ for(var i = 0 ; i < points.length ; i++){ points[i].className = " "; } if(index == 0){ points[imageCount-1].className = "now"; } if(index == imageCount+1){ points[0].className = "now"; } if(index <= imageCount){ points[index-1].className = "now"; } }; //touch事件 var startX = 0; //記錄起始x的座標 var moveX = 0; //滑動的時候x的位置 var distanceX = 0; //滑動的距離 var isMove = false; //是否滑動過 imageBox.addEventListener('touchstart', function(e){ startX = e.touches[0].clientX; //記錄起始X }); imageBox.addEventListener('touchmove',function(