-
Notifications
You must be signed in to change notification settings - Fork 96
/
clipPath.html
177 lines (159 loc) · 4.66 KB
/
clipPath.html
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
176
177
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>微信网页悬浮窗交互效果的web实现</title>
</head>
<style>
html,
body {
width: 100vw;
height: 100vh;
overflow: hidden;
margin: 0;
}
h1 {
margin: 0
}
.page1 {
width: 100%;
height: 100%;
padding: 20px;
box-sizing: border-box;
position: absolute;
left: 0;
top: 0;
z-index: -1;
background-color: #00b8ff;
overflow: hidden;
}
.page2 {
width: 100%;
height: 100%;
padding: 20px;
box-sizing: border-box;
background-color: rgb(118, 145, 39);
transition: all .2s;
clip-path: circle(200vh at 50vw 50vh);
}
.icon-link {
width: 64px;
height: 64px;
background: url(https://www.zhangxinxu.com/study/201806/icon-link.png);
background-size: contain;
border-radius: 50%;
position: fixed;
right: 10px;
bottom: 200px;
opacity: 0;
line-height: 64px;
color: #fff;
text-align: center;
}
</style>
<body>
<div class="page1">
<p>list</p>
<p>list</p>
<p>list</p>
<p>效果实现的关键是页面收缩到悬浮按钮中。使用的是CSS3 clip-path属性。</p>
<p>
如果两层都是绝对定位的话,当上面一页收起来的时候,部分浏览器底部会出现滚动条。。。设置了mate标签以及html,body,overflow:hidden,width:100%;都还是没法解决。。。。 后来我把下一层用绝对定位,同时设置z-index=-1,上一层用文档流,收缩的时候直接用margin-left,就不会出现底部的滚动条了
</p>
</div>
<div id="pageLink" class="page2">
<h1>微信网页悬浮窗交互效果的web实现</h1>
<p>如果浏览器访问,请打开开发者工具,然后使用移动端模式。</p>
<p>向右滑动页面查看效果……</p>
<p>关于技术实现
<a href="https://www.yangyuetao.cn/" target="_blank">访问这里</a>。</p>
</div>
<!-- 悬浮的按钮 -->
<a href="javascript:" id="iconLink" class="icon-link"></a>
<script>
let elPageLink = document.getElementById('pageLink');
let elIconLink = document.getElementById('iconLink');
let widthPageLink = elPageLink.clientWidth;
let widthIconLink = elIconLink.clientWidth;//图标尺寸
let heightIconLink = elIconLink.clientHeight;
let data = {
touching: false,//标识
translateX: 0,
centerX: elIconLink.offsetLeft + widthIconLink / 2,//收缩中心
centerY: elIconLink.offsetTop + heightIconLink / 2
};
const setStyle = (obj, json) => {//批量设置style方法
for (var i in json) {
obj.style[i] = json[i];
}
}
//开始触摸
elPageLink.addEventListener('touchstart', (e) => {
let touch = e.touches[0];
// console.log('1111111')
data.x1 = touch.pageX;
data.translateX = 0;
data.touching = true;
});
//游走
elPageLink.addEventListener('touchmove', (e) => {
let touch = e.touches[0];
// console.log('222222')
if (!data.touching) {
return
}
data.x2 = touch.pageX;
data.translateX = data.x2 - data.x1;
if (data.translateX < 0) {//防止移动过量
data.translateX = 0;
}
setStyle(elPageLink, {
marginLeft: data.translateX + 'px'
})
setStyle(elIconLink, {
opacity: Math.log2(1 + data.translateX / widthPageLink)
})
});
//触摸结束
elPageLink.addEventListener('touchend', (e) => {
let touch = e.changedTouches[0];
// console.log(333333)
if (!data.touching) {
return
}
data.x2 = touch.pageX;
data.translateX = data.x2 - data.x1;
if (data.translateX < widthPageLink / 3) {//小于三分之一视为未移动
data.translateX = 0;
setStyle(elPageLink, {
marginLeft: data.translateX + 'px'
})
setStyle(elIconLink, {
opacity: Math.log2(1 + data.translateX / widthPageLink)
})
} else {
setStyle(elPageLink, {
marginLeft: '0',
clipPath: 'circle(' + heightIconLink / 2 + 'px at ' + (data.centerX) + 'px ' + data.centerY + 'px)'
})
setStyle(elIconLink, {
opacity: 1
})
}
data.touching = false;
});
//点击还原
elIconLink.addEventListener('click', (e) => {
setStyle(elPageLink, {
clipPath: 'circle(100vh at 50vw 50vh)'
})
setStyle(elIconLink, {
opacity: 0
})
})
</script>
</body>
</html>