CSS 實(shí)現(xiàn)加載中省略號(hào)是一種很常見(jiàn)的效果,可以提高用戶(hù)體驗(yàn)。一般實(shí)現(xiàn)的方式是使用偽元素來(lái)創(chuàng)建三個(gè)點(diǎn),并通過(guò)動(dòng)畫(huà)來(lái)實(shí)現(xiàn)點(diǎn)的閃爍,下面是實(shí)現(xiàn)代碼:
.loading { display: inline-block; position: relative; width: 64px; height: 18px; } .loading:before, .loading:after { content: ""; display: block; width: 10px; height: 10px; background-color: #777; border-radius: 50%; position: absolute; top: 0; animation: dot 1s ease-in-out infinite; } .loading:before { left: 0; animation-delay: 0s; } .loading:after { left: 24px; animation-delay: .2s; } @keyframes dot { 0% { transform: scale(0); } 50% { transform: scale(1); } 100% { transform: scale(0); } }
三個(gè)點(diǎn)的實(shí)現(xiàn)是通過(guò):before和:after偽元素來(lái)創(chuàng)建,兩個(gè)點(diǎn)的left屬性分別為0和24px,使用animation屬性實(shí)現(xiàn)閃爍效果。同時(shí),為了讓三個(gè)點(diǎn)居中,需要將.loading元素的position設(shè)為relative,并把寬度設(shè)為64px。