一、實現效果圖
二、分析布局
主盒子裡分上下兩個小盒子(1和2)。
包含圖片的盒子占兩張圖片的寬(3),處於上盒子中,當前圖片在上盒子(1)中,其它圖片在盒子(3)的右側等待播放。
下邊的盒子(2)包括了六個小方塊及定位在主盒子上的兩個箭頭。
三、html部分
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>wangyi</title> <link type="text/css" rel="stylesheet" href="css/guide.css"/> <script type="text/javascript" src="../animate.js"></script> <script type="text/javascript" src="js/main.js"></script> </head> <body> <div class="w-slider"> <div class="slider"> <div class="slider-main" id="slider_main_block"> <div class="slider-main-img"><a href="#"><img src="images/1.jpg"/></a></div> <div class="slider-main-img"><a href="#"><img src="images/2.jpg"/></a></div> <div class="slider-main-img"><a href="#"><img src="images/3.jpg"/></a></div> <div class="slider-main-img"><a href="#"><img src="images/4.jpg"/></a></div> <div class="slider-main-img"><a href="#"><img src="images/5.jpg"/></a></div> <div class="slider-main-img"><a href="#"><img src="images/6.jpg"/></a></div> </div> </div> <div class="slider-ctrl" id="slider_ctrl"> <span class="slider-ctrl-pre"></span> <span class="slider-ctrl-next"></span> </div> </div> </body> </html>
四、CSS部分
* { margin: 0; padding: 0; } .w-slider { width:310px; height:265px; margin:100px auto; overflow: hidden; position: relative; } .slider { width:310px; height:220px; } .slider-main { width:620px; height:220px; } .slider-main-img{ width:310px; height:220px; position:absolute; top:0; left:0; } .slider-main-img img{ vertical-align: top; } .slider-ctrl { width:310px; height:45px; text-align:center; } .slider-ctrl span { cursor: pointer; } .slider-ctrl-con { display:inline-block; width:24px; height:20px; margin-right:8px; text-indent: -20em; overflow:hidden; background:url(../images/icon.png)-24px -782px no-repeat; } .slider-ctrl .current { background:url(../images/icon.png)-24px -762px no-repeat; } .slider-ctrl-pre, .slider-ctrl-next { width:27px; height:38px; position:absolute; top:50%; margin-top:-36px; opacity:0.7; filter:alpha(opacity=50); } .slider-ctrl-pre { background:url(../images/icon.png)6px 1px no-repeat; left:0; } .slider-ctrl-next { background:url(../images/icon.png)-9px -44px no-repeat; right:0; }
五、JS部
/** * Created by 15623544233 on 2016/10/17. */ window.onload=function () { //imgs的DOM對象 var slider_main_block =$("slider_main_block"); var sliderImgs =slider_main_block.children; //最外層的slider大盒子 var sider_box =slider_main_block.parentNode.parentNode; //slider-ctrl的DOM對象 var slider_ctrl = $("slider_ctrl"); var sliderCtrls =slider_ctrl.children; //動態添加Ctrl for(var i=0;i<sliderImgs.length;i++){ var spans = document.createElement("span"); spans.className="slider-ctrl-con"; //span中的數字為當前的索引,後面有重要作用 spans.innerHTML =sliderImgs.length-i; slider_ctrl.insertBefore(spans,sliderCtrls[1]); } sliderCtrls[1].setAttribute("class","current slider-ctrl-con"); //imgW ---圖片寬 var imgW = sliderImgs[0].clientWidth; //讓除第一張圖以外的所有圖右移 for(var i=1;i<sliderImgs.length;i++){ sliderImgs[i].style.left=imgW+"px"; } //至關重要的標志位,currentId 當前出現的盒子 var currentId = 0; for(var key in sliderCtrls){ sliderCtrls[key].onclick =function () { //左箭頭點擊的運動動畫 if(this.className=="slider-ctrl-pre"){ //與左移動參數相反 animate(sliderImgs[currentId],{left:imgW}); --currentId<0?currentId=sliderImgs.length-1:currentId; sliderImgs[currentId].style.left=-imgW+"px"; animate(sliderImgs[currentId],{left:0}); //右箭頭的運動動畫,與自動輪播的運動動畫一致 }else if(this.className=="slider-ctrl-next"){ autoPlay(); }else{ //得到當前要點擊的圖片索引號 var that = this.innerHTML-1; if(that>currentId){ //像點擊右側按鈕一樣播放 animate(sliderImgs[currentId],{left:-imgW}); sliderImgs[that].style.left=imgW+"px"; }else if(that<currentId){ animate(sliderImgs[currentId],{left:imgW}); sliderImgs[that].style.left=-imgW+"px"; } /*當點擊按鈕後,當前圖片currentId移動後,所點擊的盒子成為當前的圖片,使圖片連續*/ currentId =that; animate(sliderImgs[currentId],{left:0}); } flashCurrent((currentId+1)); }; } //小方塊控制slider動畫的函數 function flashCurrent(index) { //console.log(index); for(var i=1;i<sliderCtrls.length-1;i++){ sliderCtrls[i].setAttribute("class","slider-ctrl-con"); } sliderCtrls[index].setAttribute("class","current slider-ctrl-con"); } //添加定時器 var timer =null; timer =setInterval(autoPlay,2000); function autoPlay() { animate(sliderImgs[currentId],{left:-imgW}); //當第6張左移走後,6>5,第0張回到框中 ++currentId>sliderImgs.length-1?currentId = 0:currentId; //上一張圖片左移後,下一張圖片快速到右邊 sliderImgs[currentId].style.left=imgW+"px"; //下張右側圖片左移 animate(sliderImgs[currentId],{left:0}); } sider_box.onmouseover =function () { clearInterval(timer); } sider_box.onmouseout=function () { timer =setInterval(autoPlay,2000); } }; function $(id) { return document.getElementById(id); } /*當前樣式屬性值的獲取 */ function curStyle(obj,attr){ if(obj.currentStyle){ //IE浏覽器 return obj.currentStyle[attr]; }else{ //標准浏覽器 return window.getComputedStyle(obj,null)[attr]; } } /*封裝的運動框架*/ /*obj ----做動畫的DOM對象 ;json----變化的部分是json數據 */ function animate(obj,json) { //調用時先清定時器 clearInterval(obj.timer); //定時器為obj的內部定時器,不用每次調用都創建一個新的定時器 obj.timer = setInterval(function () { //遍歷json數據,每次遍歷的標志位為flag var flag=true; //遍歷json數據,eg:{left:20,top:40,opacity:50,z-index:3} for(var key in json){ //取得盒子運動當前的位置 var current= 0; if(key=="opacity"){ //Ie6,7,8沒有設置透明度,默認為undefined current =Math.round(parseInt(curStyle(obj,key)*100))||1; //console.log(current); }else{ current= parseInt(curStyle(obj,key)); } //運動步長:(目標位置-當前位置)/10 var step = (json[key]-current)/10; step = step>0?Math.ceil(step):Math.floor(step); //各屬性值的漸變動畫 if(key=="opacity"){ if("opacity" in obj.style){ obj.style.opacity = (current+step)/100; }else{ //兼容ie6,7,8 obj.style.filter ="alpha(opacity ="+(current+step)*10+")"; } }else if(key=="zIndex"){ obj.style[key] =json[key]; }else { obj.style[key] = current+step+"px"; } //遍歷每個屬性時都判斷標志位 if(current!=json[key]){ flag =false; } }遍歷結束後,標志位都為true,判斷起所有動畫執行完畢,清除定時器 if(flag){ clearInterval(obj.timer); } },20) }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。