javascript原生百葉窗

來源:互聯網
上載者:User

標籤:i++   ble   doctype   pos   網頁   tle   order   red   border   

<!DOCTYPE html>
  <html>
  <head>
  <meta charset="UTF-8">
  <title></title>
  <style>
  .box{
  width:1200px;
  height:400px;
  border:1px solid black;
  position:relative;
  overflow:hidden;
  }
  .son{
  width:700px;
  height:400px;
  position:absolute;
   
  }
   
  </style>
  </head>
  <body>
  <div class="box">
  <div class="son" style=background:red; >1</div>
  <div class="son" style=background:pink;>2</div>
  <div class="son" style=background:green;>3</div>
  <div class="son" style=background:yellow;>4</div>
  </div>
  </body>
  <script type="text/javascript">
  var bo=document.querySelector(".box"); /*1.擷取大盒*/
  var so=document.querySelectorAll(".son"); /*2.擷取所有的子項目*/
  var ow=so[0].offsetWidth; /*子級擷取父級的寬度*/
  var ew=(bo.offsetWidth-ow)/(so.length-1);
  //父級減去子級的寬度除子集的長度,為什麼-1,因為有一個不用4-1;三個位置
  fun1();
  function fun1(){
  for(var i=0; i<so.length;i++){ /*for迴圈取子項目的長度*/
  so[i].index=i /*取子項目的對應位置的*/
  so[i].style.left=(i>0?ow+(i-1)*ew:0)+"px"; /*如果i>0父級的就執行(i-1)就是他跑的距離*ew距離是0;*/
  so[i].onmouseover=function(){
  fun1()
  for(var j=1;j<=this.index;j++) /*j=1,從1開始,這是所有是位置。j++;*/
  so[j].style.left=j*ew+"px"; /*輸出網頁子項目左邊的j*ew*/
  }
  }
  }
  </script>
  </html>

javascript原生百葉窗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.