1. 程式人生 > >19.純 CSS 創作一種有削鐵如泥感覺的選單導航特效

19.純 CSS 創作一種有削鐵如泥感覺的選單導航特效

原文地址:https://segmentfault.com/a/1190000014836748

感想: 把原元素隱藏,利用偽元素::before::after 各取上下一半 clip-path 切割圖片

HTML程式碼:

<ul class="menu">
  <li data-text="New Game">New Game</li>
  <li data-text="Load Game">Load Game</li>
  <li data-text="Options">Options</li>
  <
li data-text="Exit">Exit</li> </ul>

CSS程式碼:

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: black;
}
.menu li {
    position: relative;
    list-style-type: none;
    color
: transparent; font-size: 3em; text-transform: uppercase; text-align: center; line-height: 1em; width: 7em; margin: 0.5em; /* 畫出文字的分割線 */ border-top: 1px solid transparent; transition: 0.3s; } .menu li:hover{ border-top: 1px solid yellow; } /* 用偽元素新增文字
*/ .menu li::before, .menu li::after{ position: absolute; /* 插入元素的屬性值 */ content: attr(data-text); top: 0; left: 0; width: 100%; color: silver; } /* 把偽元素文字向上移,豎跨分割線 */ .menu li::before, .menu li::after { top: -0.5em; transition: 0.3 ease-out; } /* 用遮罩切出分割線上下二部分的文字 */ .menu li::before{ clip-path: polygon(0 0, 100% 0, 100% 50%, 0 50%); } .menu li::after{ clip-path: polygon(0 50%, 100% 50%, 100% 100%, 0 100%); } .menu li:hover::before, .menu li:hover::after{ color: yellow; transition: left 0.3s ease-out; transition-delay: 0.2s; } .menu li:nth-child(odd):hover::before, .menu li:nth-child(even):hover::after{ left: -0.15em; } .menu li:nth-child(even):hover::before, .menu li:nth-child(odd):hover::after{ left: 0.15em; }