最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS實現(xiàn)圖片輪播效果實例詳解【可自動和手動】

 更新時間:2019年04月04日 11:12:32   作者:呵呵到天亮  
這篇文章主要介紹了JS實現(xiàn)圖片輪播效果,結(jié)合完整實例形式分析了javascript可自動和手動輪播圖的原理、布局與輪播功能相關實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了JS實現(xiàn)圖片輪播效果。分享給大家供大家參考,具體如下:

本次輪播效果圖如下:

具有以下功能:1.自動播放(鼠標進入顯示區(qū)域時停止播放) 2.左右焦點切換  3.底下小按鈕切換

以下為實現(xiàn)代碼:

首先是html代碼:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>最簡單的輪播效果</title>
</head>
<body>
<div class="box" id="box">
  <div class="inner">
    <!--輪播圖-->
    <ul>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/1.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/2.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/3.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/4.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/5.jpg" alt=""></a></li>
    </ul>
    <ol class="bar">
      小按鈕數(shù)量無法確定,由js動態(tài)生成
    </ol>
    <!--左右焦點-->
    <div id="arr">
       <span id="left"> <</span>
       <span id="right">></span>
    </div>

  </div>
</div>

</body>
</html>

接下來是css樣式:

<style>
    * {
      margin: 0;
      padding: 0
    }
    .box {
      width: 500px;
      height: 300px;
      border: 1px solid #ccc;
      margin: 100px auto;
      padding: 5px;

    }
    .inner{
      width: 500px;
      height: 300px;
      position: relative;
      overflow: hidden;
    }
    .inner img{
      width: 500px;
      height: 300px;
      vertical-align: top
    }
    ul {
      width: 1000%;
      position: absolute;
      list-style: none;
      left:0;
      top: 0;
    }
    .inner li{
      float: left;

    }

    ol {
      position: absolute;
      height: 20px;
      right: 20px;
      bottom: 20px;
      text-align: center;
      padding: 5px;
    }
    ol li{
      display: inline-block;
      width: 20px;
      height: 20px;
      line-height: 20px;
      background-color: #fff;
      margin: 5px;
      cursor: pointer;

    }
    ol .current{
      background-color: red;
    }
    #arr{
      display: none;
    }
    #arr span{
      width: 40px;
      height: 40px;
      position: absolute;
      left: 5px;
      top: 50%;
      margin-top: -20px;
      background: #fff;
      cursor: pointer;
      line-height: 40px;
      text-align: center;
      font-weight: bold;
      font-family: '黑體';
      font-size: 30px;
      color: #000;
      opacity: 0.5;
      border: 1px solid #fff;
    }
    #arr #right {
      right: 5px;
      left: auto;
    }

第三部分是最主要的js代碼:

<script>
  /**
   *
   * @param id 傳入元素的id
   * @returns {HTMLElement | null} 返回標簽對象,方便獲取元素
   */
  function my$(id) {
    return document.getElementById(id);
  }

  //獲取各元素,方便操作
  var box=my$("box");
  var inner=box.children[0];
  var ulObj=inner.children[0];
  var list=ulObj.children;
  var olObj=inner.children[1];
  var arr=my$("arr");
  var imgWidth=inner.offsetWidth;
  var right=my$("right");
  var pic=0;
  //根據(jù)li個數(shù),創(chuàng)建小按鈕
  for(var i=0;i<list.length;i++){
    var liObj=document.createElement("li");

    olObj.appendChild(liObj);
    liObj.innerText=(i+1);
    liObj.setAttribute("index",i);

    //為按鈕注冊mouseover事件
    liObj.onmouseover=function () {
      //先清除所有按鈕的樣式

      for (var j=0;j<olObj.children.length;j++){
        olObj.children[j].removeAttribute("class");
      }
      this.className="current";
      pic=this.getAttribute("index");
      animate(ulObj,-pic*imgWidth);
    }

  }


  //設置ol中第一個li有背景顏色
  olObj.children[0].className = "current";
  //克隆一個ul中第一個li,加入到ul中的最后=====克隆
  ulObj.appendChild(ulObj.children[0].cloneNode(true));

  var timeId=setInterval(onmouseclickHandle,1000);
  //左右焦點實現(xiàn)點擊切換圖片功能
  box.onmouseover=function () {
    arr.style.display="block";
    clearInterval(timeId);
  };
  box.onmouseout=function () {
    arr.style.display="none";
    timeId=setInterval(onmouseclickHandle,1000);
  };

  right.onclick=onmouseclickHandle;
  function onmouseclickHandle() {
    //如果pic的值是5,恰巧是ul中l(wèi)i的個數(shù)-1的值,此時頁面顯示第六個圖片,而用戶會認為這是第一個圖,
    //所以,如果用戶再次點擊按鈕,用戶應該看到第二個圖片
    if (pic == list.length - 1) {
      //如何從第6個圖,跳轉(zhuǎn)到第一個圖
      pic = 0;//先設置pic=0
      ulObj.style.left = 0 + "px";//把ul的位置還原成開始的默認位置
    }
    pic++;//立刻設置pic加1,那么此時用戶就會看到第二個圖片了
    animate(ulObj, -pic * imgWidth);//pic從0的值加1之后,pic的值是1,然后ul移動出去一個圖片
    //如果pic==5說明,此時顯示第6個圖(內(nèi)容是第一張圖片),第一個小按鈕有顏色,
    if (pic == list.length - 1) {
      //第五個按鈕顏色干掉
      olObj.children[olObj.children.length - 1].className = "";
      //第一個按鈕顏色設置上
      olObj.children[0].className = "current";
    } else {
      //干掉所有的小按鈕的背景顏色
      for (var i = 0; i < olObj.children.length; i++) {
        olObj.children[i].removeAttribute("class");
      }
      olObj.children[pic].className = "current";
    }
  }
  left.onclick=function () {
    if (pic==0){
      pic=list.length-1;
      ulObj.style.left=-pic*imgWidth+"px";
    }
    pic--;
    animate(ulObj,-pic*imgWidth);
    for (var i = 0; i < olObj.children.length; i++) {
      olObj.children[i].removeAttribute("class");
    }
    //當前的pic索引對應的按鈕設置顏色
    olObj.children[pic].className = "current";
  };

  //設置任意的一個元素,移動到指定的目標位置
  function animate(element, target) {
    clearInterval(element.timeId);
    //定時器的id值存儲到對象的一個屬性中
    element.timeId = setInterval(function () {
      //獲取元素的當前的位置,數(shù)字類型
      var current = element.offsetLeft;
      //每次移動的距離
      var step = 10;
      step = current < target ? step : -step;
      //當前移動到位置
      current += step;
      if (Math.abs(current - target) > Math.abs(step)) {
        element.style.left = current + "px";
      } else {
        //清理定時器
        clearInterval(element.timeId);
        //直接到達目標
        element.style.left = target + "px";
      }
    }, 10);
  }
</script>

所有用圖片如下:

1.jpg


2.jpg


3.jpg


4.jpg


5.jpg


下面是完整的代碼:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>最簡單的輪播效果</title>
  <style>
    * {
      margin: 0;
      padding: 0
    }
    .box {
      width: 500px;
      height: 300px;
      border: 1px solid #ccc;
      margin: 100px auto;
      padding: 5px;

    }
    .inner{
      width: 500px;
      height: 300px;
      position: relative;
      overflow: hidden;
    }
    .inner img{
      width: 500px;
      height: 300px;
      vertical-align: top
    }
    ul {
      width: 1000%;
      position: absolute;
      list-style: none;
      left:0;
      top: 0;
    }
    .inner li{
      float: left;

    }

    ol {
      position: absolute;
      height: 20px;
      right: 20px;
      bottom: 20px;
      text-align: center;
      padding: 5px;
    }
    ol li{
      display: inline-block;
      width: 20px;
      height: 20px;
      line-height: 20px;
      background-color: #fff;
      margin: 5px;
      cursor: pointer;

    }
    ol .current{
      background-color: red;
    }
    #arr{
      display: none;
    }
    #arr span{
      width: 40px;
      height: 40px;
      position: absolute;
      left: 5px;
      top: 50%;
      margin-top: -20px;
      background: #fff;
      cursor: pointer;
      line-height: 40px;
      text-align: center;
      font-weight: bold;
      font-family: '黑體';
      font-size: 30px;
      color: #000;
      opacity: 0.5;
      border: 1px solid #fff;
    }
    #arr #right {
      right: 5px;
      left: auto;
    }
  </style>
</head>
<body>
<div class="box" id="box">
  <div class="inner">
    <!--輪播圖-->
    <ul>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/1.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/2.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/3.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/4.jpg" alt=""></a></li>
      <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="images/5.jpg" alt=""></a></li>

    </ul>

    <ol class="bar">

    </ol>
    <!--左右焦點-->
    <div id="arr">
          <span id="left">
            <
          </span>
      <span id="right">
            >
          </span>
    </div>

  </div>
</div>
<script>
  /**
   *
   * @param id 傳入元素的id
   * @returns {HTMLElement | null} 返回標簽對象,方便獲取元素
   */
  function my$(id) {
    return document.getElementById(id);
  }

  //獲取各元素,方便操作
  var box=my$("box");
  var inner=box.children[0];
  var ulObj=inner.children[0];
  var list=ulObj.children;
  var olObj=inner.children[1];
  var arr=my$("arr");
  var imgWidth=inner.offsetWidth;
  var right=my$("right");
  var pic=0;
  //根據(jù)li個數(shù),創(chuàng)建小按鈕
  for(var i=0;i<list.length;i++){
    var liObj=document.createElement("li");

    olObj.appendChild(liObj);
    liObj.innerText=(i+1);
    liObj.setAttribute("index",i);

    //為按鈕注冊mouseover事件
    liObj.onmouseover=function () {
      //先清除所有按鈕的樣式

      for (var j=0;j<olObj.children.length;j++){
        olObj.children[j].removeAttribute("class");
      }
      this.className="current";
      pic=this.getAttribute("index");
      animate(ulObj,-pic*imgWidth);
    }

  }


  //設置ol中第一個li有背景顏色
  olObj.children[0].className = "current";
  //克隆一個ul中第一個li,加入到ul中的最后=====克隆
  ulObj.appendChild(ulObj.children[0].cloneNode(true));

  var timeId=setInterval(onmouseclickHandle,1000);
  //左右焦點實現(xiàn)點擊切換圖片功能
  box.onmouseover=function () {
    arr.style.display="block";
    clearInterval(timeId);
  };
  box.onmouseout=function () {
    arr.style.display="none";
    timeId=setInterval(onmouseclickHandle,1000);
  };

  right.onclick=onmouseclickHandle;
  function onmouseclickHandle() {
    //如果pic的值是5,恰巧是ul中l(wèi)i的個數(shù)-1的值,此時頁面顯示第六個圖片,而用戶會認為這是第一個圖,
    //所以,如果用戶再次點擊按鈕,用戶應該看到第二個圖片
    if (pic == list.length - 1) {
      //如何從第6個圖,跳轉(zhuǎn)到第一個圖
      pic = 0;//先設置pic=0
      ulObj.style.left = 0 + "px";//把ul的位置還原成開始的默認位置
    }
    pic++;//立刻設置pic加1,那么此時用戶就會看到第二個圖片了
    animate(ulObj, -pic * imgWidth);//pic從0的值加1之后,pic的值是1,然后ul移動出去一個圖片
    //如果pic==5說明,此時顯示第6個圖(內(nèi)容是第一張圖片),第一個小按鈕有顏色,
    if (pic == list.length - 1) {
      //第五個按鈕顏色干掉
      olObj.children[olObj.children.length - 1].className = "";
      //第一個按鈕顏色設置上
      olObj.children[0].className = "current";
    } else {
      //干掉所有的小按鈕的背景顏色
      for (var i = 0; i < olObj.children.length; i++) {
        olObj.children[i].removeAttribute("class");
      }
      olObj.children[pic].className = "current";
    }
  }
  left.onclick=function () {
    if (pic==0){
      pic=list.length-1;
      ulObj.style.left=-pic*imgWidth+"px";
    }
    pic--;
    animate(ulObj,-pic*imgWidth);
    for (var i = 0; i < olObj.children.length; i++) {
      olObj.children[i].removeAttribute("class");
    }
    //當前的pic索引對應的按鈕設置顏色
    olObj.children[pic].className = "current";
  };

  //設置任意的一個元素,移動到指定的目標位置
  function animate(element, target) {
    clearInterval(element.timeId);
    //定時器的id值存儲到對象的一個屬性中
    element.timeId = setInterval(function () {
      //獲取元素的當前的位置,數(shù)字類型
      var current = element.offsetLeft;
      //每次移動的距離
      var step = 10;
      step = current < target ? step : -step;
      //當前移動到位置
      current += step;
      if (Math.abs(current - target) > Math.abs(step)) {
        element.style.left = current + "px";
      } else {
        //清理定時器
        clearInterval(element.timeId);
        //直接到達目標
        element.style.left = target + "px";
      }
    }, 10);
  }
</script>
</body>
</html>

更多關于JavaScript相關內(nèi)容感興趣的讀者可查看本站專題:《JavaScript圖片操作技巧大全》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript運動效果與技巧匯總》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學運算用法總結(jié)

希望本文所述對大家JavaScript程序設計有所幫助。

相關文章

  • 《javascript設計模式》學習筆記三:Javascript面向?qū)ο蟪绦蛟O計單例模式原理與實現(xiàn)方法分析

    《javascript設計模式》學習筆記三:Javascript面向?qū)ο蟪绦蛟O計單例模式原理與實現(xiàn)方法分析

    這篇文章主要介紹了Javascript面向?qū)ο蟪绦蛟O計單例模式原理與實現(xiàn)方法,結(jié)合實例形式分析了《javascript設計模式》中Javascript面向?qū)ο髥卫J较嚓P概念、原理、用法及操作注意事項,需要的朋友可以參考下
    2020-04-04
  • 移到這里,就會自動點擊

    移到這里,就會自動點擊

    移到這里,就會自動點擊...
    2006-08-08
  • AutoJs實現(xiàn)刷寶短視頻的思路詳解

    AutoJs實現(xiàn)刷寶短視頻的思路詳解

    這篇文章主要介紹了AutoJs實現(xiàn)刷寶短視頻的思路詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • js獲取RadioButtonList的Value/Text及選中值等信息實現(xiàn)代碼

    js獲取RadioButtonList的Value/Text及選中值等信息實現(xiàn)代碼

    RadioButtonList的Value,Text及選中值等信息想必有很多的朋友都想獲取到,接下來將為你介紹下如何使用js獲取,代碼很詳細,感興趣的你可以參考下,或許對你有所幫助
    2013-03-03
  • JavaScript定義函數(shù)_動力節(jié)點Java學院整理

    JavaScript定義函數(shù)_動力節(jié)點Java學院整理

    這篇文章主要介紹了JavaScript定義函數(shù)的相關資料,需要的朋友可以參考下
    2017-06-06
  • JavaScript從數(shù)組的indexOf()深入之Object的Property機制

    JavaScript從數(shù)組的indexOf()深入之Object的Property機制

    這篇文章主要介紹了JavaScript從數(shù)組的indexOf()深入——Object的Property機制的相關資料,需要的朋友可以參考下
    2016-05-05
  • JavaScript+html5 canvas繪制繽紛多彩的三角形效果完整實例

    JavaScript+html5 canvas繪制繽紛多彩的三角形效果完整實例

    這篇文章主要介紹了JavaScript+html5 canvas繪制繽紛多彩的三角形效果,以完整實例形式分析了html5的canvas繪制圖形的相關技巧,需要的朋友可以參考下
    2016-01-01
  • 最新評論

    仪征市| 日照市| 千阳县| 广河县| 甘德县| 贵港市| 灵寿县| 砀山县| 南宁市| 旬邑县| 泊头市| 南汇区| 鄂伦春自治旗| 大渡口区| 扎鲁特旗| 高尔夫| 响水县| 塔河县| 桐庐县| 始兴县| 金溪县| 莱西市| 新邵县| 昌吉市| 三穗县| 汝南县| 阳高县| 读书| 南丹县| 四子王旗| 红河县| 通渭县| 望城县| 万全县| 桐乡市| 红桥区| 漯河市| 都江堰市| 湄潭县| 古交市| 阿拉善左旗|