【问题标题】:css background zoom with fade in effect具有淡入效果的 CSS 背景缩放
【发布时间】:2016-12-27 15:11:52
【问题描述】:

使用下面的放大效果从这里https://codepen.io/dragoeco/pen/ibrzK

需要一些帮助来让背景图像淡入并在悬停时产生缩放效果,就像这里的 http://fantasy.co/work 保持前景徽标相同。

.image-box{
  width:300px;
  overflow:hidden;
}
.image {
	width:300px;
  height:200px;
	background: url("http://lorempixel.com/300/200");
	background-position:center;
	transition: all 1s ease;
  -moz-transition: all 1s ease;
  -ms-transition: all 1s ease;
  -webkit-transition: all 1s ease;
  -o-transition: all 1s ease;
} 
.image:hover {
  transform: scale(1.5);
  -moz-transform: scale(1.5);
  -webkit-transform: scale(1.5);
  -o-transform: scale(1.5);
  -ms-transform: scale(1.5); /* IE 9 */
  -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand')"; /* IE8 */
   filter: progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand'); /* IE6 and 7 */ 
} 
<div class="image-box">
  <div class="image">
  </div>
</div>  

【问题讨论】:

    标签: jquery html css image css-transitions


    【解决方案1】:

    将悬停从image更改为image-box,在image-box内放置一个标签并添加以下css:

    .image-box, .image {
      position: relative;
    }
    .image:after {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      height: 100%;
      width: 100%;
      background-color: red;
      transition: all 1s ease;
      -moz-transition: all 1s ease;
      -ms-transition: all 1s ease;
      -webkit-transition: all 1s ease;
      -o-transition: all 1s ease;
    }
    .label {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      font-size: 25px;
      font-weight: bolder;
    }
    .image-box:hover .image:after {
      opacity: 0.5;
    }
    

    检查一下,让我知道您的反馈。谢谢!

    编辑:

    也增加了image 的转换时间。 :)

    .image-box {
      width: 300px;
      overflow: hidden;
    }
    .image {
      width: 300px;
      height: 200px;
      background: url("http://lorempixel.com/300/200");
      background-position: center;
      transition: all 10s ease;
      -moz-transition: all 10s ease;
      -ms-transition: all 10s ease;
      -webkit-transition: all 10s ease;
      -o-transition: all 10s ease;
    }
    .image-box:hover .image {
      transform: scale(1.5);
      -moz-transform: scale(1.5);
      -webkit-transform: scale(1.5);
      -o-transform: scale(1.5);
      -ms-transform: scale(1.5);
      /* IE 9 */
      -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand')";
      /* IE8 */
      filter: progid: DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand');
      /* IE6 and 7 */
    }
    
    .image-box, .image {
      position: relative;
    }
    .image:after {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      height: 100%;
      width: 100%;
      background-color: red;
      transition: all 1s ease;
      -moz-transition: all 1s ease;
      -ms-transition: all 1s ease;
      -webkit-transition: all 1s ease;
      -o-transition: all 1s ease;
    }
    .label {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      font-size: 25px;
      font-weight: bolder;
    }
    .image-box:hover .image:after {
      opacity: 0.5;
    }
    <div class="image-box">
      <div class="image"></div>
      <div class="label">LABEL</div>
    </div>

    【讨论】:

    • 这很有帮助,而且是正确的!非常感谢! @kukkuz
    【解决方案2】:

    .image-box{
      width:300px;
      overflow:hidden;
    }
    .image {
    	width:300px;
      height:200px;
    	background: url("http://lorempixel.com/300/200");
    	background-position:center;
    	transition: all 1s ease;
      -moz-transition: all 1s ease;
      -ms-transition: all 1s ease;
      -webkit-transition: all 1s ease;
      -o-transition: all 1s ease;
      position: relative;
    } 
    
    .image:hover {
      transform: scale(1.5);
      -moz-transform: scale(1.5);
      -webkit-transform: scale(1.5);
      -o-transform: scale(1.5);
      -ms-transform: scale(1.5); /* IE 9 */
      -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand')"; /* IE8 */
       filter: progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand'); /* IE6 and 7 */ 
    } 
    
    .image:after {
      content: '';
      display: block;
      height: 100%;
      width: 100%;
      position: absolute;
      left: 0;
      top: 0;
      background-color: rgba(255, 0, 0, 0);
      transition: background-color 1s ease;
    }
    
    .image:hover:after {
      background-color: rgba(255, 0 ,0 , 0.4);
    }
    <div class="image-box">
      <div class="image">
      </div>
    </div>  

    我不确定这是否是您正在寻找的东西,但请尝试一下,如果它没有问题,请重做我的代码;

    首先添加一个相对于你的 .image 类的位置

    .image {position: relative;}
    

    然后添加这个:

    .image:after {
          content: '';
          display: block;
          height: 100%;
          width: 100%;
          position: absolute;
          left: 0;
          top: 0;
          background-color: rgba(255, 0, 0, 0);
          transition: background-color 1s ease;
        }
    
        .image:hover:after {
          background-color: rgba(255, 0 ,0 , 0.4);
        }
    

    【讨论】:

    • 感谢 ShadowFoOm ,我试图在背景中的图像淡入和放大的地方得到相反的结果。前景有文字。保持不变。和这个 Fantasy.co/work 链接中的 div 完全一样
    • 我选择使用缩放效果,但我可以使用其他有效的东西。
    • 要扭转淡入淡出效果,你只需要使用background-color: rgba 的不透明度,但如果你也需要添加文本,那么你必须按照 kukkuz 所说的去做。这是你可以做到的一种方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 2019-06-23
    • 2011-10-23
    • 2014-02-24
    • 2023-02-22
    • 1970-01-01
    相关资源
    最近更新 更多