惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
T
Threatpost
T
Tor Project blog
AWS News Blog
AWS News Blog
S
Schneier on Security
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Palo Alto Networks Blog
P
Proofpoint News Feed
Vercel News
Vercel News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
V2EX
腾讯CDC
C
CERT Recently Published Vulnerability Notes
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V2EX - 技术
V2EX - 技术
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
Docker
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
Know Your Adversary
Know Your Adversary
宝玉的分享
宝玉的分享
爱范儿
爱范儿
Simon Willison's Weblog
Simon Willison's Weblog
N
News | PayPal Newsroom
Recent Announcements
Recent Announcements
小众软件
小众软件
Project Zero
Project Zero
SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
Cloudbric
Cloudbric
博客园 - Franky
Forbes - Security
Forbes - Security
C
Cisco Blogs
Webroot Blog
Webroot Blog
H
Help Net Security

爱吃肉的猫

那就,再相逢 Butterfly的魔改教程:最新评论页 离歌不夜天 Butterfly的魔改教程:右键菜单 音乐分享 - doi微醺氛围 Butterfly的魔改教程:动态相册页 近况记事 - 11 微信公众号:Ai大模型让回复更具智能化 近况记事 - 10 PWA:让你的网站变成桌面应用APP Healthy Love Butterfly的魔改教程:关于本站 近况记事 - 9 Butterfly的魔改教程:待办清单 TrollStore - 不掉签助手 近况记事 - 8 Twikoo评论回复邮件模版 过一个很特别的七夕 The Young Boy and the Sea
前端分享 - 滑动阻尼效果
亦小封 · 2024-05-17 · via 爱吃肉的猫

在页面上下滚动(滑动)浏览内容时,页面的滚动会随着我们滚轮停止而停止,在某些浏览体验中,会显得略微生硬。
CSS里有个scroll-behavior属性,可以使得滚动变得平滑,但却并不是阻尼效果。

于是在某次冲浪,看到了@JIEJOE的个人网站实现了类似阻尼感的滑动效果。

效果实现

通过为页面添加一个缓冲滑动的效果,我们可以使用一个名为.scrollbox的容器,并结合transition属性来设定滚动的缓动时长。

为了固定视窗,我们需要先将浏览器窗口的位置锁定。页面内容会因为高度超过视窗而出现滚动条,导致页面可以上下滑动。我们通过将页面大小固定为窗口大小,并创建一个与窗口高度一致的容器,实现滚动限制。

接着,添加一个名为.viewbox的容器,并通过CSS的fixed属性将其固定在页面顶部。随后,通过 JavaScript监听滚动事件并动态更新.scrollbox的位置。

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
<!DOCTYPE html>
<html lang="zh-CN">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>滑动阻尼</title>
<style>
* {
font-size: 2vmin;
margin: 0;
padding: 0;
}

::-webkit-scrollbar {
display: none;
}

body {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
background-color: #171717;
}

.scrollbox {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
flex-shrink: 0;
transition: .3s ease-out;
}

.viewbox {
position: fixed;
top: 0;
display: flex;
align-items: flex-start;
width: 100%;
height: 100vh;
overflow: hidden;
}

.box {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-size: calc(18px + 1vw);
font-weight: bold;
}

</style>
</head>

<body>
<div class="viewbox">
<div class="scrollbox">
<div class="box" style="background-color: #17f700;">#17f700</div>
<div class="box" style="background-color: #ffc93e;">#ffc93e</div>
<div class="box" style="background-color: #0084FF;">#0084FF</div>
<div class="box" style="background-color: #FF3842;">#FF3842</div>
<div class="box" style="background-color: #d44040;">#d44040</div>
</div>
</div>
</body>
<script>
let scrollbox = document.getElementsByClassName("scrollbox")[0];
function resize_body() {
let height = scrollbox.offsetHeight;
document.body.style.height = `${height}px`;
};
function scroll() {
scrollbox.style.transform = `translateY(${-scrollY}px)`;
};
window.addEventListener("scroll", scroll);
window.addEventListener("load", resize_body);
window.addEventListener("resize", resize_body);
</script>

</html>

在移动端浏览器中,滑动时可能会遇到一些兼容性问题。这可能与浏览器的默认滚动策略冲突有关。

优化方法是:

  • 在移动设备上使用 scroll-behavior: smooth; 替代阻尼效果。

  • 使用 JS 进行屏幕宽度判断:当 document.body.clientWidth < 768 时,移除阻尼逻辑并恢复默认滚动行为。

参考文献

引用站外地址,注意辨别

JIEJOE

视觉设计者:包括网站、平面、视频、和交互设计

前端分享 - 滑动阻尼效果

本文是转载文章,版权归原作者所有。建议访问原文,转载相关请联系原作者。