Animating CSS scaleY causes glitchy lines around divs.

huangapple go评论72阅读模式
英文:

Animating CSS scaleY causes glitchy lines around divs

问题

如果您运行以下代码段,当您悬停在 <div> 上时,动画会导致边缘出现小白线。这看起来有点故障。有没有更好的方法在鼠标悬停时对框进行动画处理,而不会导致 <div> 周围出现这些故障线?我以为动画缩放应该是性能高的方式?这个故障在 <div> 越多时变得更糟糕...是某种抗锯齿设置吗?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Find My Shade</title>
</head>
<body>
    <div class="fms-wrapper">
        <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
        <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
    </div>
</body>

<style>

.fms-wrapper {
    height: 300px;
    width: 100%;
    display: flex;
    margin: 100px 0;
}

.fms-shade {
    flex-basis: auto;
    flex-grow: 1;
    transition: all 0.8s;
    transform-origin: 0.5;
    transform: scaleY(1);
}

.fms-selected {
    flex-grow: 4;
    transition: all 0.2s;
    transform-origin: 0.5;
    transform: scaleY(1.2);
}

</style>

<script>
    var hoverColor = 'pink';

    document.addEventListener('DOMContentLoaded', fms())

    function fms(){
        var shadeFinder = document.querySelector('.fms-wrapper')
        var shadeDiv = document.querySelectorAll('.fms-shade')

        //首次加载正确的颜色到<div>中
        shadeDiv.forEach(div => {
            div.style.backgroundColor = div.getAttribute("fms-bg")
        })

        //鼠标事件
        shadeFinder.onmouseover = function(event) {
            let target = event.target;
            target.style.background = hoverColor;
            target.classList.add('fms-selected')
        };
        shadeFinder.onmouseout = function(event) {
            let target = event.target;
            target.style.background = target.getAttribute("fms-bg");
            target.classList.remove('fms-selected')
        };
    }
</script>
</html>
英文:

If you run the below snippet you see when you hover over the divs the animation causes small white lines to form on the edges. This kinda looks glitchy. Is there a better way to animate the box out on mouseover without causing these glitchy lines around the divs? I thought that animating scale was meant to be performant? This glitch is even worse the more divs there are.. is it some kind of antialias setting?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;title&gt;Find My Shade&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;fms-wrapper&quot;&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 72, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(65, 104, 148)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(95, 189, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 168, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 72, 183)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 72, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(65, 104, 148)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(95, 189, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 168, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 72, 183)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 72, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(65, 104, 148)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(95, 189, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 168, 72)&quot;&gt;&lt;/div&gt;
&lt;div f9 class=&quot;fms-shade&quot; fms-bg=&quot;rgb(189, 72, 183)&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;style&gt;
.fms-wrapper {
height: 300px;
width: 100%;
display: flex;
margin: 100px 0;
}
.fms-shade {
flex-basis: auto;
flex-grow: 1;
transition: all 0.8s;
transform-origin: 0.5;
transform: scaleY(1);
}
.fms-selected {
flex-grow: 4;
transition: all 0.2s;
transform-origin: 0.5;
transform: scaleY(1.2);
}
&lt;/style&gt;
&lt;script&gt;
var hoverColor = &#39;pink&#39;;
document.addEventListener(&#39;DOMContentLoaded&#39;, fms())
function fms(){
var shadeFinder = document.querySelector(&#39;.fms-wrapper&#39;)
var shadeDiv = document.querySelectorAll(&#39;.fms-shade&#39;)
//first load the right colors in to the divs
shadeDiv.forEach(div =&gt; {
div.style.backgroundColor = div.getAttribute(&quot;fms-bg&quot;)
})
//mouse events
shadeFinder.onmouseover = function(event) {
let target = event.target;
target.style.background = hoverColor;
target.classList.add(&#39;fms-selected&#39;)
};
shadeFinder.onmouseout = function(event) {
let target = event.target;
target.style.background = target.getAttribute(&quot;fms-bg&quot;);
target.classList.remove(&#39;fms-selected&#39;)
};
}
&lt;/script&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 3

将您的 .fms-shade 类添加 margin-left:-1px 可以消除间隙。

.fms-shade {
    flex-basis: auto;
    flex-grow: 1;
    transition: all 0.8s;
    transform-origin: 0.5;
    transform: scaleY(1);
    margin-left:-1px;
}

<a href="https://jsfiddle.net/to98gamv/">这是应用此更改的工作代码</a>。

英文:

Adding a margin-left:-1px to your .fms-shade class removes the gap.

.fms-shade {
flex-basis: auto;
flex-grow: 1;
transition: all 0.8s;
transform-origin: 0.5;
transform: scaleY(1);
margin-left:-1px;
}

<a href="https://jsfiddle.net/to98gamv/">Here is the working coding with this change</a>.

huangapple
  • 本文由 发表于 2020年1月6日 22:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定