您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用jQuery怎么實現一個淡入淡出圖片輪播效果,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
jquery是一個簡潔而快速的JavaScript庫,它具有獨特的鏈式語法和短小清晰的多功能接口、高效靈活的css選擇器,并且可對CSS選擇器進行擴展、擁有便捷的插件擴展機制和豐富的插件,是繼Prototype之后又一個優秀的JavaScript代碼庫,能夠用于簡化事件處理、HTML文檔遍歷、Ajax交互和動畫,以便快速開發網站。
1.HTML 框架搭建(css代碼里寬高的大小與圖片的大小一致)
css部分:
<style> * { padding: 0; margin:0; } ul { list-style: none; } .out { width: 640px; height: 270px; margin:50px auto; position: relative; } .out img{ width: 640px; height: 270px; } .out .img li { position: absolute; left:0; top:0; display: none; } .out .num { position: absolute; bottom: 20px; left: 0; font-size:0px; text-align:center; width: 100%; } .out .num li { width: 20px; height: 20px; line-height:20px; border-radius:50%; background:#666; color: #fff; text-align:center; display: inline-block; font-size:16px; margin:0 3px; cursor: pointer; } .out .num li.active { background:#a00; } .out .btn { position:absolute; top: 50%; margin-top:-30px; width: 30px; height: 60px; line-height:60px; background:rgba(0, 0, 0, 0.5); font-size:40px; color: #fff; text-align:center; display: none; } .out .left { left: 0; } .out .right { right: 0; } .out:hover .btn { display: block; cursor: pointer; } </style>
HTML部分:
<body> <div class="out "> <ul class="img "> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="image/1.jpg " alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="image/2.jpg " alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="image/3.jpg " alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="image/4.jpg " alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="image/5.jpg " alt=" "> </a> </li> </ul> <ul class="num "> </ul> <div class="left btn "> <</div> <div class="right btn ">></div> </div> </body>
juery代碼實現圖片的自動輪播和 手動輪播效果
<script type="text/javascript" src="JS/jquery.js"></script> <script type="text/javascript"> $(function() { //代碼初始化 var size=$(".img li").size(); for (var i = 1; i <= size; i++) { var li="<li>"+i+"</li>"; $(".num").append(li); }; //手動控制輪播效果 $(".img li").eq(0).show(); $(".num li").eq(0).addClass("active"); $(".num li").mouseover(function() { $(this).addClass("active").siblings().removeClass("active"); var index = $(this).index(); i=index; $(".img li").eq(index).fadeIn(300).siblings().fadeOut(300); }) //自動 var i = 0; var t = setInterval(move, 1500); //核心向左的函數 function moveLeft() { i--; if (i == -1) { i = size-1; } $(".num li").eq(i).addClass("active").siblings().removeClass("active"); $(".img li").eq(i).fadeIn(300).siblings().fadeOut(300); } //核心向右的函數 function move() { i++; if (i == size) { i = 0; } $(".num li").eq(i).addClass("active").siblings().removeClass("active"); $(".img li").eq(i).fadeIn(300).siblings().fadeOut(300); } //定時器的開始與結束 $(".out").hover(function() { clearInterval(t); }, function() { t = setInterval(move, 1500) }) //左邊按鈕的點擊事件 $(".out .left").click(function() { moveLeft(); }) //右邊按鈕的點擊事件 $(".out .right").click(function() { move(); }) }) </script>
這里使用本站演示圖片,構建完整代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.jb51.net jQuery淡入淡出輪播圖</title> <style> * { padding: 0; margin:0; } ul { list-style: none; } .out { width: 640px; height: 270px; margin:50px auto; position: relative; } .out .img li { position: absolute; left:0; top:0; display: none; } .out .num { position: absolute; bottom: 20px; left: 0; font-size:0px; text-align:center; width: 100%; } .out .num li { width: 20px; height: 20px; line-height:20px; border-radius:50%; background:#666; color: #fff; text-align:center; display: inline-block; font-size:16px; margin:0 3px; cursor: pointer; } .out .num li.active { background:#a00; } .out .btn { position:absolute; top: 50%; margin-top:-30px; width: 30px; height: 60px; line-height:60px; background:rgba(0, 0, 0, 0.5); font-size:40px; color: #fff; text-align:center; display: none; } .out .left { left: 0; } .out .right { right: 0; } .out:hover .btn { display: block; cursor: pointer; } </style> </head> <body> <div class="out "> <ul class="img "> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/Guardians-of-the-Galaxy-Poster-High-Res.jpg" alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/Blade-Runner-poster-art-Harrison-Ford.jpg" alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/2017_alien_covenant_4k-5120x2880-1920x1080.jpg" alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/robocop-1987-wallpaper-2.jpg" alt=" "> </a> </li> <li> <a href="# " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/sJALsDXak4EehSg2F2y92rt5hPe.jpg" alt=" "> </a> </li> </ul> <ul class="num "> </ul> <div class="left btn "> <</div> <div class="right btn ">></div> </div> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function() { //代碼初始化 var size=$(".img li").size(); for (var i = 1; i <= size; i++) { var li="<li>"+i+"</li>"; $(".num").append(li); }; //手動控制輪播效果 $(".img li").eq(0).show(); $(".num li").eq(0).addClass("active"); $(".num li").mouseover(function() { $(this).addClass("active").siblings().removeClass("active"); var index = $(this).index(); i=index; $(".img li").eq(index).fadeIn(300).siblings().fadeOut(300); }) //自動 var i = 0; var t = setInterval(move, 1500); //核心向左的函數 function moveLeft() { i--; if (i == -1) { i = size-1; } $(".num li").eq(i).addClass("active").siblings().removeClass("active"); $(".img li").eq(i).fadeIn(300).siblings().fadeOut(300); } //核心向右的函數 function move() { i++; if (i == size) { i = 0; } $(".num li").eq(i).addClass("active").siblings().removeClass("active"); $(".img li").eq(i).fadeIn(300).siblings().fadeOut(300); } //定時器的開始與結束 $(".out").hover(function() { clearInterval(t); }, function() { t = setInterval(move, 1500) }) //左邊按鈕的點擊事件 $(".out .left").click(function() { moveLeft(); }) //右邊按鈕的點擊事件 $(".out .right").click(function() { move(); }) }) </script> </body> </html>
關于使用jQuery怎么實現一個淡入淡出圖片輪播效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。