【问题标题】:Dynamically adjust height of div with transition over image通过图像过渡动态调整 div 的高度
【发布时间】:2017-10-13 22:43:39
【问题描述】:

我有一个图像描述出现在悬停时。我通过更改 margin-top 值使其向上过渡。问题是我希望它能够获取尽可能多的文本,但仍然让所有内容都出现在图像上。

这是 JsBin 上的 demo

.myslider{
  overflow:hidden;
  position: relative;
  max-width:400px; 
}

.myslider img{
 width:100%;
 
}

.title{
  position:absolute;
  width: 100%;
  height:20px;
  padding-bottom: 2px;
  margin-top: -20px;
  transition: margin-top 200ms ease-in;
  
  background:black;
  opacity:0.6;  
}

.title-text{
  color:white;
  float left;
  padding-left:5px;
  
}

.nav-buttons{
 background: white;
 float:right;
 padding: 0px 5px 0px 5px;
 border: 1px solid black;
 margin: 0px 5px 0px 0px;
 cursor:pointer;
}

.myslider:hover .title{
  margin-top: 0px
}

.description {
  text-align: center;
  position:absolute;
  width: 100%;

  
  margin-top:0px;
  transition: margin-top 200ms ease-in; 
  color:white;
  background:black;
  opacity: 0.6;

}

.myslider:hover .description {
  margin-top: -20px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="myslider">
      <div class="title">
        <span class="title-text">Image 1 </span>
        <span class= "nav-buttons"> > </span>
        <span class= "nav-buttons"> < </span>
      </div> 
      <img src="http://placekitten.com/400/400">
      <div class="description">Image Caption: Should accept as much text as posisble. more text, more text, more text</div>
  </div>
</body>
</html>

【问题讨论】:

    标签: html css image css-position css-transforms


    【解决方案1】:

    不要使用margin-top 来制作动画,而是使用transform 属性来做这样的事情:

    .description {
      text-align: center;
      position: absolute;
      width: 100%;
      transition: transform 200ms ease-in;
      color: white;
      background: black;
      opacity: 0.6;
    }
    .myslider:hover .description {
      transform: translate(0, -100%);
    }
    

    然后就可以了!干杯!

    .myslider {
      overflow: hidden;
      position: relative;
      max-width: 400px;
    }
    .myslider img {
      width: 100%;
      display: block;
    }
    .title {
      position: absolute;
      width: 100%;
      height: 20px;
      padding-bottom: 2px;
      margin-top: -20px;
      transition: margin-top 200ms ease-in;
      background: black;
      opacity: 0.6;
    }
    .title-text {
      color: white;
      float left;
      padding-left: 5px;
    }
    .nav-buttons {
      background: white;
      float: right;
      padding: 0px 5px 0px 5px;
      border: 1px solid black;
      margin: 0px 5px 0px 0px;
      cursor: pointer;
    }
    .myslider:hover .title {
      margin-top: 0px
    }
    .description {
      text-align: center;
      position: absolute;
      width: 100%;
      transition: transform 200ms ease-in;
      color: white;
      background: black;
      opacity: 0.6;
    }
    .myslider:hover .description {
      transform: translate(0, -100%);
    }
    <div class="myslider">
      <div class="title">
        <span class="title-text">Image 1 </span>
        <span class="nav-buttons"> &gt; </span>
        <span class="nav-buttons"> &lt; </span>
      </div>
      <img src="http://placekitten.com/400/400">
      <div class="description">Image Caption: Should accept as much text as posisble. more text, more text, more text</div>
    </div>

    还做了一些小改动:

    1. 使用&amp;lt;&amp;gt; 获得正确的XHTML 语法

    2. 在图片中添加了display: block,以去除图片下方的小空白。

    【讨论】:

      【解决方案2】:

      不要使用margin-top。使用bottom 将其锚定到其父级的底部。

      【讨论】:

        猜你喜欢
        • 2014-03-08
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多