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

首頁 > 開發(fā) > JS > 正文

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

2024-05-06 16:49:35
字體:
供稿:網(wǎng)友

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

本次輪播效果圖如下:

JS,圖片輪播

具有以下功能:1.自動播放(鼠標(biāo)進(jìn)入顯示區(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"      <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"      <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"      <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"      <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"    </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} 返回標(biāo)簽對象,方便獲取元素   */  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);    }  }  //設(shè)置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的值,此時頁面顯示第六個圖片,而用戶會認(rèn)為這是第一個圖,    //所以,如果用戶再次點擊按鈕,用戶應(yīng)該看到第二個圖片    if (pic == list.length - 1) {      //如何從第6個圖,跳轉(zhuǎn)到第一個圖      pic = 0;//先設(shè)置pic=0      ulObj.style.left = 0 + "px";//把ul的位置還原成開始的默認(rèn)位置    }    pic++;//立刻設(shè)置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 = "";      //第一個按鈕顏色設(shè)置上      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");    }    //當(dāng)前的pic索引對應(yīng)的按鈕設(shè)置顏色    olObj.children[pic].className = "current";  };  //設(shè)置任意的一個元素,移動到指定的目標(biāo)位置  function animate(element, target) {    clearInterval(element.timeId);    //定時器的id值存儲到對象的一個屬性中    element.timeId = setInterval(function () {      //獲取元素的當(dāng)前的位置,數(shù)字類型      var current = element.offsetLeft;      //每次移動的距離      var step = 10;      step = current < target ? step : -step;      //當(dāng)前移動到位置      current += step;      if (Math.abs(current - target) > Math.abs(step)) {        element.style.left = current + "px";      } else {        //清理定時器        clearInterval(element.timeId);        //直接到達(dá)目標(biāo)        element.style.left = target + "px";      }    }, 10);  }</script>

所有用圖片如下:

1.jpg

JS,圖片輪播

2.jpg

JS,圖片輪播

3.jpg

JS,圖片輪播

4.jpg

JS,圖片輪播

5.jpg

JS,圖片輪播

下面是完整的代碼:

<!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"      <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"      <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"      <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"      <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"    </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} 返回標(biāo)簽對象,方便獲取元素   */  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);    }  }  //設(shè)置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的值,此時頁面顯示第六個圖片,而用戶會認(rèn)為這是第一個圖,    //所以,如果用戶再次點擊按鈕,用戶應(yīng)該看到第二個圖片    if (pic == list.length - 1) {      //如何從第6個圖,跳轉(zhuǎn)到第一個圖      pic = 0;//先設(shè)置pic=0      ulObj.style.left = 0 + "px";//把ul的位置還原成開始的默認(rèn)位置    }    pic++;//立刻設(shè)置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 = "";      //第一個按鈕顏色設(shè)置上      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");    }    //當(dāng)前的pic索引對應(yīng)的按鈕設(shè)置顏色    olObj.children[pic].className = "current";  };  //設(shè)置任意的一個元素,移動到指定的目標(biāo)位置  function animate(element, target) {    clearInterval(element.timeId);    //定時器的id值存儲到對象的一個屬性中    element.timeId = setInterval(function () {      //獲取元素的當(dāng)前的位置,數(shù)字類型      var current = element.offsetLeft;      //每次移動的距離      var step = 10;      step = current < target ? step : -step;      //當(dāng)前移動到位置      current += step;      if (Math.abs(current - target) > Math.abs(step)) {        element.style.left = current + "px";      } else {        //清理定時器        clearInterval(element.timeId);        //直接到達(dá)目標(biāo)        element.style.left = target + "px";      }    }, 10);  }</script></body></html>

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


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 开江县| 胶州市| 固阳县| 蒙山县| 湟中县| 巧家县| 赣榆县| 瓮安县| 昔阳县| 永修县| 石景山区| 习水县| 天峻县| 门头沟区| 江口县| 饶阳县| 和政县| 浦县| 报价| 黎平县| 秦皇岛市| 印江| 巩留县| 滦南县| 丘北县| 岳西县| 崇左市| 隆化县| 克什克腾旗| 大洼县| 四平市| 本溪市| 乌兰浩特市| 万宁市| 全南县| 鄯善县| 霍山县| 萝北县| 龙口市| 松原市| 张家港市|