您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

使用jquery做出PC端轮播图的实列详解

2024/6/8 9:21:01发布13次查看
这次给大家带来使用jquery做出pc端轮播图的实列详解,使用jquery做出pc端轮播图的注意事项有哪些,下面就是实战案例,一起来看一下。
最近其他项目不是很忙,被安排给公司的官网项目做一个新的页面(之前没接触公司官网项目),其中有一个用到轮播图的地方,最开始想直接用swiper.js插件实现就好了,可是发现官网项目里之前都没有引入过swiper.js,后来想了想,就不引入它了,免得又得增加依次一次网络请求,项目里既然已经用到了jquery,那就索性用jquery写一个轮播图吧。
现在把自己写的轮播图这块代码单独拿出来,做一个小demo写在这里记录一下(demo中轮播图的图片网上随意找的)
实现的效果:
1、自动轮播(轮播时间间隔在js代码中自定义)
2、点击左右侧按钮,实现手动切换
3、底部小圆点根据切换图片的位置相应的显示active状态
4、鼠标经过轮播图区域,停止轮播,离开轮播图区域开始轮播
代码目录结果如下:
一、index.html
注:这里以5张图片为例,页面上真正轮播展示给用户看到的是5张不同的图片,但是为了轮播效果的连贯性,所以在第一张图片前面添加上第五张图片,在第五张图片后面加上了第一张图片,所以demo结构里是7张图片,每张图片的尺寸必须都是一样的哦(这里宽高尺寸是720*350px)。
<!doctype html> <html lang="en"> <head>  <meta charset="utf-8">  <title>pc-jquery版轮播图</title>  <link rel="stylesheet" href="css/style.css" rel="external nofollow" > </head> <body> <p class="layout">  <h2 style="text-align: center;">pc-jquery版轮播图</h2>  <p class="slide" id="slide">   <p id="outer" class="outer">    <ul id="inner" class="inner">     <li><a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>图片-5</p><img src="images/slide-5.jpg"></a></li>     <li><a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>图片-1</p><img src="images/slide-1.jpg"></a></li>     <li><a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>图片-2</p><img src="images/slide-2.jpg"></a></li>     <li><a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>图片-3</p><img src="images/slide-3.jpg"></a></li>     <li><a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>图片-4</p><img src="images/slide-4.jpg"></a></li>     <li><a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>图片-5</p><img src="images/slide-5.jpg"></a></li>     <li><a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>图片-1</p><img src="images/slide-1.jpg"></a></li>    </ul> <!--底部小圆点-->    <ol class="dot" id="dot">     <li class="active"></li>     <li></li>     <li></li>     <li></li>     <li></li>    </ol>   </p>  <!--左右两侧的点击切换按钮-->   <p class="arrow-box">    <p class="arrow arrow-l" id="arrow_l">‹</p>    <p class="arrow arrow-r" id="arrow_r">›</p>   </p>  </p> </p> <script src="js/jquery.min.js"></script> <script src="js/index.js"></script> </body> </html>
二、style.css
* {  margin: 0;  padding: 0;  box-sizing: border-box; } .layout {  width: 1000px;  margin: 30px auto; } ul,ol,li {  list-style: none; } .slide {  position: relative;  width: 900px;  margin:auto; } .slide .outer {  position: relative;  margin: 30px auto;  width: 720px;  height: 400px;  overflow: hidden; } .slide .outer .inner {  width: 5040px;  height: 350px;  position: absolute;  left: -720px;  top: 0; } .slide .outer .inner li {  float: left;  height: 350px; } .slide .outer .inner li a {  display: block;  position: relative;  width: 100%;  height: 100%; } .slide .outer .inner li a p {  position: absolute;  left: 0;  bottom: 0;  color: #fff;  font-size: 18px;  width: 720px;  height: 80px;  line-height: 80px;  padding-left: 50px;  background: linear-gradient(180deg,rgba(0,0,0,0), rgba(0,0,0,0.5)); } .slide .outer .dot {  margin-top: 365px;  text-align: center; } .slide .outer .dot li {  height: 6px;  width: 6px;  border-radius: 3px;  background-color: #d2cbcb;  display: inline-block;  margin: 0 3px; } .slide .outer .dot li.active {  background-color: #6e5ca5; } .slide .arrow-box {  position: absolute;  width: 900px;  height: 60px;  top: 150px;  left: 0; } .slide .arrow-box .arrow {  width: 60px;  height: 60px;  line-height: 60px;  text-align: center;  border-radius: 30px;  background-color: #dde2e6;  font-size: 60px;  color: #999;  cursor: pointer; } .slide .arrow-box .arrow.arrow-l {  float: left; } .slide .arrow-box .arrow.arrow-r {  float: right; }
三、index.js
注:js代码中,每个变量均已给了注释。为了防止快速多次点击,而出现动画不停的现象,这里在每次切换图片的时候先调用stop(false,true)。但是注意在向左侧滚动的时候,滚动到最后一张图图片后,再次切换时就不要用stop(false,true),而是要瞬间定位到第一张图片(其实是dom结构中的第二张)的位置,同样,向右侧滚动时,当滚动到第一张图片后,再次切换时就不用stop(false,true),而是要瞬间定位到最后一张图片(其实是dom结构中的倒数第二张)的位置。
var interval = 3000;    //轮播间隔时间 var arrowl = $('#arrow_l');   //左侧箭头 var arrowr = $('#arrow_r');   //右侧箭头 var slidebox = $('#slide');   //轮播图区域 var innerbox = $('#inner');   //内层大盒子 var img = innerbox.children('li'); //每个图片 var dot = $('#dot');    //小圆点盒子 var imgw = $(img[0]).outerwidth(); //每个li标签的宽度 var imgcount = 5;     //总图片个数(不同图片的个数)(实际dom上是有7张) var i = 0;       //初始化为第0张图片 timer = null;      //定时器 //自动轮播 timer = setinterval(function () {  i++;  innerbox.stop(false, true).animate({'left':-i*imgw+'px'},300)  dot.find('li').removeclass('active').eq(i-1).addclass('active')  if(i > imgcount){   innerbox.animate({'left':-1*imgw+'px'},0);   dot.find('li').removeclass('active').eq(0).addclass('active')   i = 1;  } },interval) //点击右侧箭头,播放下一张 arrowr.click(function () {  i++;  innerbox.stop(false, true).animate({'left':-i*imgw+'px'},300)  dot.find('li').removeclass('active').eq(i-1).addclass('active')  if(i > imgcount){   innerbox.animate({'left':-1*imgw+'px'},0);   dot.find('li').removeclass('active').eq(0).addclass('active')   i = 1;  } }) //点击左侧箭头,播放上一张 arrowl.click(function () {  i--;  innerbox.stop(false, true).animate({'left':-i*imgw+'px'},300)  dot.find('li').removeclass('active').eq(i-1).addclass('active')  if(i < 1){ innerbox.animate({'left':-imgcount*imgw+'px'},0); dot.find('li').removeclass('active').eq(imgcount-1).addclass('active') i = imgcount; } }) //鼠标经过轮播图区域时,清除定时器,停止自动轮播 slidebox.mouseenter(function () { clearinterval(timer); }) //鼠标离开轮播图区域时,重新启动自动轮播 slidebox.mouseleave(function () { timer = setinterval(function () { i++; innerbox.stop(false, true).animate({'left':-i*imgw+'px'},300) dot.find('li').removeclass('active').eq(i-1).addclass('active') if(i > imgcount){    innerbox.animate({'left':-1*imgw+'px'},0);    dot.find('li').removeclass('active').eq(0).addclass('active')    i = 1;   }  },interval) })
四、效果图展示
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js动态操作表格
用name取select值
微信小程序的多文件下载封装使用
js中object对象的原型的使用方法
以上就是使用jquery做出pc端轮播图的实列详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product