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

首頁 > 編程 > JavaScript > 正文

jquery+css實現簡單的圖片輪播效果

2019-11-19 15:53:36
字體:
來源:轉載
供稿:網友

開發過程中需要用到圖片輪播的插件,在網上找了幾個插件之后還是決定自己碼一個,比較簡潔的功能,以后說不定還會有用。

ps:

功能比較簡單,整個框并不能根據圖片的大小自動調節,這里所用的圖片是1170*500的,如果需要改成其他大小的圖片,自行修改.pic-list下img的寬度。

.pic-list中的寬度為整個橫幅的寬度,如果需要輪播的圖片數量很多,.pic-list的寬度應大于數量*單張寬度,

html

<div class="banner">    <!--第一張圖片為最后一張,用來做輪播連接使用,所以一開始顯示的第一章,應是第二張圖片,這里圖片的寬度為1170px,所以一開始left為-1170px,同理最后一張圖也為第一張圖。-->  <div class="pic-list" style="left: -1170px">    <img src="/static/img/4.jpg" alt="">    <img src="/static/img/1.jpg" alt="">    <img src="/static/img/2.jpg" alt="">    <img src="/static/img/3.jpg" alt="">    <img src="/static/img/4.jpg" alt="">    <img src="/static/img/1.jpg" alt="">  </div>  <div id="buttons">    <!-- 確保span的數量跟img數量一樣多,不包括第一張img和最后一張img-->    <span class='on'></span>    <span></span>    <span></span>    <span></span>  </div>  <a href="javascript:;" class="arrow" id="prev"><</a>  <a href="javascript:;" class="arrow" id="next">></a></div>

css

.banner{  width: 100%;  height: 500px;  overflow: hidden;  position: relative;}.banner a{  text-decoration: none;}.banner .pic-list{  width: 10000px;  height: 500px;  position: absolute;  z-index: 1;}.banner .pic-list img{  width: 1170px;  float: left;}#buttons{  position: absolute;  z-index: 2;  height: 10px;  bottom: 20px;  left: 550px;}#buttons span{  cursor: pointer;  float: left;  border: 1px solid #fff;  width: 10px;  height: 10px;  border-radius: 50%;  background: #333;  margin-right: 5px;}#buttons .on{  background: orange;}.arrow{  cursor: pointer;  line-height: 36px;  text-align: center;  font-size: 20px;  font-weight: bold;  width: 40px;  height: 40px;  position: absolute;  z-index: 2;  top: 200px;  background: rgba(0,0,0,0.5);  color: #fff;  display: none;}.banner:hover .arrow{display: block;}#prev{  left: 20px;}#next{  right:20px;}

js

$(document).ready(function(){  var picNum = 4;//圖片數量,根據實際修改  var picWidth = 1170;//圖片的寬度,根據實際修改  var picMaxWidth = -1 * picNum * picWidth;  var currentPic = 1;  var next = $('#next');  var prev = $('#prev');  var flag = false;  prev.on('click',function(){    if(!flag){      calPx(1170);      currentPic--;      if (currentPic < 1) {        currentPic = picNum;      }      $('#buttons span').eq(currentPic-1).addClass('on').siblings().removeClass('on');    }  });  next.on('click',function(){    if(!flag){      calPx(-1170);      currentPic++;      if (currentPic > picNum) {        currentPic = 1;      }      $('#buttons span').eq(currentPic-1).addClass('on').siblings().removeClass('on');    }  });  $('.banner').on('mouseover',function(){    stop();  }).on('mouseout',function(){    play();  })  function nextClick(){    next.click();  }  function play(){    setInt = setInterval(nextClick,2000);  }  function stop(){    clearInterval(setInt);  }  function calPx(leftPx){    flag = true;    var left = parseInt($('.pic-list').css('left'));    var newLeft = left+leftPx;    var time = 300;    var interval = 10;    var speed = leftPx/(time/interval);    function go(){      var left = parseInt($('.pic-list').css('left'));      if((speed < 0 && left > newLeft) || (speed > 0 && left < newLeft)){        $('.pic-list').css('left', (left + speed) + 'px');        setTimeout(go,interval);      }else{        flag = false;        if( newLeft > -1170){          newLeft = picMaxWidth;        }else if (newLeft < picMaxWidth ) {          newLeft = -1170;        }        $('.pic-list').css('left',newLeft + 'px');      }    }    go();  }  play();});

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彰化市| 三穗县| 大足县| 资兴市| 班玛县| 宜宾县| 启东市| 忻城县| 安顺市| 正宁县| 沐川县| 庆云县| 东港市| 克什克腾旗| 哈尔滨市| 开平市| 石家庄市| 尚义县| 荣昌县| 和政县| 嘉善县| 溧水县| 新建县| 望谟县| 贵港市| 白山市| 五华县| 农安县| 万山特区| 札达县| 武隆县| 大悟县| 巴里| 松滋市| 广灵县| 鄂伦春自治旗| 闵行区| 灯塔市| 凤翔县| 临朐县| 扶沟县|