js實(shí)現(xiàn)單張圖片平移切換效果
本文參考了JQuery實(shí)現(xiàn)圖片切換(自動(dòng)切換+手動(dòng)切換)
由于個(gè)人不需要手動(dòng)切換功能,因此把那部分的內(nèi)容刪了,主要是增加了無(wú)縫切換的效果。
原理也很簡(jiǎn)單,大概是一張圖移動(dòng)到左邊以后,從底部移回最右,等待下一次循環(huán)。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.1.min.js"></script> </head> <body> <div class="wrapper"> <h1>jquer實(shí)現(xiàn)圖片切換</h1> <div id="focus"> <ul> <!-- 這里有三個(gè)z-index的設(shè)置 --> <li><div class="switch_pic" style="z-index: 3;background: url('imgs/01.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="z-index: 2;background: url('imgs/02.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="z-index: 1;background: url('imgs/03.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="background: url('imgs/04.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="background: url('imgs/meiko2.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="background: url('imgs/meiko7.jpg') center center;background-size: cover;"></div></li> </ul> </div> </div> <script type="text/javascript"> $(function() { var sWidth = $("#focus").width(); var len = $("#focus ul li").length; var index = 0; var picTimer; var $pics = $("#focus ul li").find('.switch_pic');//獲取所有圖片 showPics(index); //網(wǎng)頁(yè)打開(kāi)立即執(zhí)行一次動(dòng)畫 picTimer = setInterval(function() { index++; if(index == len) {index = 0;} showPics(index); },3000);//3000毫秒的間隔 //顯示圖片函數(shù),根據(jù)接收的index值顯示相應(yīng)的內(nèi)容 function showPics(index) { //普通切換 var nowLeft = -sWidth; //每次移動(dòng)固定量 var $pic = $pics.eq(index);//獲取當(dāng)前要移出的圖片 var $nexPic = $pics.eq((index+1)%len);//當(dāng)前要移入的圖片 var $nexnexPic = $pics.eq((index+2)%len);//下一個(gè)要移入的圖片 $nexPic.css("left",sWidth);//下一個(gè)圖片移動(dòng)到最右 //當(dāng)前要移出的圖片開(kāi)始左移,模式設(shè)為平滑"linear",速度和間隔一樣 $pic.animate({"left":nowLeft},3000,"linear",function(){ // 當(dāng)前圖片完全移出后,重新設(shè)置z-index $pic.css("z-index",1); $nexPic.css("z-index",3); $nexnexPic.css("z-index",2); }); //當(dāng)前要移入的圖片同時(shí)左移 $nexPic.animate({"left":0},3000,"linear"); } }); </script> <style type="text/css"> *{margin:0;padding:0;} body{font-size:12px;color:#222;font-family:Verdana,Arial,Helvetica,sans-serif;background:#f0f0f0;} .clearfix:after{content: ".";display: block;height: 0;clear: both;visibility: hidden;} .clearfix{zoom:1;} ul,li{list-style:none;} img{border:0;} .wrapper{width:800px;margin:0 auto;padding-bottom:50px;} h1{height:50px;line-height:50px;font-size:22px;font-weight:normal;font-family:"Microsoft YaHei",SimHei;margin-bottom:20px;} #focus{width:450px;height:350px;overflow:hidden;position:relative;} #focus ul{height:380px;position:absolute;} #focus ul li{float:left;width:450px;height:350px;overflow:hidden;position:absolute;background:#000;} #focus ul li div{position:absolute;overflow:hidden;width: 450px;height: 350px;} </style> </body>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解JavaScript對(duì)數(shù)組操作(添加/刪除/截取/排序/倒序)
這篇文章主要介紹了JavaScript對(duì)數(shù)組操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04javascript獲取當(dāng)前鼠標(biāo)坐標(biāo)的方法
這篇文章主要介紹了javascript獲取當(dāng)前鼠標(biāo)坐標(biāo)的方法,可針對(duì)不同瀏覽器獲取鼠標(biāo)的坐標(biāo)位置,是非常實(shí)用技巧,需要的朋友可以參考下2015-01-01Javascript的數(shù)組與字典用法與遍歷對(duì)象的技巧
Javascript 的數(shù)組Array,既是一個(gè)數(shù)組,也是一個(gè)字典(Dictionary)。先舉例看看數(shù)組的用法2012-11-11JavaScript中創(chuàng)建類/對(duì)象的幾種方法總結(jié)
這篇文章主要是對(duì)JavaScript中創(chuàng)建類/對(duì)象的幾種方法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11JS實(shí)現(xiàn)模擬風(fēng)力的雪花飄落效果
這篇文章主要介紹了JS實(shí)現(xiàn)模擬風(fēng)力的雪花飄落效果,可在右側(cè)填入風(fēng)力值點(diǎn)擊按鈕即可看到伴隨風(fēng)力的雪花飄落效果,同時(shí)右側(cè)有實(shí)時(shí)雪花數(shù)量統(tǒng)計(jì)功能,需要的朋友可以參考下2015-05-05JavaScript 全面解析各種瀏覽器網(wǎng)頁(yè)中的JS 執(zhí)行順序
近來(lái)我通過(guò)一些測(cè)試以全面的解析網(wǎng)頁(yè)在各種瀏覽器中的JavaScript代碼的執(zhí)行順序,在這兒做個(gè)記錄。2009-02-02JS實(shí)現(xiàn)數(shù)組去重方法總結(jié)(六種方法)
這篇文章給大家總結(jié)下JS實(shí)現(xiàn)數(shù)組去重方法(六種方法),面試中也經(jīng)常會(huì)遇到這個(gè)問(wèn)題。文中給大家引申的還有合并數(shù)組并去重的方法,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2017-07-07