jQuery实现轮播图效果demo

编程思路:

1. 首先是基础的布局,使用"子绝父相"等页面布局方法,将图片、左右按钮以及每张图片下方对应的标识小按钮安排的明明白白。
2. JS中在通过点击左右按钮来切换图片时,使用三个变量分别来表示当前显示的图片序号、点击上一张按钮时候显示的图片序号、点击下一张按钮时候显示的图片序号。
3. 在自动轮播的时候,通过使用定时器来改变当前显示的图片序号来控制轮播

具体代码:

HTML代码:

<div> <a href="#"><img src="https://www.jb51.net/img/phone_photo/p1.jpg" alt="0"></a> <a href="#"><img src="https://www.jb51.net/img/phone_photo/p2.jpg" alt="1"></a> <a href="#"><img src="https://www.jb51.net/img/phone_photo/p3.jpg" alt="2"></a> <a href="#"><img src="https://www.jb51.net/img/phone_photo/p4.jpg" alt="3"></a> <a href="#"><img src="https://www.jb51.net/img/phone_photo/p5.jpg" alt="4"></a> <a href="#"><img src="https://www.jb51.net/img/phone_photo/p6.jpg" alft="5"></a> <button> < </button> <button> > </button> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> </div>

CSS代码:

#slideShow{ width: 330px; height: 245px; background-color: #999999; text-align: center; left: 50%; margin-left: -165px; position: relative; } #slideShow .slide_pic{ position: absolute; left: 0; top: 0; } #slideShow .prev_one{ position: absolute; left: 0; top: 45%; } #slideShow .next_one{ position: absolute; right: 0; top: 45%; } #slideShow #mark_box{ position: absolute; bottom: 0; } #mark_box .mark{ width: 20px; height: 20px; border-radius: 20px; padding: 2px; text-align: center; line-height: 20px; background-color: red; float: left; list-style: none; margin: 10px 10px; cursor: pointer; } #mark_box .active_img{ background-color: green; }

Javascript代码:

$(function(){ var prev_mark=0; //点击上一张按钮时候的一个标志位 var next_mark=0; //点击下一张按钮时候的一个标志位 var cur_pic=$('.slide_pic').length-1; //当前图片的序号 $('.prev_one').click(function(){ prev_mark=cur_pic; //获取当前图片的序号 if(prev_mark === 0){ prev_mark=$('.slide_pic').length-1; } else{ prev_mark--; } cur_pic=prev_mark; $('.slide_pic').eq(cur_pic).fadeIn(500).siblings('.slide_pic').fadeOut(500); $('.mark').eq(cur_pic).addClass('active_img').siblings('.mark').removeClass('active_img'); }); $('.next_one').click(function(){ next_mark=cur_pic; if(next_mark === $('.slide_pic').length-1){ next_mark=0; } else{ next_mark++; } cur_pic=next_mark; $('.slide_pic').eq(cur_pic).fadeIn(500).siblings('.slide_pic').fadeOut(500); $('.mark').eq(cur_pic).addClass('active_img').siblings('.mark').removeClass('active_img'); }); // 轮播图下面的指示点 $.each($('.mark'),function(index,value){ $(value).click(function(){ cur_pic=this.innerHTML-1; $('.slide_pic').eq(cur_pic).fadeIn(500).siblings('.slide_pic').fadeOut(500); $(this).addClass('active_img').siblings('.mark').removeClass('active_img'); }); }); // 鼠标移入图片则停止轮播;鼠标移出图片则开始轮播 var slide_timer=setInterval(auto_slide,2000); $('#slideShow').mouseenter(function(){ clearInterval(slide_timer); }); $('#slideShow').mouseleave(function(){ slide_timer=setInterval(auto_slide,2000); }); // 自动轮播函数 function auto_slide(){ if(cur_pic === $('.slide_pic').length-1){ cur_pic =0; } else { cur_pic++; } $('.slide_pic').eq(cur_pic).fadeIn(500).siblings('.slide_pic').fadeOut(500); $('.mark').eq(cur_pic).addClass('active_img').siblings('.mark').removeClass('active_img'); } });

更多关于轮播图效果的专题,请点击下方链接查看学习

javascript图片轮播效果汇总

jquery图片轮播效果汇总

Bootstrap轮播特效汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e9268326413410336e47e48f6733fe01.html