簡體   English   中英

jQuery:將元素懸停在一個元素上時如何為多個元素設置動畫?

[英]jquery: how to animate multiple elements when hovered over one element?

我有以下HTML代碼。

<div id="table1"></div>
  <script>
    var arr = [
    ["s1","s2","s3"],
    ["sk1","sk2","sk3"],
    ["skl1","skl2"]
    ];
    table_generator("table1", arr);
  </script>

現在要將表添加到該div中,我有以下javascript代碼。

<script>
      function table_generator(body_table_name, s_array) {

        var body_table = document.getElementById(body_table_name);

        //Create table section
        var table = document.createElement("TABLE");
        table.setAttribute("id", "table_"+body_table_name);

        //Append to body div
        body_table.appendChild(table);

        //Add rows
        var row_length = s_array.length;
        for(var i = 0; i <row_length;  i++)
        {
           row(s_array[i], "row"+i, "table_"+body_table_name);
        }

        assign_height_width_hover_effects();
    }
</script>

要在表格中添加行,我具有此javascript函數

<script>
  function row(td_array, row_name, table_name) {
    //Create row
    var tr = document.createElement("TR");
    tr.setAttribute("id", "tr_"+row_name);
    document.getElementById(table_name).appendChild(tr);

    //Create the tds
    for(var i = 0; i<td_array.length; i++)
    {
      var td = document.createElement("TD");
      td.setAttribute("id", "td_"+td_array[i]);
      tr.appendChild(td);

      //Add td styling
      td.style.transition = "all 0.5s ease-in-out";

      //Add text to cell
      var name = document.createElement("SPAN");
      name.setAttribute("id", "name_"+td_array[i]);
      name.innerHTML = td_array[i];
      td.appendChild(name);

      //Add skill info
      var info = document.createElement("SPAN");
      info.setAttribute("id", "info_"+td_array[i]);
      info.innerHTML = "Some info here";
      name.appendChild(info);

      //Add styling to this info
      info.style.position = "absolute";
      info.style.width = "inherit";
      info.style.height = "inherit";
      info.style.overflow = "hidden";
      info.style.opacity = 0;
      info.style.transition = "all 1s ease-in-out 0.4s";
      info.style.transform = "scale(0)";

      //Calculate cell width
      var width = (td.clientWidth + 1);
      if (width > max_width)
      {
        max_width = width;
      }

      //Push it in array
      main_array.push(td_array[i]);
    }
  }
</script>

最后,為表格單元格提供高度和寬度並添加懸停效果,我調用以下函數

<script>
  function assign_height_width_hover_effects() {
    //Assigh height and width to all cells in that row
    for(var i = 0; i<main_array.length; i++)
    {
      var td = document.getElementById("td_"+main_array[i]);
      var name = document.getElementById("name_"+main_array[i]);
      var info = document.getElementById("info_"+main_array[i]);

      //Add styling to td
      td.style.width = max_width+"px";
      td.style.height = max_width+"px";
      td.style.textAlign = "center";    
      td.style.boxShadow = "rgb(0, 0, 0) 0px 0px 0px 0px inset";

      //Here is where I need help
      $(td).hover(
        function(){
          $(this).stop().animate({boxShadow: '0 0 '+max_width+'px'}, 'fast');
          //$(this).stop().animate({"opacity": 0});
        }, 
        function(){
          $(this).stop().animate({boxShadow: '0 0 0'}, 'fast');
          //$(this).stop().animate({"opacity": 1});
    }
    );
    }
  }
</script>

基本上,我要設置以下動畫:

  • 當鼠標進入td表單元格時:

    • td單元的框陰影可動畫成某些顏色
    • 在td單元格內ID為“ name”的span元素逐漸消失,我使用不透明度0來做到這一點。
    • 出現在td單元格中的id為“ info”的span元素,我使用不透明度1來做到這一點。
  • 當鼠標離開td表單元格時:

    • td單元的框陰影可以恢復正常狀態。 那是要走了
    • 在td單元中出現的id為“ name”的span元素將顯示回來,我使用不透明度1來做到這一點。
    • 在td單元內部的id為“ info”的span元素逐漸消失,我使用不透明度0來做到這一點。

現在,我可以單獨對框陰影進行動畫處理,或者對名稱元素進行淡入或淡出動畫,但不能同時對兩者進行動畫處理。

您僅需使用CSS就能輕松做到這一點

 td { transition: box-shadow 1s ease 0s; box-shadow: 5px 5px 5px #000; padding: 20px; } td:hover { box-shadow: 5px 5px 5px #f00; } td .name { transition: opacity 1s ease 0s; opacity: 1; } td:hover .name { opacity: 0; } td .info { transition: opacity 1s ease 0s; opacity: 0; } td:hover .info { opacity: 1; } 
 <table> <tbody> <tr> <td>1-1</td> <td>1-2</td> <td>1-3</td> </tr> <tr> <td>2-1</td> <td>2-2</td> <td>2-3</td> </tr> </tbody> </table> 

我確實將名稱和信息的選擇器更改為類而不是ID,因為ID應該是唯一的。

注意:這是一個簡單的示例。 您將要在td之前添加另一個特定的選擇器,這樣它就不會應用於您的頁面/站點上的每個表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM