当前位置:网站首页>web移动端:touchmove实现局部滚动
web移动端:touchmove实现局部滚动
2022-07-25 10:30:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
总结一下最近项目用到的一个功能点,具体要求如下: body中会呈现一个可滚动的长页面,在点击某个按钮的时候,会出现一个弹出框,由于弹出框的内容较长,会出现滚动条,但是要保证位于弹框下部的body在弹框滚动的时候不触发滚动事件
实现效果图如下:
一.css部分
html,body{ margin:0; padding: 0; }
.btn{ position: fixed; bottom: 0; width: 100%; padding: .5rem 0; background-color: pink; text-align: center; color: #fff; font-weight: 700; }
.replyCeng{ display: none; position: fixed; top:0; width:100%; height: 100vh; background-color: rgba(0,0,0,0.6); z-index: 100; }
.replyContainer{ background-color: #fff; position: absolute; bottom: 0; width: 100%; height: 24rem; text-align: center; overflow: hidden; }
.replyList{ position: absolute; top:3.2rem; width: 100%; }
.replyTitle{ position: absolute; top: 0; width:100%; padding:1.0625rem 0; border-bottom:1px solid #e0e0e2; z-index: 202; color: #575b61; font-size: .9375rem; font-weight: 700; background-color: #fff; }
.closeReply{ position: absolute; width: 1rem; height: 1rem; top: .25rem; right: .25rem; z-index: 202; margin: 1rem; }
.reply-item{ padding:.9375rem 1.625rem; font-size: .9375rem; line-height: 1.5; color: #666666; border-bottom:1px solid #e0e0e2; }
.replyBtn{ /* padding: 1.0625rem 1.625rem;*/ font-size: .9375rem; color: #1daffa; font-weight: 700; width:100%; position: absolute; bottom: 0; background-color: #fff; z-index: 202; border-top: 1px solid #e0e0e2; }
.replyBtn div{ margin: 1.0625rem 1.625rem; padding: .6rem; /* width: 100%;*/ border: 1px solid #1daffa; border-radius: .3rem; }二.html部分
<div>
<img src="image/langImg.jpg" style="width:100%;height:auto;"/>
</div>
<div class="btn" onclick="replyCengShow()">button</div>
<div class="replyCeng">
<div class="replyContainer">
<div class="replyTitle">睡前小贴士</div>
<img class="closeReply" src="image/close.png" />
<div class="replyList" id="wrapper">
<div class="reply-item" onclick="selectReply()">要注意哦,睡觉前不要吃水果,可以适当喝牛奶</div>
<div class="reply-item">要注意哦,睡觉前不要吃水果,可以适当喝牛奶</div>
<div class="reply-item">要注意哦,睡觉前不要吃水果,可以适当喝牛奶</div>
<div class="reply-item">要注意哦,睡觉前不要吃水果,可以适当喝牛奶</div>
<div class="reply-item">把黑暗的光线试想成大自然的安眠药,从而提示你的身体去大量地制造褪黑激素,这是一种能帮助人平静下来的激素。来自旧金山的专门从事焦虑和失眠的心理学家、心理学博士Steve Orma表示,在入睡前将灯关掉能提高褪黑激素的产量,这样人就会感到倦意。支持这一说法的研究有:2011年发表在《临床内分泌与代谢》期刊上的一篇研究报告表明,在黄昏时刻到睡觉之前暴露在明亮的灯光下会极大地压缩褪黑激素的产量,使人变得极其兴奋。</div>
<div class="reply-item">把黑暗的光线试想成大自然的安眠药,从而提示你的身体去大量地制造褪黑激素,这是一种能帮助人平静下来的激素。来自旧金山的专门从事焦虑和失眠的心理学家、心理学博士Steve Orma表示,在入睡前将灯关掉能提高褪黑激素的产量,这样人就会感到倦意。支持这一说法的研究有:2011年发表在《临床内分泌与代谢》期刊上的一篇研究报告表明,在黄昏时刻到睡觉之前暴露在明亮的灯光下会极大地压缩褪黑激素的产量,使人变得极其兴奋。</div>
</div>
<div class="replyBtn">
<div>管理</div>
</div>
</div>
</div>三.js部分
1.获得触屏的初始坐标
$(".replyList").on("touchstart",function(e){
startX = e.originalEvent.changedTouches[0].pageX,
startY = e.originalEvent.changedTouches[0].pageY;
});2.移动屏幕添加相应事件 touchmove事件的要点主要是移动方向的判断和滑动边界的判断 <1>移动方向的判断
var replyListTop=parseInt($(".replyList").css("top"));
$(".replyList").bind("touchmove",function(e){
moveEndX = e.originalEvent.changedTouches[0].pageX;
moveEndY = e.originalEvent.changedTouches[0].pageY;
var moveX;
var moveY;
moveX=moveEndX-startX;
moveY=moveEndY-startY;
//向上滑动
if(moveY < 0){
}
//向下滑动
else if(moveY > 0){
}
});<2>移动边界的判断 A.向上滑动到上边界
注:replyScrollTop为文中replyListTop
此时replyListTop为负数
canSeeHeight == replyHeight + replyListTop //(此时正好处于边界状态)如果再往上滑动,我们将会看到越来越少的内容,也就是:
canSeeHeight > replyHeight + replyListTop于是得到上边界值:
hasTop= canSeeHeight <= $(this).height()+replyListTop ? true:falseB.向下滑动到下边界
由于滚动区域是包裹在replyContainer里面的,所以滚动区域的初始top值为标题区域的高度,只要标题区域的高度 == 滚动区域的top值 ,就代表向下滑动到下边界
hasBottom = 3.2*fontHeight >= replyListTop ? true:false ;//title区域用rem布局,fontHeight为html字体大小//向上滑动
if(moveY < 0 && hasTop){
$(".replyList").css("top",replyListTop+"px");
replyListTop= replyListTop+moveY;
}
//向下滑动
else if(moveY > 0 && hasBottom){
$(".replyList").css("top",replyListTop);
replyListTop= replyListTop+moveY;
}<3>限制滑动过大越界
//向上滑动
if(moveY < 0 && hasTop){
$(".replyList").css("top",replyListTop+"px");
replyListTop= replyListTop+moveY;
//判断加上移动后的位置是否越界,如果越界,以边界值为top值
replyListTop= canSeeHeight <= ($(this).height()+replyListTop) ? replyListTop : (-Math.abs($(this).height()-canSeeHeight));
}
//向下滑动
else if(moveY > 0 && hasBottom){
$(".replyList").css("top",replyListTop);
replyListTop= replyListTop+moveY;
//判断加上移动后的位置是否越界,如果越界,以边界值为top值
replyListTop= 3.2*fontHeight >= replyListTop ? replyListTop : 3.2*fontHeight;
}<4>js完整代码
function replyCengShow(){
var startX;
var startY;
var moveEndX;
var moveEndY;
var testLeft;
var fontHeight=parseInt($("html").css("fontSize"));
$(".replyCeng").css("display","block");
$('body').bind("touchmove",function(e){
e.preventDefault();
});
$(".replyList").on("touchstart",function(e){
startX = e.originalEvent.changedTouches[0].pageX,
startY = e.originalEvent.changedTouches[0].pageY;
});
var replyListTop=parseInt($(".replyList").css("top"));
$(".replyList").bind("touchmove",function(e){
moveEndX = e.originalEvent.changedTouches[0].pageX;
moveEndY = e.originalEvent.changedTouches[0].pageY;
var moveX;
var moveY;
moveX=moveEndX-startX;
moveY=moveEndY-startY;
canSeeHeight=$(".replyContainer").height()-($(".replyBtn").height()+2.125*fontHeight);
hasTop= canSeeHeight <= $(this).height()+replyListTop ? true:false
hasBottom = 3.2*fontHeight >= replyListTop ? true:false ;
//向上滑动
if(moveY < 0 && hasTop){
$(".replyList").css("top",replyListTop+"px");
replyListTop= replyListTop+moveY;
replyListTop= canSeeHeight <= ($(this).height()+replyListTop) ? replyListTop : (-Math.abs($(this).height()-canSeeHeight));
}
//向下滑动
else if(moveY > 0 && hasBottom){
$(".replyList").css("top",replyListTop);
replyListTop= replyListTop+moveY;
replyListTop= 3.2*fontHeight >= replyListTop ? replyListTop : 3.2*fontHeight;
}
});
$(".closeReply").unbind("click").bind("click",function(){
$(".replyCeng").css("display","none");
$('body').unbind("touchmove");
});
}四.不足
在滚动的过程中能够感到明显的卡顿
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127092.html原文链接:https://javaforall.cn
边栏推荐
- I, AI doctoral student, online crowdfunding research topic
- 数字孪生万物可视 | 联接现实世界与数字空间
- NowCoderTOP7-11——持续更新ing
- [cloud enjoys freshness] community weekly · Vol 72 - the first opening ceremony of the 2022 Huawei developer competition in China was launched; Huawei cloud koomessage is in hot public beta
- Shell 脚本参数传递时有 \r 换行符问题
- The integration of two in one has a long way to go
- [树] 100. 相同的树
- 为什么重写equals()方法必须要重写hashCode()方法
- Reinforcement Learning 强化学习(三)
- 游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)
猜你喜欢
Learning Weekly - total issue 63 - an open source local code snippet management tool

SQL语言(四)

哥廷根大学提出CLIPSeg:一个使用文本和图像prompt能同时作三个分割任务的模型

Flask framework - Message flash

SQL语言(三)

Flask framework -- flask caching

Learn NLP with Transformer (Chapter 8)

NowCoderTOP7-11——持续更新ing

从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计
![[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)](/img/a0/9110b83ff5c7965809bbc9f3948956.jpg)
[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)
随机推荐
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】
Openstack Skyline 组件安装
Learn NLP with Transformer (Chapter 2)
feign客户端请求之LoadBalancerLifecycle生命周期
Google Earth Engine——统计逐年土地分类的频率
The B2B2C multi merchant system has rich functions and is very easy to open!!!
Learn NLP with Transformer (Chapter 6)
Digital twin everything can be seen | connecting the real world and digital space
二合一的集度,任重道远
新能源销冠宏光MINIEV,有着怎样的产品力?
AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic
HCIP(12)
【Servlet】请求的解析
MySQL master-slave replication and read-write separation
上周热点回顾(7.18-7.24)
Last week's hot review (7.18-7.24)
PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years
从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计