效果地址:https://scrimba.com/c/cqKv4VCRhtml
HTML code:flex
<div class="loader"> <span>Loading...</span> </div>
CSS code:spa
html, body { margin: 0; padding: 0; } /* 設置body的子元素水平垂直居中 */ body { height: 100vh; display: flex; justify-content: center; align-items: center; background-color: black; overflow: hidden; } /* 設置容器尺寸 */ .loader { /* width、height包括邊框、內邊距、內容區 */ box-sizing: border-box; position: relative; /* 在此調節font-size能夠調節整個loader的大小,前提是不能在後代元素裏設置font-size了 */ font-size: 20px; width: 10em; height: 10em; border-radius: 50%; border-top: 0.3em solid red; border-bottom: 0.3em solid blue; animation: rotating 2s ease-in-out infinite; --direction: 1; } /* 設置文字樣式 */ .loader span { position: absolute; color: white; width: inherit; height: inherit; text-align: center; line-height: 10em; font-family: sans-serif; animation: rotating 2s linear infinite; --direction: -1; } @keyframes rotating { 50% { transform: rotate(calc(180deg * var(--direction))); } 100% { transform: rotate(calc(360deg * var(--direction))); } }