【问题标题】:How do I prevent child affected for transform scale in css3?如何防止儿童在 css3 中因变换比例而受到影响?
【发布时间】:2014-08-16 11:00:49
【问题描述】:

HTML

<div class="wrapper">
    <div class="parent">
        <div class="child">
            lorem ipsum
        </div>
    </div>
</div>

CSS

.wrapper {
    width: 300px;
    height: 300px;
    margin: 30px auto;
    border: 1px dashed #ccc;
    overflow:hidden;
}

.parent {
    width: 1px;
    height: 100px;
    background-color: #f00;
    opacity: 0;
    margin: 0 auto;
    -webkit-transition: all 0.4s ease;
            transition: all 0.4s ease;
}

.wrapper:hover .parent {
    opacity: 1;
    -webkit-transform: scaleX(300);
            transform: scaleX(300);
}

.wrapper . parent .child {
    -webkit-transform: none;
            transform: none;
}

当悬停 .wrapper div 时,我只想缩放 .parent 元素。但它也会影响两个 div(父和子),尽管为 .child div 提供了 transform:none 属性。如何防止 .child 受影响?

http://jsfiddle.net/cdmkoao4/1/

【问题讨论】:

  • 恐怕不可行,通常它应该可以反转内部元素的transform,但看起来scaleX并非如此......出于一些奇怪的原因。我会选择width
  • @RokoC.Buljan 我想你是对的,但我会等待更好的 scalex 答案。
  • scaleX 在 Chrome 中也给出了奇怪的结果。 FF 使过渡完全平滑,而另一个具有奇怪的方形表面。只要你等! :)

标签: html css transform css-transforms


【解决方案1】:

尝试更改宽度,而不是transform: scaleX

.wrapper:hover .parent {
    opacity: 1;
    width: 100%;
}

http://jsfiddle.net/cdmkoao4/3/

更新:

老实说,我看不出你为什么要使用scaleX 而不是width,但这是我想出的唯一解决方案。不要将.child.parent 嵌套,将它们与position: absolute 分开。悬停时,您将只更改 scaleX .parent,而您只会更改 .childopacity(我仍然使用相同的类,即使它们不再具有 parent-child 状态)。

http://jsfiddle.net/cdmkoao4/9/

您需要花一点时间在transition-delay 上,以获得您想要的确切效果。

【讨论】:

  • @Davin 这是一个很好的采样方法,但是项目太复杂了,这只是一个项目。所以我必须在我的项目中使用 scalex。但是谢谢你的回答。它很有用
  • Positionscalerotationopacity 在现代浏览器中非常便宜,而且如果可能的话,应该优先考虑。如果您使用其他任何东西,您可能不会获得每秒 60 帧的动画。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多