HTML5+CSS3静态页面项目-BusinessTheme的总结
因为期末考试、调整心态等等的种种原因,距离上一次的项目练习已经过了很久了,今天终于有时间继续练习HTML5+CSS3的页面架构和设计稿还原。设计图很长,整个页面分为了好几个区域,所以就不放完整的设计图
顺晟科技
2021-09-17 12:59:16
178
弹窗是一种常见的交互方式,而蒙层是弹窗必不可少的元素,用于隔断页面与弹窗区块,暂时阻断页面的交互。但是,在蒙层元素中滑动的时候,滑到内容的尽头时,再继续滑动,蒙层底部的页面会开始滚动,显然这不是我们想要的效果,因此需要阻止这种行为。
那么,如何阻止呢?请看以下分析:
overflow: hidden;
height: ;
在某些机型下,你可能还需要给根节点添加样式:
overflow: hidden;
优点:
简单方便,只需添加css样式,没有复杂的逻辑。
缺点:
兼容性不好,适用于pc,移动端就尴尬了。
部分安卓机型以及safari中,无法无法阻止底部页面滚动。
如果需要应用于移动端,那么你可能需要方案二。
就是利用移动端的touch事件,来阻止默认行为(这里可以理解为页面滚动就是默认行为)。
1 // node为蒙层容器dom节点
2 node.addEventListener(\'touchstart\', e => {
3 e.preventDefault()
4 }, false)
办公资源网址导航 https://www.wode007.com
简单粗暴,滚动时底部页面也无法动弹了。假如你的蒙层内容不会有滚动条,那么上述方法prefect。
但是,最怕空气突然安静,假如蒙层内容有滚动条的话,那么它再也无法动弹了。因此我们需要写一些js逻辑来判断要不要阻止默认行为,复杂程度明显增加。
具体思路:判定蒙层内容是否滚动到尽头,是则阻止默认行为,反之任它横行。
touchend事件触发前),继续滑动时页面内容不会滚动,此时若松手再继续滚动,则页面内容会滚动。利用这一个小技巧,我们可以精简优化我们的代码逻辑。
Tip:这里我发现了一个小技巧,可以省略不少代码。在一次滑动中,若蒙层内容可以滚动,则蒙层内容滚动,过程中即使蒙层内容已滚至尽头,只要不松手(可以理解为
示例代码如下:
1 <body>
2 <div class="page">
3 <!-- 这里多添加一些,直至出现滚动条 -->
4 <p>页面</p>
5 <p>页面</p>
6 <button class="btn">打开蒙层</button>
7 <p>页面</p>
8 </div>
9 <div class="container">
10 <div class="layer"></div>
11 <div class="content">
12 <!-- 这里多添加一些,直至出现滚动条 -->
13 <p>蒙层</p>
14 <p>蒙层</p>
15 <p>蒙层</p>
16 </div>
17 </div>
18 </body>
19 body {
20 margin: 0;
21 padding: 20px;
22 }
23
24 .btn {
25 border: none;
26 outline: none;
27 font-size: inherit;
28 border-radius: 4px;
29 padding: 1em;
30 width: ;
31 margin: 1em 0;
32 color: #fff;
33 background-color: #ff5777;
34 }
35
36 .container {
37 position: fixed;
38 top: 0;
39 left: 0;
40 bottom: 0;
41 right: 0;
42 z-index: 1001;
43 display: none;
44 }
45
46 .layer {
47 position: absolute;
48 top: 0;
49 left: 0;
50 bottom: 0;
51 right: 0;
52 z-index: 1;
53 background-color: rgba(0, 0, 0, .3);
54 }
55
56 .content {
57 position: absolute;
58 bottom: 0;
59 left: 0;
60 right: 0;
61 height: 50%;
62 z-index: 2;
63 background-color: #f6f6f6;
64 overflow-y: auto;
65 }
66 const btnNode = document.querySelector(\'.btn\')
67 const containerNode = document.querySelector(\'.container\')
68 const layerNode = document.querySelector(\'.layer\')
69 const contentNode = document.querySelector(\'.content\')
70 let startY = 0 // 记录开始滑动的坐标,用于判断滑动方向
71 let status = 0 // 0:未开始,1:已开始,2:滑动中
72
73 // 打开蒙层
74 btnNode.addEventListener(\'click\', () => {
75 containerNode.style.display = \'block\'
76 }, false)
77
78 // 蒙层部分始终阻止默认行为
79 layerNode.addEventListener(\'touchstart\', e => {
80 e.preventDefault()
81 }, false)
82
83 // 核心部分
84 contentNode.addEventListener(\'touchstart\', e => {
85 status = 1
86 startY = e.targetTouches[0].pageY
87 }, false)
88
89 contentNode.addEventListener(\'touchmove\', e => {
90 // 判定一次就够了
91 if (status !== 1) return
92
93 status = 2
94
95 let t = e.target || e.srcElement
96 let py = e.targetTouches[0].pageY
97 let ch = t.clientHeight // 内容可视高度
98 let sh = t.scrollHeight // 内容滚动高度
99 let st = t.scrollTop // 当前滚动高度
100
101 // 已经到头部尽头了还要向上滑动,阻止它
102 if (st === 0 && startY < py) {
103 e.preventDefault()
104 }
105
106 // 已经到低部尽头了还要向下滑动,阻止它
107 if ((st === sh - ch) && startY > py) {
108 e.preventDefault()
109 }
110 }, false)
111
112 contentNode.addEventListener(\'touchend\', e => {
113 status = 0
114 }, false)
问题虽然是解决了,但是回头来看,复杂程度和代码量明显增加了一个梯度。
本着简单方便的原则,我们是不是还可以探索其他的方案呢?
既然touch事件判定比较复杂,何不跳出这个框框,另辟蹊径,探索更加合适的方案。
于是,便有了我们的方案三。
来讲讲我的思路,既然我们要阻止页面滚动,那么何不将其固定在视窗(即position: fixed),这样它就无法滚动了,当蒙层关闭时再释放。
当然还有一些细节要考虑,将页面固定视窗后,内容会回头最顶端,这里我们需要记录一下,同步top值。
示例代码:
1 let bodyEl = document.body
2 let top = 0
3
4 function stopBodyScroll (isFixed) {
5 if (isFixed) {
6 top = window.scrollY
7
8 bodyEl.style.position = \'fixed\'
9 bodyEl.style.top = -top + \'px\'
10 } else {
11 bodyEl.style.position = \'\'
12 bodyEl.style.top = \'\'
13
14 window.scrollTo(0, top) // 回到原先的top
15 }
16 }
本文到这里也即将结束了,在这里我强烈推荐一下方案三,原因在于简单、方便、兼容性好,一次封装,受用。
15
2022-09
15
2022-09
15
2022-09
14
2022-09
13
2022-09
13
2022-09