我一直在努力解决这个问题。我似乎只能在悬停时让它工作,但我希望它在打开网页时工作,而不必移动游标。
/* styles.css */
body {
margin: 0;
background: linear-gradient(90deg, #403d3d, #555555);
height: 100vh;
}
.loader-line {
width: 0;
height: 2px;
background-color: #e7e3e3;
position: absolute;
top: 50%;
transform: translateY(-50%);
animation: loaderAppear 2s linear 1s forwards, loaderAnim 5s linear infinite;
opacity: 0.4;
}
@keyframes loaderAppear {
0% {
opacity: 0.4;
}
100% {
opacity: 0.8;
}
}
@keyframes loaderAnim {
0% {
left: 50%;
right: 50%;
width: 0;
}
100% {
left: 1%;
right: 99%;
width: 100%;
}
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="test.css">
</head>
<body>
<div class="loader-line"></div>
</body>
</html>
如果使用动画而不是转换;页面一加载,他们就可以播放。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
background-color: #eee;
}
hr {
border-style: none;
border-radius: 1rem;
width: 0.5rem; height: 0.5rem;
background-color: #222;
animation-name: extend;
animation-duration: 1.5s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-direction: alternate;
}
@keyframes extend {
10% {
width: 0.5rem;
}
100% {
width: 90%;
}
}
<hr>
如果希望线只设置一次动画并停留在最后一帧,则可以使用animation-fill-mode: forwards
见下文:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
background-color: #eee;
}
hr {
border-style: none;
border-radius: 1rem;
width: 0.5rem; height: 0.5rem;
background-color: #222;
animation-name: extend;
animation-duration: 2s;
animation-timing-function: ease-in-out;
animation-direction: alternate;
animation-fill-mode: forwards;
}
@keyframes extend {
10% {
width: 0.5rem;
}
100% {
width: 90%;
}
}
<hr>