简体   繁体   English

菜单项下划线在 hover state 末尾垂直收缩

[英]Menu item underline shrinks vertically at the end of hover state

I am having hard time to understand why underline is shrinking at the end of hovering state (small vertical shrink).我很难理解为什么下划线在悬停 state (小的垂直收缩)结束时收缩。 Code itself is simple and there shouldn't be anything that shrinks this pseudo element.代码本身很简单,不应该有任何东西可以缩小这个元素。 The same thing is happening in this editor.在这个编辑器中也发生了同样的事情。 But, this effect isn't happening on Mozilla for example.但是,这种效果并没有发生在例如Mozilla上。 So my question is: Am I missing something or is this something Google chrome has with?所以我的问题是:我错过了什么还是谷歌浏览器有什么?

 a{ text-decoration: none; }.header{ z-index: 1; height: 100px; width: 100%; position: absolute; }.main-header{ background-color: var(--menu-header-background); top: var(--header_height); text-shadow: 0 2px 5px rgba(0, 0, 0, .2); }.small-header{ height: var(--header_height); background-color: rgba(0, 0, 0, 0.63); } li{ list-style-type: none; }.header-container{ max-width: 1100px; margin: auto; height: inherit; }.navigation{ float: right; height: inherit; }.navigation{ float: right; height: inherit; } header.navigation a li{ display: table-cell; vertical-align: middle; color: black; text-transform: uppercase; height: inherit; } #main-menu li:hover{ color: blue; transition: .4s; } #main-menu a{ position: relative; } #main-menu li::after{ content: ''; position: absolute; left: 50%; bottom: 0px; transform: translateX(-50%) scaleX(0); width: 100%; height: 2px; background-color: blue; transition: transform.25s; } #main-menu li:hover::after{ transform: translateX(-50%) scaleX(1); }
 <header class="main-header header"> <div class="header-container"> <ul class="navigation" id="main-menu"> <a href="#"> <li>home</li> </a> <a href="#"> <li>about me</li> </a> <a href="#"> <li>projects</li> </a> <a href="#"> <li>contact me</li> </a> </ul> </div> </header>

This a bug about transform.这是关于转换的错误。 Adding a dummy box-shadow and will-change: transform will fix the bug.添加一个虚拟的 box-shadow 和will-change: transform将修复该错误。

 a{ text-decoration: none; }.header{ z-index: 1; height: 100px; width: 100%; position: absolute; }.main-header{ background-color: var(--menu-header-background); top: var(--header_height); text-shadow: 0 2px 5px rgba(0, 0, 0, .2); }.small-header{ height: var(--header_height); background-color: rgba(0, 0, 0, 0.63); } li{ list-style-type: none; }.header-container{ max-width: 1100px; margin: auto; height: inherit; }.navigation{ float: right; height: inherit; }.navigation{ float: right; height: inherit; } header.navigation a li{ display: table-cell; vertical-align: middle; color: black; text-transform: uppercase; height: inherit; } #main-menu li:hover{ color: blue; transition: .4s; } #main-menu a{ position: relative; } #main-menu li::after{ content: ''; position: absolute; left: 50%; bottom: 0px; transform: translateX(-50%) scaleX(0); width: 100%; height: 2px; background-color: blue; transition: transform.25s; /* added */ will-change: transform; box-shadow: 0 0 1px rgba(255, 255, 255, 0); } #main-menu li:hover::after{ transform: translateX(-50%) scaleX(1); }
 <header class="main-header header"> <div class="header-container"> <ul class="navigation" id="main-menu"> <a href="#"> <li>home</li> </a> <a href="#"> <li>about me</li> </a> <a href="#"> <li>projects</li> </a> <a href="#"> <li>contact me</li> </a> </ul> </div> </header>


My refer answer我的参考答案

Firstly, the line underneath the menu-item shrinks because when you hover the menu item, it changes the scaleX from 0 to 1:首先,菜单项下方的行缩小了,因为当您 hover 菜单项时,它将 scaleX 从 0 更改为 1:

from: transform: translateX(-50%) scaleX(0);来自: transform: translateX(-50%) scaleX(0);
to: transform: translateX(-50%) scaleX(1); to: transform: translateX(-50%) scaleX(1);

and you have transition property on transform because of which the line shrinks smoothly.并且您在变换上有过渡属性,因此线条会平滑收缩。

It is not working on Mozilla Firefox because you haven't added anyvendor prefixes .它不适用于 Mozilla Firefox,因为您没有添加任何供应商前缀 You need to add prefixes to both transition and transform properties to ensure maximum compatibility.您需要为转换和转换属性添加前缀以确保最大兼容性。

Try using these in your style:尝试在您的风格中使用这些:


a {
    text-decoration: none;
}
.header{
    z-index: 1;
    height: 100px;
    width: 100%;
    position: absolute;
}
.main-header{
    background-color: var(--menu-header-background);
    top: var(--header_height);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .2);
}
.small-header{
    height: var(--header_height);
    background-color: rgba(0, 0, 0, 0.63);
}
li{
    list-style-type: none;
}
.header-container{
    max-width: 1100px;
    margin: auto;
    height: inherit;
}
.navigation{
    float: right;
    height: inherit;
}
.navigation{
    float: right;
    height: inherit;
}
header .navigation a li{
    display: table-cell;
    vertical-align: middle;
    color: black;
    text-transform: uppercase;
    height: inherit;
}
#main-menu li:hover{
    color: blue;
    -webkit-transition: .4s;
    -o-transition: .4s;
    transition: .4s;
}
#main-menu a{
    position: relative;
}
#main-menu li::after{
    content: '';
    position: absolute;
    left: 50%;
    bottom: 0px;
    -webkit-transform: translateX(-50%) scaleX(0);
        -ms-transform: translateX(-50%) scaleX(0);
            transform: translateX(-50%) scaleX(0);
    width: 100%;
    height: 2px;
    background-color: blue;
    -webkit-transition: -webkit-transform .25s;
    transition: -webkit-transform .25s;
    -o-transition: transform .25s;
    transition: transform .25s;
    transition: transform .25s, -webkit-transform .25s;
}
#main-menu li:hover::after{
    -webkit-transform: translateX(-50%) scaleX(1);
        -ms-transform: translateX(-50%) scaleX(1);
            transform: translateX(-50%) scaleX(1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM