hooyantsing's Blog

P236_28_还用啥JS效果CSS这么给力了

字数统计: 373阅读时长: 2 min
2020/04/25

P236 28 还用啥JS效果CSS这么给力了

实战案例

效果:

54f9f2e86ee808400277242ab475dd39.png

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100vw;
            height: 100vh;
        }

        main {
            width: 400px;
            height: 200px;
            overflow: hidden;
            position: relative;
        }

        article {
            display: grid;
            grid-template: 1fr / repeat(4, 400px);
            animation-name: slide;
            animation-duration: 4s;
            animation-iteration-count: infinite;
            animation-timing-function: steps(4, end);
        }

        article:hover, article:hover + ul::after {
            animation-play-state: paused;
        }

        div {
            overflow: hidden;
        }

        img {
            width: 100%;
        }

        @keyframes slide {
            to {
                transform: translateX(-1600px);
            }
        }

        ul {
            width: 200px;
            position: absolute;
            left: 50%;
            bottom: 50px;
            transform: translateX(-50%);
            list-style-type: none;
            display: grid;
            grid-template: 1fr / repeat(4, 1fr);
            justify-items: center;
        }

        ul::after {
            position: absolute;
            content: '';
            width: 20px;
            height: 20px;
            border-radius: 50%;
            left: 15px;
            background: #e67e22;
            z-index: -1;
            animation-name: num;
            animation-duration: 4s;
            animation-iteration-count: infinite;
            animation-timing-function: steps(4, end);
        }

        li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: rgba(0, 0, 0, .5);
            display: flex;
            justify-content: center;
            align-content: center;
            color: white;
            font-size: .8em;
        }

        @keyframes num {
            to {
                transform: translateX(200px);
            }
        }
    </style>
</head>
<body>
<main>
    <article>
        <div><img src="1.jpg"></div>
        <div><img src="2.jpg"></div>
        <div><img src="3.jpg"></div>
        <div><img src="4.jpg"></div>
    </article>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
</main>
</body>
</html>
CATALOG