国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jquery實現焦點輪播效果

2019-11-19 17:26:44
字體:
來源:轉載
供稿:網友

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();

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙江县| 高州市| 镇赉县| 沽源县| 贵南县| 长泰县| 宁南县| 大关县| 娄底市| 古浪县| 阿城市| 鄂尔多斯市| 特克斯县| 泸州市| 象州县| 汝南县| 辽源市| 西乡县| 吉水县| 万全县| 克山县| 桐乡市| 巴南区| 霍林郭勒市| 防城港市| 洛扎县| 静宁县| 什邡市| 黄山市| 庄河市| 财经| 延安市| 邢台县| 镇康县| 社旗县| 卢龙县| 临武县| 南澳县| 古田县| 安阳市| 平邑县|