您的位置:首页 - 网页特效 - 正文
CSS代码实现过山车加载中动画
网页动画的实现方法,以前热门的有FLASH动画,微软推出的SilverLight可以实现网页动画,或者使用Javascript实现。

后来HTML5和CSS3推出后,网页动画的实现更加方便了,可以很方便的实现强大,复杂的动画。

例如这个示例,CSS实现加载中动画:
https://hovertree.com/texiao/css3/48/

这个类似过山车的动画只用CSS和THML代码实现,没有用到Javascript代码。如果设计这样的过山车,你敢坐吗?

代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>CSS Loaders加载动画-何问起</title>
<style>
* {
border: 0;
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--hue: 223;
--bg: hsl(var(--hue),10%,90%);
--fg: hsl(var(--hue),10%,10%);
font-size: calc(16px + (24 - 16) * (100vw - 320px) / (1280 - 320));
}

body {
background-color: #1f1f1f;
color: var(--fg);
font: 1em/1.5 sans-serif;
height: 100vh;
display: grid;
place-items: center;
transition: background-color 0.3s;
}

main {
padding: 1.5em 0;
}

.ap {
width: 8em;
height: 16em;
}

.ap__ring {
transition: stroke 0.3s;
}

.ap__worm1,
.ap__worm2 {
animation-duration: 3s;
animation-iteration-count: infinite;
}

.ap__worm1 {
animation-name: worm1;
}

.ap__worm2 {
animation-name: worm2;
visibility: hidden;
}

/* Dark theme */
@media (prefers-color-scheme: dark) {
:root {
--bg: hsl(var(--hue),10%,10%);
--fg: hsl(var(--hue),10%,90%);
}

.ap__ring {
stroke: hsla(var(--hue),10%,90%,0.9);
}
}

/* Animtions */
@keyframes worm1 {
from {
animation-timing-function: ease-in-out;
stroke-dashoffset: -87.96;
}

20% {
animation-timing-function: ease-in;
stroke-dashoffset: 0;
}

60% {
stroke-dashoffset: -791.68;
visibility: visible;
}

60.1%, to {
stroke-dashoffset: -791.68;
visibility: hidden;
}
}

@keyframes worm2 {
from, 60% {
stroke-dashoffset: -87.96;
visibility: hidden;
}

60.1% {
animation-timing-function: cubic-bezier(0,0,0.5,0.75);
stroke-dashoffset: -87.96;
visibility: visible;
}

77% {
animation-timing-function: cubic-bezier(0.5,0.25,0.5,0.88);
stroke-dashoffset: -340;
visibility: visible;
}

to {
stroke-dashoffset: -669.92;
visibility: visible;
}
}
</style>
</head>
<body>
<main>
<svg class="ap" viewBox="0 0 128 256" width="128px" height="256px" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="ap-grad1" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stop-color="hsl(223,90%,55%)" />
<stop offset="100%" stop-color="hsl(253,90%,55%)" />
</linearGradient>
<linearGradient id="ap-grad2" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stop-color="hsl(193,90%,55%)" />
<stop offset="50%" stop-color="hsl(223,90%,55%)" />
<stop offset="100%" stop-color="hsl(253,90%,55%)" />
</linearGradient>
</defs>
<circle class="ap__ring" r="56" cx="64" cy="192" fill="none" stroke="#ddd" stroke-width="16" stroke-linecap="round" />
<circle class="ap__worm1" r="56" cx="64" cy="192" fill="none" stroke="url(#ap-grad1)" stroke-width="16" stroke-linecap="round" stroke-dasharray="87.96 263.89" />
<path class="ap__worm2" d="M120,192A56,56,0,0,1,8,192C8,161.07,16,8,64,8S120,161.07,120,192Z" fill="none" stroke="url(#ap-grad2)" stroke-width="16" stroke-linecap="round" stroke-dasharray="87.96 494" />
</svg>
</main>
<footer>
&copy; hovertree.com <a href="https://hovertree.com/h/bjag/gd5qsnd1.htm">查看原文</a>
</footer>
</body>
</html>
收藏 列表

评论: