【问题标题】:How to scale inside a div without expanding the div itself on mozilla browser? [duplicate]如何在 div 内部缩放而不在 mozilla 浏览器上扩展 div 本身? [复制]
【发布时间】:2019-06-27 09:17:44
【问题描述】:

我正在使用缩放功能,因此当我将鼠标悬停在 div 中时,其中的图像和文本会增长。在 chrome 浏览器上它工作得很好,但在 mozilla 上 div 也会增长。我该如何解决?我以前从来没有修复过浏览器兼容性问题,所以我不知道从哪里开始。

<div class="zoom-in">
    <a href="#">
        <img src="https://via.placeholder.com/150">
    </a>
    <h2><a href="#"><span>My Text</a></h2>
</div>
.zoom-in {
    -webkit-transition: all 0.2s ease;
    -moz-transition: all 0.2s ease ;
    -ms-transition: all 0.2s ease ;
    -o-transition: all 0.2s ease ;
    transition: all 0.2s ease ;
}
.zoom-in:hover {
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -ms-transform: scale(1.1);
    -o-transform: scale(1.1);
    transform: scale(1.1);
}

解决方案: 最后我决定只为 mozilla 浏览器使用特定的 CSS 代码

.zoom-in {
    -webkit-transition: all 0.2s ease;
    -ms-transition: all 0.2s ease ;
    -o-transition: all 0.2s ease ;
    transition: all 0.2s ease ;
    &:hover {
        -webkit-transform: scale(1.1);
        -ms-transform: scale(1.1);
        -o-transform: scale(1.1);
        transform: scale(1.1);
    }
}
// Mozilla browser only
@-moz-document url-prefix() {
    .zoom-in {
        max-width: -moz-fit-content;
        &:hover {
            transform: none;
        }
        &:hover img,
        &:hover h2 {
            -moz-transition: all 0.2s ease ;
            -moz-transform: scale(1.1);
        }
    }
}

【问题讨论】:

  • .zoom-in:hover img ?

标签: css mozilla


【解决方案1】:

悬停时需要在img 上应用scale 而不是div 上的scale

.zoom-in {
  -webkit-transition: all 0.2s ease;
  -moz-transition: all 0.2s ease;
  -ms-transition: all 0.2s ease;
  -o-transition: all 0.2s ease;
  transition: all 0.2s ease;
  max-width:fit-content
}

.zoom-in:hover img,
.zoom-in:hover h2 {
  -webkit-transform: scale(1.1);
  -moz-transform: scale(1.1);
  -ms-transform: scale(1.1);
  -o-transform: scale(1.1);
  transform: scale(1.1);
  transform-origin: 0 0;
}
<div class="zoom-in">
  <img src="https://via.placeholder.com/150" alt="img">
  <h2><a href="#" class="text">My Text</a></h2>
</div>

【讨论】:

  • 我编辑了我的代码,因为我有文本和图像,我希望两者都在 div 内缩放。只有当我有一个 img 并且我也为 div 设置了最大宽度时,你的解决方案才有效。
  • @NicolasTwil 更新了答案。你可以使用transform-origin: 0 0;
  • 如果对您有帮助,请投票并接受我的回答@NicolasTwil
  • 最后我为 mozilla 浏览器编写了特定的代码,否则我无法让两个浏览器使用相同的代码
猜你喜欢
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
相关资源
最近更新 更多