在flexbox中对图像的缩放效果(Scale effect on image inside a flexbox)

编程入门 行业动态 更新时间:2024-10-24 14:21:58
在flexbox中对图像的缩放效果(Scale effect on image inside a flexbox)

我试图在一个弹性盒子里对图像产生比例效果。 一旦应用了比例效果,图像就会从flexbox中增长出来。 我希望通过保持在柔性盒区域来缩放图像。 我试图应用overflow: hidden但它没有工作。 以下是代码片段:

  

.imageContainer {
    display: flex;
    color: #fff;
    -webkit-box-flex: 0;
    flex-grow: 0;
    flex-shrink: 0;
    padding: 8px;
    flex-basis: 32.33333333%;
    border: solid 1px red;
}
.imageContainer img {
    width: 100%;
    background-size: cover;
}
.thumbnail-container{
webkit-transition: background 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
    transition: background 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
    position: relative;
    margin: 0 auto;
    overflow: hidden;
    
    &:hover{
        transform: scale(1.1);
    }
} 
  
  <div class="imageContainer">
        <div class="thumbnail-container">
            <img src="https://upload.wikimedia.org/wikipedia/commons/5/55/Monkey_mustang.jpg">
        </div>
    </div>
    
     <div class="imageContainer">
        <div class="thumbnail-container">
            <img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Mustang_mario.jpg">
        </div>
    </div> 
  
 

I am trying to have a scale effect on the image inside a flex box. Once the scale effect is applied, the image grows out of the flexbox. I would like the image to scale by staying within the flexbox area. I tried to apply overflow: hidden but it did not work. Below is the code snippet:

  

.imageContainer {
    display: flex;
    color: #fff;
    -webkit-box-flex: 0;
    flex-grow: 0;
    flex-shrink: 0;
    padding: 8px;
    flex-basis: 32.33333333%;
    border: solid 1px red;
}
.imageContainer img {
    width: 100%;
    background-size: cover;
}
.thumbnail-container{
webkit-transition: background 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
    transition: background 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
    position: relative;
    margin: 0 auto;
    overflow: hidden;
    
    &:hover{
        transform: scale(1.1);
    }
} 
  
  <div class="imageContainer">
        <div class="thumbnail-container">
            <img src="https://upload.wikimedia.org/wikipedia/commons/5/55/Monkey_mustang.jpg">
        </div>
    </div>
    
     <div class="imageContainer">
        <div class="thumbnail-container">
            <img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Mustang_mario.jpg">
        </div>
    </div> 
  
 

最满意答案

您应该使用overflow:hidden在缩略图容器上,并且在您直接添加图像时没有点动画背景

并且该比例尺应该应用在图像上而不是容器上

看解决方案:

.imageContainer {
  display: flex;
  color: #fff;
  -webkit-box-flex: 0;
  flex-grow: 0;
  flex-shrink: 0;
  padding: 8px;
  flex-basis: 32.33333333%;
  border: solid 1px red;
}

.imageContainer img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  webkit-transition: all 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
  transition: all 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.thumbnail-container {
  position: relative;
  margin: 0 auto;
  overflow: hidden;
  width: 100%;
}

.thumbnail-container:hover img {
  transform: scale(1.1);
} 
  
<div class="imageContainer">
  <div class="thumbnail-container">
    <img src="https://upload.wikimedia.org/wikipedia/commons/5/55/Monkey_mustang.jpg">
  </div>
</div>

<div class="imageContainer">
  <div class="thumbnail-container">
    <img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Mustang_mario.jpg">
  </div>
</div> 
  
 

You should use overflow:hidden on the thumbnail-container and there is no point animating the background when you are adding the image directly

and also the scale should be applied on the image and not the container

see solution:

.imageContainer {
  display: flex;
  color: #fff;
  -webkit-box-flex: 0;
  flex-grow: 0;
  flex-shrink: 0;
  padding: 8px;
  flex-basis: 32.33333333%;
  border: solid 1px red;
}

.imageContainer img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  webkit-transition: all 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
  transition: all 250ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.thumbnail-container {
  position: relative;
  margin: 0 auto;
  overflow: hidden;
  width: 100%;
}

.thumbnail-container:hover img {
  transform: scale(1.1);
} 
  
<div class="imageContainer">
  <div class="thumbnail-container">
    <img src="https://upload.wikimedia.org/wikipedia/commons/5/55/Monkey_mustang.jpg">
  </div>
</div>

<div class="imageContainer">
  <div class="thumbnail-container">
    <img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Mustang_mario.jpg">
  </div>
</div> 
  
 

更多推荐

本文发布于:2023-07-08 19:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   中对   图像   效果   flexbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!