jquery實(shí)現(xiàn)呼吸輪播效果
本文實(shí)例為大家分享了jquery實(shí)現(xiàn)呼吸輪播效果的具體代碼,供大家參考,具體內(nèi)容如下
大概思路:
呼吸輪播就是圖片淡入淡出輪播。
div(設(shè)置相對(duì)定位)里存放ul,li不需要float:left,設(shè)置絕對(duì)定位即可。left:0,top:0。
此時(shí)不需要在所有圖片后面補(bǔ)假0.
div里還包括左右按鈕,下面小圓點(diǎn),通過(guò)絕對(duì)定位,把它們定位到合適位置就好。
設(shè)置index為0,點(diǎn)擊右按鈕時(shí),當(dāng)前圖片淡出,index++,當(dāng)圖片為最后一個(gè)的時(shí)候,index設(shè)置為0,新圖片淡入。下面小圓點(diǎn)對(duì)應(yīng)變色。
左按鈕思路大致相同。
下面小圖片點(diǎn)擊的時(shí)候,如果點(diǎn)擊的和當(dāng)前顯示的index相同,則什么事情都不需要做。
點(diǎn)擊其他小圓點(diǎn)的時(shí)候,舊的圖片淡出,把當(dāng)前小圓點(diǎn)的index賦值給全局變量index,新圖片淡入。
被點(diǎn)擊的小圓點(diǎn)變色,其兄弟元素仍然為最初的顏色。
程序:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> *{ margin:0; padding:0; } .big{ width:560px; height:300px; position: relative; margin:200px auto; border:10px solid red; } .big .move{ width:5600px; height:300px; position: absolute; left:0; top:0; } .big .move li{ /*float: left;*/ list-style: none; display: none; position: absolute; top:0; left:0; } .big ul .first{ display: block; } img{ width:560px; height:300px; } .btn div{ width:40px; height:60px; background: red; position: absolute; top:50%; margin-top:-30px; } .rightbtn{ right:0; } .circle{ position: absolute; left:0px; bottom:0px; width:200px; height:30px; } .circle ul{ /*overflow: hidden;*/ list-style: none; float: left; } .circleUl li{ background: white; float: left; margin-right:10px; width:20px; height:20px; border-radius:50%; } </style> <body> <div class="big"> <ul class="move"> <li class="first"> <img src="img/0.jpg" alt=""> </li> <li> <img src="img/1.jpg" alt=""> </li> <li> <img src="img/2.jpg" alt=""> </li> <li> <img src="img/3.jpg" alt=""> </li> </ul> <div class="btn"> <div class="leftbtn"> < </div> <div class="rightbtn"> > </div> </div> <div class="circle"> <ul class="circleUl"> <li></li> <li></li> <li></li> <li></li> </ul> </div> </div> <script src="js/jquery-1.12.3.min.js"> </script> <script> // 設(shè)置第一個(gè)小圓點(diǎn)為紅色 $(".circleUl li").eq(0).css("background","red"); var index = 0; $(".leftbtn").click(function(){ // 舊的圖淡出 $(".move li").eq(index).fadeOut(400); index--; if(index<0){ index = 3; } // 新圖淡入 $(".move li").eq(index).fadeIn(400); // 對(duì)應(yīng)下標(biāo)為index的小圓點(diǎn)變色 $(".circleUl li").eq(index).css("background","red").siblings().css("background","white"); }); $(".rightbtn").click(function(){ $(".move li").eq(index).fadeOut(400); index++; console.log(index); if(index ==4 ){ index = 0; } $(".move li").eq(index).fadeIn(400); $(".circleUl li").eq(index).css("background","red").siblings().css("background","white"); }); // 小圓點(diǎn)點(diǎn)擊事件 $(".circleUl li").click(function(){ // 如果本來(lái)就顯示的圖一,再次點(diǎn)擊第一個(gè)小圓點(diǎn)的話,什么也不做 if(index == $(this).index()) return; // 舊的圖片淡出 $(".move li").eq(index).fadeOut(400); // 點(diǎn)擊到哪個(gè)小圓點(diǎn),把小圓點(diǎn)的index賦值給全局變量index (更新全局變量index) index = $(this).index(); // 新圖片淡入 $(".move li").eq(index).fadeIn(400); // 小圓點(diǎn)變色 $(this).css("background","red").siblings().css("background","white"); }) </script> </body> </html>
運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JQuery 學(xué)習(xí)筆記01 JQuery初接觸
jQuery是一個(gè)Javascript庫(kù),用于簡(jiǎn)化Web相關(guān)的Javascript開(kāi)發(fā)2010-05-05jQuery實(shí)現(xiàn)控制文字內(nèi)容溢出用省略號(hào)(…)表示的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)控制文字內(nèi)容溢出用省略號(hào)(…)表示的方法,涉及jQuery針對(duì)字符串及樣式操作相關(guān)技巧,需要的朋友可以參考下2016-02-02jquery實(shí)現(xiàn)ajax提交表單信息的簡(jiǎn)單方法(推薦)
下面小編就為大家?guī)?lái)一篇jquery實(shí)現(xiàn)ajax提交表單信息的簡(jiǎn)單方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08jQuery 綁定事件到動(dòng)態(tài)創(chuàng)建的元素上的方法實(shí)例
這篇文章介紹了jQuery 綁定事件到動(dòng)態(tài)創(chuàng)建的元素上的方法實(shí)例,有需要的朋友可以參考一下2013-08-08擴(kuò)展jquery easyui tree的搜索樹(shù)節(jié)點(diǎn)方法(推薦)
下面小編就為大家?guī)?lái)一篇擴(kuò)展jquery easyui tree的搜索樹(shù)節(jié)點(diǎn)方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10jQuery.extend 與 jQuery.fn.extend的用法及區(qū)別實(shí)例分析
這篇文章主要介紹了jQuery.extend 與 jQuery.fn.extend的用法及區(qū)別,結(jié)合實(shí)例形式分析了jQuery.extend與jQuery.fn.extend的功能、使用方法及區(qū)別,需要的朋友可以參考下2018-07-07jQuery打印指定區(qū)域Html頁(yè)面并自動(dòng)分頁(yè)
項(xiàng)目中需要用到打印HTML頁(yè)面,需要指定區(qū)域打印,使用jquery.PrintArea.js 插件實(shí)現(xiàn)分頁(yè),需要的朋友可以參考下2014-07-07