移动端上拉抽屉效果

217 字
1 分钟
移动端上拉抽屉效果

移动端抽屉#

移动端上拉抽屉是一种常见的交互效果,可以在屏幕底部显示一个可拖动的视图,用于展示更多的内容或功能。实现这种效果的代码有多种方式,这里介绍一种基于CSS和JavaScript的demo,代码和演示效果如下。

demo仅在移动端有效,pc端无效

<!DOCTYPE html>
<html>
<head>
<title>移动端拖住增加元素高度</title>
<style>
#drag-element {
width: 100%;
height: 200px;
background-color: rgb(46, 228, 197);
margin: 50px auto;
position: absolute;
bottom: 0;
max-height: 90%;
}
</style>
</head>
<body>
<div id="drag-element"></div>
<script>
const element = document.getElementById('drag-element');
let initialTouchPosition;
let initialElementHeight;
element.addEventListener('touchstart', (event) => {
initialTouchPosition = event.touches[0].clientY;
initialElementHeight = element.offsetHeight;
});
element.addEventListener('touchmove', (event) => {
const currentTouchPosition = event.touches[0].clientY;
const touchDistance = initialTouchPosition - currentTouchPosition;
const newElementHeight = initialElementHeight + touchDistance;
element.style.height = `${newElementHeight}px`;
});
element.addEventListener('touchend', () => {
initialTouchPosition = null;
initialElementHeight = null;
});
</script>
</body>
</html>

DEMO#

示例如下,打开移动端抽屉DEMO查看

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

移动端上拉抽屉效果
https://xiyu.pro/posts/30/
作者
席宇
发布于
2023-11-04
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
席宇
是你的皮尔王先藐视法律。
公告
欢迎来到我的博客!
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
35
分类
3
标签
22
总字数
25,813
运行时长
0
最后活动
0 天前

目录