html - CSS Border Transition添加了多余的多余像素

标签 html css sass electron css-transitions

我正在为我的 Electron 应用程序(HTML,SASS)设计一个按钮。悬停时,此按钮将在边框顶部和边框底部添加10px,然后将其高度调整为-20,以取消多余的边框高度。除了将小的边框(看起来少于1px)添加到每个边框(悬停的顶部和底部)时,我的过渡效果几乎完美,此附加边框增加了按钮的总高度,足以插入下游html在视觉上有点烦人的颠簸。在包含的gif中,当悬停过渡完成时,您会最注意到它,您应该会看到一个1px的边框在结尾处突然消失。如何防止边框的这几个额外像素生成并碰撞其余的html?还有其他方法可以产生与我相同的效果吗?
这是我在这里的第二篇帖子,最近一篇是在2年前,所以如果我缺少我应该包括的内容,请告诉我,谢谢阅读!
HTML:

<div class="--square e--block --btn">
    <h1>Create Backups</h1>                    
</div>
SCSS变量:
$mainSize: 100px;
$btnBorder: 10px;
SCSS:

//-- Basic element
.e--block {
    //-- Position
    //- Flex
    display: flex;
    flex-direction: column;
    justify-content: center;
    //- Text
    text-align: center;
    //- Align
    margin: $baseSpacing;

    //-- Style
    //- BG
    background-color: $btnColorLight;
    //- Text
    color: $white;
    font-weight: 2;
    //- Border
    //border: none;
    border-radius: $borderRadius;

    
    * {
        //-- Position
        //- Flex
        vertical-align: middle;
        //- Align
        padding: {
            top: 10px;
            bottom: 10px;
        }
        margin: auto;
    }
}

.--btn {
    //-- Style
    //- Border
    border: 0px solid white;
    //- Transition
    transition: {
        property: border-width, height;
        duration: 1s;
        timing-function: ease;
    }
}
.--square {
    //-- Position
    //- Size
    height: ($mainSize * 2);
    width: ($mainSize * 2);
}
.--btn.--square:hover:not(.--no-trans) {
    //-- Position
    //- Size
    height: (($mainSize * 2) - ($btnBorder * 2));
    //-- Style
    //- Border
    border-top-width: $btnBorder;
    border-bottom-width: $btnBorder;
}
Visual of the problem

最佳答案

我无法解释为什么会发生此问题,但是我想这是因为渲染引擎不善于处理这种双重转换。
作为解决方法,您可以在box-sizing上使用border-box值为 .--square 属性,并删除height更改:

.--square {
   //-- Position
   //- Size
   height: ($mainSize * 2);
   width: ($mainSize * 2);
   box-sizing: border-box;
}

.--btn.--square:hover:not(.--no-trans) {
    //-- Position
    //- Size
    //-- Style
    //- Border
    border-top-width: $btnBorder;
    border-bottom-width: $btnBorder;
}

关于html - CSS Border Transition添加了多余的多余像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65403096/

相关文章:

javascript - 文本区域剩余字符无法正常工作

javascript - 将 react-css-modules 与 sass 一起使用不起作用

css - 使用 CSS 为移动设备绘制字体大小范围

html - Angular 2 *ngFor导致bootstrap 4列内容太瘦

html - 透明悬停在图像按钮中的图灵图像

javascript - HTML 文档中脚本标签的位置 - <head> 和 <body> 具有不同的行为

css - Bootstrap : How to create additional menu items in navbar-collapse

javascript - 错误的 Canvas 上下文

html - CSS 无法在移动设备上加载?

css - css 属性值中的 !default 是什么意思?