JS---案例:手風琴 (利用封裝好的動畫函數)

案例:手風琴

   
封裝好的動畫函數在common.js裏面
    //function getStyle(element, attr) {...}
    //function animate(element, json, fn) {...}

手風琴設置的是背景圖嗎,backgroudImage="url(image/***.jpg)";html

當鼠標進入的時候,當前的寬度變爲800,其他的遍歷並設置爲100;json

鼠標離開的時候,全部li寬度變爲400函數

<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    ul {
      list-style: none;
    }

    * {
      margin: 0;
      padding: 0;
    }

    div {
      width: 1150px;
      height: 400px;
      margin: 50px auto;
      border: 1px solid red;
      overflow: hidden;
    }

    div li {
      width: 240px;
      height: 400px;
      float: left;
    }

    div ul {
      width: 1300px;
    }
  </style>
</head>

<body>
  <div id="box">
    <ul>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>
  <script src="common.js"></script>
  <script>

    //下面2個封裝好的函數在common.js裏面
    //function getStyle(element, attr) {
    //function animate(element, json, fn) {

    //先獲取全部的li標籤
    var list = my$("box").getElementsByTagName("li");
    for (var i = 0; i < list.length; i++) {
      list[i].style.backgroundImage = "url(images/" + (i + 1) + ".jpg)";
      //鼠標進入
      list[i].onmouseover = mouseoverHandle;
      //鼠標離開
      list[i].onmouseout = mouseoutHandle;
    }
    //進入和離開函數
    function mouseoverHandle() {
      for (var j = 0; j < list.length; j++) {
        animate(list[j], { "width": 100 });//動畫效果
      }
      animate(this, { "width": 800 });

    }
    function mouseoutHandle() {
      for (var j = 0; j < list.length; j++) {
        animate(list[j], { "width": 240 });//動畫效果
      }
    }

  </script>

</body>

</html>
相關文章
相關標籤/搜索