Jquery背景滑下来

时间:2016-08-24 00:56:46

标签: javascript jquery

我想知道是否可以更改使div跟随其表格单元格的大小?目前我只能在另一个div上面显示一些内容。这就是我到目前为止所拥有的。

https://jsfiddle.net/amosangyongjian/mmqasf5t/54/

$(document).ready(function(){
    $("td").hover(function(){
    $(this).find(".expand").slideDown("slow");
  },function(){
    $(this).find(".expand").slideUp("slow");
  });
});

1 个答案:

答案 0 :(得分:2)

jsFiddle example

td {
  width:100px;
  height:100px;
  position:relative; /* ADD THIS */
}
.expand {
  display:none;
  position:absolute;
  top:0; left:0; width:100%; height:100%; /* ADD */
  z-index:1;
  background:red;
  /*height:auto; width:100%;  REMOVE */
  overflow:hidden;
}

.static {
  position:absolute;
  z-index:0;
  background:yellow;
  text-align:center;
}

这是一个更清洁的解决方案,在HTML,CSS,jQuery中进行了一些小的改动

jQuery(function( $ ) {

  $("td").hover(function() {
    $(this).find(".expand").stop().slideToggle("slow");
  });

});
td {
  width:100px;
  height:100px;
  position:relative;
}
.expand {
  display:none;
  position:absolute;
  top:0; left:0; width:100%; height:100%;
  background:red;
  overflow:hidden;
}
.static {
  position:absolute;
  background:yellow;
  text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1" id="tableclass">
  <tr>
    <td>
      <div class="static">Hello1</div>
      <div class="expand">Expanded</div>
    </td>
    <td>
      <div class="static">Hello2</div>
      <div class="expand">Expanded</div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="static">Hello3</div>
      <div class="expand">Expanded</div>
    </td>
    <td>
      <div class="static">Hello4</div>
      <div class="expand">Expanded</div>
    </td>
  </tr>
</table>

这是另一个使用完全没有JavaScript 的示例 但是从transitionheight:0;

的CSS3 height:100%;

td {
  width:100px;
  height:100px;
  position:relative;
}
.static {
  position:absolute;
  background:yellow;
  text-align:center;
}
.expand {
  position:absolute;
  overflow:hidden;
  top:0; left:0; width:100%; height:0;
  background:red;
  transition: 0.5s;
}
td:hover .expand{
  height:100%;
}
<table border="1" id="tableclass">
  <tr>
    <td>
      <div class="static">Hello1</div>
      <div class="expand">Expanded</div>
    </td>
    <td>
      <div class="static">Hello2</div>
      <div class="expand">Expanded</div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="static">Hello3</div>
      <div class="expand">Expanded</div>
    </td>
    <td>
      <div class="static">Hello4</div>
      <div class="expand">Expanded</div>
    </td>
  </tr>
</table>