jquery实现焦点轮播效果


Posted in Javascript onFebruary 23, 2017

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <link rel="stylesheet" href="./css/lunbo.css" rel="external nofollow" >
</head>
<body>
 <div id="banner">
  <ul class="img-ul"></ul>
  <ol class="index-ol"></ol>
 <div class="slide">
  <span class="prev"><</span>
  <span class="next">></span>
 </div>
 </div>
<script src="./js/jquery-1.11.3.js"></script>
<script src="./js/lunbo.js"></script>
</body>
</html>

css代码

div {
 width: 670px;
 height: 240px;
 position: relative;
 overflow: hidden;
}
div > ul,
div ol {
 list-style: none;
 position: absolute;
 margin: 0;
 padding: 0;
}
div > ul.img-ul,
div ol.img-ul {
 width: 3350px;
 height: 240px;
 z-index: 100;
}
div > ul.img-ul > li,
div ol.img-ul > li {
 float: left;
 width: 670px;
 height: 240px;
}
div > ul.index-ol,
div ol.index-ol {
 width: 205px;
 bottom: 10px;
 left: 217px;
 z-index: 1000;
}
div > ul.index-ol > li,
div ol.index-ol > li {
 float: left;
 cursor: pointer;
 margin-left: 20px;
 background: #000;
 color: #fff;
 border-radius: 50%;
 height: 20px;
 width: 20px;
 text-align: center;
 line-height: 20px;
}
div > ul.index-ol > li.active,
div ol.index-ol > li.active {
 background: red;
}
div > div.slide {
 z-index: 500;
 position: absolute;
 width: 670px;
 height: 240px;
 left: 0;
 top: 0;
}
div > div.slide > span {
 cursor: pointer;
 position: absolute;
 top: 100px;
 width: 30px;
 height: 60px;
 line-height: 60px;
 text-align: center;
 font-size: 20px;
 color: #fff;
 background: rgba(0, 0, 0, 0.2);
}
div > div.slide > span:nth-child(1) {
 left: 0;
}
div > div.slide > span:nth-child(2) {
 right: 0;
}

JavaScript代码

var arr=[
 {"img":"./images/banner_01.jpg"},
 {"img":"./images/banner_02.jpg"},
 {"img":"./images/banner_03.jpg"},
 {"img":"./images/banner_04.jpg"},
 {"img":"./images/banner_05.jpg"},
 ];
var lunbo={
 can:0, //判断
 ul_li:"",//图片列表
 ol_li:"",//数字列表
 width:"",//一个li的宽度
 interval:"",//定时器
 init:function(){
 console.log(this);
 this.view();
 this.view_index();
 $("ol.index-ol").children("li:eq(0)").addClass("active");
 this.width=$("ul.img-ul>li").width(); //670
 this.slide(); //这是左右箭头
 this.animation_index();//这是下标
 this.play(); //这是自动轮播
 this.mouse(); //这是鼠标滑入/滑出
 },
 mouse:function(){
 var _this=this;
 $("#banner").on({
  mouseenter:function(){
  _this.stop()
  },
  mouseleave:function(){
  _this.play();
  }
 })
 },
 play:function(){
 this.interval=setInterval(function(){
  var active_index= parseInt($("ol.index-ol>li.active").attr("data-index"));//得到当前激活向下标
  $("ol.index-ol>li").removeClass("active");
  $(this).addClass("active");
  this.animation(1);
  (active_index==4)&&(active_index=-1);
  $("ol.index-ol>li:eq("+(active_index+1)+")").addClass("active")
 }.bind(this),3000);
 },
 stop:function(){
 clearInterval(this.interval)
 this.interval=null;
 },
 animation_index:function(){//更新下标
 var _this=this;
 $("ol.index-ol>li").mouseenter(function(){//点击下标
  var active_index= $("ol.index-ol>li.active").attr("data-index");//得到当前激活向下标
  var index=$(this).attr("data-index");//得到当前下标;
  if(active_index==index){return;};
  $("ol.index-ol>li").removeClass("active");
  $(this).addClass("active");
  var end=index-active_index;
  _this.animation(-end)
 })
 },
 slide:function(){//点击左右箭头
 var _this=this;
 $("div.slide>span").click(function(){
  if(_this.can){return;};
  var active_index= parseInt($("ol.index-ol>li.active").attr("data-index"));//得到当前激活向下标
  $("ol.index-ol>li").removeClass("active");
  if(this.className=="prev"){
  _this.animation(1);
  (active_index==1)&&(active_index=5);
  $("ol.index-ol>li:eq("+(active_index-1)+")").addClass("active")
  }else{
  _this.animation(-1);
  (active_index==4)&&(active_index=-1);
  $("ol.index-ol>li:eq("+(active_index+1)+")").addClass("active")
  }
 })
 },
 view:function(){//更新图片
 for(var i=0;i<arr.length;i++){
  this.ul_li+="<li data-index="+i+"><img src="+arr[i].img+"></li>"
 }
 $("ul.img-ul").html(this.ul_li);
 this.ul_li="";
 },
 view_index:function(){//更新数字
 for(var i=0;i<arr.length;i++){
  this.ol_li+="<li data-index="+i+">"+(i+1)+"</li>"
 }
 $("ol.index-ol").html(this.ol_li);
 },
 animation:function(n){//做动画
 this.can=1;
 if(n<0){
  arr=arr.splice(arr.length+n,-n).concat(arr);
  this.view();
  $("ul.img-ul").css({"left":n*this.width+"px"});
  $("ul.img-ul").animate({"left":"0px"},1000,function(){
  this.can=0;
  }.bind(this));
 }else{
  $("ul.img-ul").animate({"left":-n*this.width+"px"},1000,function(){
  arr=arr.concat(arr.splice(0,n));
  this.view();
  $("ul.img-ul").css({"left":0+"px"});
  this.can=0;
  }.bind(this));
 }
 }
};
lunbo.init();

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
页面回到顶部的三种实现(锚标记,js)
Oct 01 Javascript
jQuery实现的简单提示信息插件
Dec 08 Javascript
jQuery中的each()详细介绍(推荐)
May 25 Javascript
jQuery EasyUI 入门必看
Jun 03 Javascript
jquery实现界面无刷新加载登陆注册
Jul 30 Javascript
学习vue.js表单控件绑定操作
Dec 05 Javascript
纯原生js实现table表格的增删
Jan 05 Javascript
微信小程序 点击控件后选中其它反选实例详解
Feb 21 Javascript
浅析Angular2子模块以及异步加载
Apr 24 Javascript
Angular2使用jQuery的方法教程
May 28 jQuery
javascript 玩转Date对象(实例讲解)
Jul 11 Javascript
ES6使用Set数据结构实现数组的交集、并集、差集功能示例
Oct 31 Javascript
SVG描边动画
Feb 23 #Javascript
Angular JS 生成动态二维码的方法
Feb 23 #Javascript
js实现楼层导航功能
Feb 23 #Javascript
jQuery点击头像上传并预览图片
Feb 23 #Javascript
jQuery事件与动画基础详解
Feb 23 #Javascript
bootstrap折叠调用collapse()后data-parent不生效的快速解决办法
Feb 23 #Javascript
解析Vue2.0双向绑定实现原理
Feb 23 #Javascript
You might like
Yii中render和renderPartial的区别
2014/09/03 PHP
PHP查询分页的实现代码
2017/06/09 PHP
PHP实现redis限制单ip、单用户的访问次数功能示例
2018/06/16 PHP
JavaScript对象模型-执行模型
2008/04/28 Javascript
传递参数的标准方法(jQuery.ajax)
2008/11/19 Javascript
jQuery图片播放8款精美插件分享
2013/02/17 Javascript
js实现页面转发功能示例代码
2013/08/05 Javascript
jsPDF导出pdf示例
2014/05/02 Javascript
深入分析JQuery和JavaScript的异同
2014/10/23 Javascript
js实现iGoogleDivDrag模块拖动层拖动特效的方法
2015/03/04 Javascript
window.setInterval()方法的定义和用法及offsetLeft与style.left的区别
2015/11/11 Javascript
javascript insertAfter()定义与用法示例
2016/07/25 Javascript
javascript基本数据类型及类型检测常用方法小结
2016/12/14 Javascript
深入理解jquery中extend的实现
2016/12/22 Javascript
vue-router路由简单案例介绍
2017/02/21 Javascript
老生常谈JavaScript面向对象基础与this指向问题
2017/10/16 Javascript
three.js中文文档学习之创建场景
2017/11/20 Javascript
基于Vue实现拖拽效果
2018/04/27 Javascript
JavaScript 判断对象中是否有某属性的常用方法
2018/06/14 Javascript
代码整洁之道(重构)
2018/10/25 Javascript
Python基于回溯法子集树模板实现8皇后问题
2017/09/01 Python
python3 实现一行输入,空格隔开的示例
2018/11/14 Python
Python Django框架实现应用添加logging日志操作示例
2019/05/17 Python
详解基于python-django框架的支付宝支付案例
2019/09/23 Python
浅谈spring boot 集成 log4j 解决与logback冲突的问题
2020/02/20 Python
Python3+selenium实现cookie免密登录的示例代码
2020/03/18 Python
django修改models重建数据库的操作
2020/03/31 Python
完美解决python针对hdfs上传和下载的问题
2020/06/05 Python
结束运行python的方法
2020/06/16 Python
pycharm导入源码的具体步骤
2020/08/04 Python
外语专业毕业生自荐信
2014/04/14 职场文书
2014年党课学习心得体会
2014/07/08 职场文书
科级干部群众路线教育实践活动对照检查材料思想汇报
2014/09/20 职场文书
2014年党建工作汇报材料
2014/10/27 职场文书
Python基础之元类详解
2021/04/29 Python
教你怎么用Python操作MySql数据库
2021/05/31 Python