css – 为什么在webkit浏览器中’margin’和’padding’的转换是滞后的?

前端之家收集整理的这篇文章主要介绍了css – 为什么在webkit浏览器中’margin’和’padding’的转换是滞后的?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图对margin和padding属性应用CSS转换.

我想让视觉上的背景变得更大.所以我增加了填充,并相应地减少了空白,所以文本将保持在他们当前的位置.

这是代码

.content {
    width: 600px;
    margin: auto;
}

a.btn {
    background-color: #5C9E42;
    color: #fff;
    text-decoration: none;
    font-size: 35px;
    border-radius: 5px;
    padding: 10px;
    text-shadow: 2px 2px 2px #696969;
    transition: all 0.3s ease;
}

a.btn:hover {
    background-color: #23570E;
    padding: 20px;
    margin: -10px;
}
<div class="content">
    <p>Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <a href="#" class="btn">Bello! How are you doing?</a>
</div>

当您运行代码时,您可以看到,转换是滞后的,文本在悬停时提供了混乱.

但是,它只发生在Chrome,Safari,Opera和其他Webkit浏览器中.
它在Firefox和IE中工作正常.

P.S:使a.btns显示为内联块稍微减少了滞后.可能是什么问题?

解决方法

解决方法是将转换应用于具有背景颜色的伪元素并将其缩放到悬停上.这样,文本仍然是“未经过转换的”,不会摆动:

Demo

CSS:

a.btn {
     position:relative;
    color: #fff;
    text-decoration: none;
    font-size: 35px;
    padding: 10px;
    text-shadow: 2px 2px 2px #696969;
}
a.btn:before{
    content:'';
    position:absolute;
    top:0; left:0;
    border-radius: 5px;
    width:100%; height:100%;
    background-color: #5C9E42;
    z-index:-1;

    -webkit-transition: all .3s ease;
    transition: all .3s ease;
}
a.btn:hover:before {
    background-color: #23570E;

    -webkit-transform: scaleX(1.1) scaleY(1.2);
    -ms-transform: scaleX(1.1) scaleY(1.2);
    transform: scaleX(1.1) scaleY(1.2);
}

您应该包括转换和转换CSS属性的供应商前缀,更多信息请查看CanIUse.

原文链接:https://www.f2er.com/css/214545.html

猜你在找的CSS相关文章