【问题标题】:Scale the element (and background) but not the content缩放元素(和背景)但不缩放内容
【发布时间】:2016-07-24 01:35:30
【问题描述】:

我正在尝试将鼠标悬停在一个元素上,使其增大几个百分点,但我希望标题和里面的内容保持原样。如果我只是将scale(1.05) 添加到元素中,内容也会缩放。如果我尝试使用scale(0.95) 对内部元素进行反缩放,则大小将与原始元素相同,但位置将不同:

.container {
  display: block;
  width: 100%;
  padding: 50px;
}
.element {
  height: 562px;
  width: 590px;
  display: inline-block;
  position: relative;
  -webkit-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
  background-image: url(https://images.unsplash.com/photo-1451906278231-17b8ff0a8880?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925);
  background-size: cover;
  background-position: center center;
}
.title {
  font-size: 45px;
  line-height: 48px;
  bottom: 10%;
  position: absolute;
  margin: 0 25px;
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.14);
  font-weight: 700;
  font-family: sans-serif;
  -webkit-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
}
.element:hover {
  transform: scale(1.05);
  z-index: 10;
}
.element:hover .title {
  transform: scale(0.95);
}
<div class="container">
  <div class="element">
    <div class="title">This is the big title.</div>
  </div>
</div>

里面的内容是绝对放置的,所以我可能不得不通过指定bottomleft 属性来修复它,但我认为内部元素仍然会有一些过渡。

有没有办法解决这个问题?只是为了扩展元素而不影响内容?

【问题讨论】:

    标签: html css hover css-transitions css-transforms


    【解决方案1】:

    将背景移动到单独的图层。

    .container {
      position: relative;
      display: block;
      width: 300px;
      height: 300px;
      padding: 50px;
      margin: 50px auto;
    }
    .background {
      position: absolute;
      left: 0;
      top: 0;
      
      height: 100%;
      width: 100%;
      
      transition: all 300ms ease-in-out;
      background: url(https://images.unsplash.com/photo-1451906278231-17b8ff0a8880?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925) center center;
      background-size: cover;
    }
    .title {
      position: absolute;
      bottom: 10%;
      left: 0;
    
      margin: 0 25px;
      
      font-size: 45px;
      line-height: 48px;  
      color: #fff;
      font-weight: 700;
      font-family: sans-serif;
    }
    .container:hover .background {
      transform: scale(1.05);
    }
    <div class="container">
      <div class="background"></div>
      <div class="title">This is the big title.</div>
    </div>

    【讨论】:

      【解决方案2】:

      由于element 的孩子会受到影响,这可能是一个不错的选择,使用伪元素。

      .container {
        display: block;
        width: 100%;
        padding: 50px;
      }
      .element {
        position: relative;
        height: 562px;
        width: 590px;
        display: inline-block;
      }
      .element:before {
        content: ' ';
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        width: 100%;
        display: inline-block;
        -webkit-transition: all 300ms ease-in-out;
        transition: all 300ms ease-in-out;
        background-image: url(https://images.unsplash.com/photo-1451906278231-17b8ff0a8880?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925);
        background-size: cover;
        background-position: center center;
      }
      .title {
        font-size: 45px;
        line-height: 48px;
        bottom: 10%;
        position: absolute;
        margin: 0 25px;
        color: #fff;
        text-shadow: 0 2px 0 rgba(0, 0, 0, 0.14);
        font-weight: 700;
        font-family: sans-serif;
        -webkit-transition: all 300ms ease-in-out;
        transition: all 300ms ease-in-out;
      }
      .element:hover:before {
        transform: scale(1.05);
      }
      <div class="container">
        <div class="element">
          <div class="title">This is the big title.</div>
        </div>
      </div>

      如果您仍然需要 element 来添加其他元素等,这可能是第二种选择

      .container {
        display: block;
        width: 100%;
        padding: 50px;
      }
      .element {
        position: relative;
        height: 562px;
        width: 590px;
        display: inline-block;
      }
      .inner {
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        width: 100%;
        display: inline-block;
        -webkit-transition: all 300ms ease-in-out;
        transition: all 300ms ease-in-out;
        background-image: url(https://images.unsplash.com/photo-1451906278231-17b8ff0a8880?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925);
        background-size: cover;
        background-position: center center;
      }
      .title {
        font-size: 45px;
        line-height: 48px;
        bottom: 10%;
        position: absolute;
        margin: 0 25px;
        color: #fff;
        text-shadow: 0 2px 0 rgba(0, 0, 0, 0.14);
        font-weight: 700;
        font-family: sans-serif;
        -webkit-transition: all 300ms ease-in-out;
        transition: all 300ms ease-in-out;
      }
      .element:hover .inner {
        transform: scale(1.05);
      }
      <div class="container">
        <div class="element">
          <div class="inner"></div>
          <div class="title">This is the big title.</div>
        </div>
      </div>

      【讨论】:

      • 第二个必须要做,因为我不能将 url 放入伪元素中(我从 wordpress 中提取图像)。感谢您提供带有 bg 的内部元素的想法。
      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      相关资源
      最近更新 更多