hooyantsing's Blog

P179_28_让时钟走起来transform技巧使用

字数统计: 495阅读时长: 3 min
2020/04/22

P179 28 让时钟走起来transform技巧使用

实战案例

效果:

ed1bd46649db48570b7022a223707d8c.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

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

        main {
            width: 400px;
            height: 400px;
            border-radius: 50%;
            position: relative;
        }

        .clock {
            width: 400px;
            height: 400px;
            border-radius: 50%;
            background: linear-gradient(to right, #f1c40f, #e67e22, #e74c3c);
            position: relative;
            z-index: -2;
        }

        .clock:after {
            content: '';
            background: #2c3e50;
            position: absolute;
            width: 90%;
            height: 90%;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border-radius: 50%;
            z-index: -1;
        }

        .line {
            position: absolute;
            width: 80%;
            height: 80%;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border-radius: 50%;
        }

        .line::before {
            content: '';
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            position: absolute;
            width: 90%;
            height: 90%;
            background: #2c3e50;
            border-radius: 50%;
            z-index: 2;
        }

        .line::after {
            content: '';
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            position: absolute;
            width: 20px;
            height: 20px;
            background: #fff;
            border-radius: 50%;
            z-index: 2;
        }

        .line div {
            width: 8px;
            height: 100%;
            position: absolute;
            background: white;
            left: 50%;
        }

        .line div:nth-child(1) {
            transform: translateX(-50%) rotateZ(0);
        }

        .line div:nth-child(2) {
            transform: translateX(-50%) rotateZ(30deg);
        }

        .line div:nth-child(3) {
            transform: translateX(-50%) rotateZ(60deg);
        }

        .line div:nth-child(4) {
            transform: translateX(-50%) rotateZ(90deg);
        }

        .line div:nth-child(5) {
            transform: translateX(-50%) rotateZ(120deg);
        }

        .line div:nth-child(6) {
            transform: translateX(-50%) rotateZ(150deg);
        }

        .hour {
            width: 8px;
            height: 15%;
            position: absolute;
            background: white;
            left: 50%;
            bottom: 50%;
            transform: translateX(-50%);
            transform-origin: bottom;
        }

        .minute {
            width: 5px;
            height: 25%;
            position: absolute;
            background: white;
            left: 50%;
            bottom: 50%;
            transform: translateX(-50%) rotateZ(60deg);
            transform-origin: bottom;
        }

        .second {
            width: 1px;
            height: 35%;
            position: absolute;
            background: white;
            left: 50%;
            bottom: 50%;
            transform: translateX(-50%) rotateZ(90deg);
            transform-origin: bottom;
            transition: 40s;
        }

        main:hover .second {
            transform: translateX(-50%) rotateZ(360deg);
        }
    </style>
</head>
<body>
<main>
    <div class="clock">
        <div class="line">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
        <div class="hour"></div>
        <div class="minute"></div>
        <div class="second"></div>
    </div>
</main>
</body>
</html>
CATALOG