jquery写出PC端轮播图实例


Posted in jQuery onJanuary 26, 2018

最近其他项目不是很忙,被安排给公司的官网项目做一个新的页面(之前没接触公司官网项目),其中有一个用到轮播图的地方,最开始想直接用swiper.js插件实现就好了,可是发现官网项目里之前都没有引入过swiper.js,后来想了想,就不引入它了,免得又得增加依次一次网络请求,项目里既然已经用到了jQuery,那就索性用jQuery写一个轮播图吧。

现在把自己写的轮播图这块代码单独拿出来,做一个小demo写在这里记录一下(demo中轮播图的图片网上随意找的)

实现的效果:

1、自动轮播(轮播时间间隔在js代码中自定义)

2、点击左右侧按钮,实现手动切换

3、底部小圆点根据切换图片的位置相应的显示active状态

4、鼠标经过轮播图区域,停止轮播,离开轮播图区域开始轮播

代码目录结果如下:

jquery写出PC端轮播图实例

一、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>
<div class="layout">
 <h2 style="text-align: center;">PC-jquery版轮播图</h2>
 <div class="slide" id="slide">
  <div 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>
  </div>


 <!--左右两侧的点击切换按钮-->
  <div class="arrow-box">
   <div class="arrow arrow-l" id="arrow_l">‹</div>
   <div class="arrow arrow-r" id="arrow_r">›</div>
  </div>
 </div>
</div>
<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)
})

四、效果图展示

jquery写出PC端轮播图实例

jquery写出PC端轮播图实例

jquery写出PC端轮播图实例

jQuery 相关文章推荐
jQuery插件FusionCharts实现的Marimekko图效果示例【附demo源码】
Mar 24 jQuery
jQuery EasyUI 组件加上“清除”功能实例详解
Apr 11 jQuery
jQuery+pjax简单示例汇总
Apr 21 jQuery
jquery实现一个全局计时器(商城可用)
Jun 30 jQuery
jquery实现限制textarea输入字数的方法
Sep 06 jQuery
springmvc接收jquery提交的数组数据代码分享
Oct 28 jQuery
jquery手机触屏滑动拼音字母城市选择器的实例代码
Dec 11 jQuery
jQuery基于Ajax实现读取XML数据功能示例
May 31 jQuery
学习jQuery中的noConflict()用法
Sep 28 jQuery
jQuery实现上下滚动公告栏详细代码
Nov 21 jQuery
jQuery判断自定义属性data-val用法示例
Jan 07 jQuery
jQuery实现滑动开关效果
Aug 02 jQuery
jquery根据name取得select选中的值实例(超简单)
Jan 25 #jQuery
用jquery获取select标签中选中的option值及文本的示例
Jan 25 #jQuery
jQuery zTree搜索-关键字查询 递归无限层功能实现代码
Jan 25 #jQuery
使用Ajax和Jquery配合数据库实现下拉框的二级联动的示例
Jan 25 #jQuery
解决Jquery下拉框数据动态获取的问题
Jan 25 #jQuery
jquery 获取索引值在一定范围的列表方法
Jan 25 #jQuery
jquery 输入框查找关键字并提亮颜色的实例代码
Jan 23 #jQuery
You might like
PHP mysql与mysqli事务使用说明 分享
2013/08/17 PHP
JQuery的Alert消息框插件使用介绍
2010/10/09 Javascript
jquery监听div内容的变化具体实现思路
2013/11/04 Javascript
Jquery焦点与失去焦点示例应用
2014/06/10 Javascript
Javascript获取表单名称(name)的方法
2015/04/02 Javascript
javascript实现数字倒计时特效
2016/03/30 Javascript
Javascript小技能总结(推荐)
2016/06/02 Javascript
Bootstrap Modal对话框如何在关闭时触发事件
2016/12/02 Javascript
Canvas实现放射线动画效果
2017/02/15 Javascript
BootStrap fileinput.js文件上传组件实例代码
2017/02/20 Javascript
Vue 2.0 服务端渲染入门介绍
2017/03/29 Javascript
webpack配置的最佳实践分享
2017/04/21 Javascript
利用forever和pm2部署node.js项目过程
2017/05/10 Javascript
Node.js编写CLI的实例详解
2017/05/17 Javascript
Vue中render函数的使用方法
2018/01/31 Javascript
nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG的方法
2018/11/01 NodeJs
vant-ui框架的一个bug(解决切换后onload不触发)
2020/11/11 Javascript
聊聊vue 中的v-on参数问题
2021/01/29 Vue.js
python中的内置函数getattr()介绍及示例
2014/07/20 Python
实例介绍Python中整型
2019/02/11 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
Pycharm保存不能自动同步到远程服务器的解决方法
2019/06/27 Python
python系列 文件操作的代码
2019/10/06 Python
解决tensorflow训练时内存持续增加并占满的问题
2020/01/19 Python
PyQt5实现画布小程序
2020/05/30 Python
基于Python爬取51cto博客页面信息过程解析
2020/08/25 Python
python实现计算器简易版
2020/12/17 Python
分享一个H5原生form表单的checkbox特效代码
2018/02/26 HTML / CSS
美国在线工具商店:Acme Tools
2018/06/26 全球购物
Fanatics法国官网:美国体育电商
2019/08/27 全球购物
应届毕业生应聘自荐信范文
2014/02/26 职场文书
公路局群众路线教育实践活动第一阶段工作汇报
2014/10/25 职场文书
2015年大学生村官工作总结
2015/04/21 职场文书
硕士学位申请报告
2015/05/15 职场文书
晚会主持人开场白台词
2015/05/28 职场文书
HTML速写之Emmet语法规则的实现
2021/04/07 HTML / CSS