jQuery實現(xiàn)的簡單圖片輪播效果完整示例
本文實例講述了jQuery實現(xiàn)的簡單圖片輪播效果。分享給大家供大家參考,具體如下:
先來看看運行效果:
具體代碼如下:
<!DOCTYPE html> <html> <head> <title>jquery實現(xiàn)圖片輪播效果</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style> #lunbo{width: 600px;height: 300px;margin: 0 auto;overflow: hidden;} #pics{width: 600px;height: 300px;cursor: pointer;position: relative;} ul li{width: 600px;height: 300px;list-style: none;position: absolute;top: 0;left: 0;display: none;} img{width: 600px;height: 300px;} </style> </head> <body> <div id="lunbo"> <ul id="pics"> <li style="display: inline;"><img src="http://pic.qiantucdn.com/58pic/14/60/54/32n58PICxE6_1024.jpg"></li> <li><img src="http://pic.qiantucdn.com/58pic/19/09/94/5678b76f75315_1024.jpg"></li> <li><img src="http://pic.qiantucdn.com/58pic/19/39/22/01D58PICFx7_1024.jpg"></li> <li><img src="http://pic.qiantucdn.com/58pic/19/11/08/5679490f4892d_1024.jpg"></li> <li><img src="http://pic.qiantucdn.com/58pic/18/44/26/5620690acb188_1024.jpg"></li> </ul> </div> <script type="text/javascript"> $(document).ready(function(){ var oLi = $("li"); var liWidth = oLi.eq(0).width(); var liHeight = oLi.eq(0).height(); //每隔3秒進行輪播 var timer = setInterval(change,3000); //鼠標放置在圖片上時停止輪播,移開時繼續(xù)輪播 $("div").mouseover ( function(){ clearInterval(timer); }) $("div").mouseout (function(){ timer = setInterval(change,3000); }) //輪播函數(shù) var prevIndex = 0,nextIndex = 1; function change(){ if (prevIndex == oLi.length-1 ) {//若當前圖片是最后一張圖片,下一張出現(xiàn)首張圖片 nextIndex = 0; } var n = Math.floor(Math.random()*3); if (n == 0) { fade(prevIndex,nextIndex); } else if (n== 2) { slide(prevIndex,nextIndex); } else{ grap(prevIndex,nextIndex); } prevIndex = nextIndex; nextIndex ++; } //淡入淡出效果, function fade(prevIndex,nextIndex){ //傳入當前顯示圖片以及下一張圖片的索引 oLi.eq(prevIndex).fadeOut(1000); oLi.eq(nextIndex).fadeIn(1000); } //向左右滑動效果 function slide(prevIndex,nextIndex){ var rand = Math.floor(Math.random()*2); oLi.eq(nextIndex).show(); oLi.eq(nextIndex).css("z-index","-1"); if (rand) { //向左滑動 oLi.eq(prevIndex).animate({left: -liWidth},1000,fun); } else{ oLi.eq(prevIndex).animate({left: liWidth},1000,fun); } function fun(){ oLi.eq(prevIndex).css({"left":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); } } //收縮效果 function grap(prevIndex,nextIndex){ var rand = Math.floor(Math.random()*4); oLi.eq(nextIndex).show(); oLi.eq(nextIndex).css("z-index","-1"); switch (rand){ case 0://向左上角滑動 oLi.eq(prevIndex).animate({left: -liWidth,top: -liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":"0","top": "0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 1://向右上角滑動 oLi.eq(prevIndex).animate({left: liWidth,top: -liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 2://向右下角滑動 oLi.eq(prevIndex).animate({left: liWidth,top: liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 3://向左下角滑動 oLi.eq(prevIndex).animate({left: -liWidth,top: liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; default:break; } } }); </script> </body> </html>
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery圖片操作技巧大全》、《jQuery切換特效與技巧總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
- 原生js和jquery實現(xiàn)圖片輪播淡入淡出效果
- 基于jQuery實現(xiàn)淡入淡出效果輪播圖
- 原生js和jQuery實現(xiàn)淡入淡出輪播效果
- jQuery實現(xiàn)圖片簡單輪播功能示例
- 使用JQuery實現(xiàn)圖片輪播效果的實例(推薦)
- 純javaScript、jQuery實現(xiàn)個性化圖片輪播【推薦】
- jQuery簡單自定義圖片輪播插件及用法示例
- jQuery實現(xiàn)的圖片輪播效果完整示例
- jQuery的圖片輪播插件PgwSlideshow使用詳解
- jQuery實現(xiàn)圖片輪播效果代碼(基于jquery.pack.js插件)
- jQuery實現(xiàn)的淡入淡出圖片輪播效果示例
相關(guān)文章
easyui Draggable組件實現(xiàn)拖動效果
Draggable是easyui中用于實現(xiàn)拖拽功能的一個插件。利用它,我們可以實現(xiàn)控件的拖拽效果。Draggble覆蓋默認值$.fn.draggable.defaults。2015-08-08JQuery獲取可視區(qū)尺寸和文檔尺寸及制作懸浮菜單示例
這篇文章主要介紹了JQuery獲取可視區(qū)尺寸和文檔尺寸及制作懸浮菜單,涉及jQuery針對頁面元素屬性動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-05-05jQuery實現(xiàn)jQuery-form.js實現(xiàn)異步上傳文件
jquery.form.js是一個非常強大的用于表單提交的插件。這篇文章主要介紹了jQuery實現(xiàn)jQuery-form.js實現(xiàn)異步上傳文件,有興趣的可以了解一下。2017-04-04jquery中ajax函數(shù)執(zhí)行順序問題之如何設(shè)置同步
這篇文章主要介紹了jquery中ajax函數(shù)執(zhí)行順序問題之如何設(shè)置同步,需要的朋友可以參考下2014-02-02jquery.bgiframe.js在IE9下提示INVALID_CHARACTER_ERR錯誤
今天測試偶然發(fā)現(xiàn)jquery.bgiframe.js在IE9環(huán)境下提示錯誤,于是很是好奇,想辦法知道究竟,于是搜索了一下,現(xiàn)在與大家分享希望可以幫助你們2013-01-01