首页 技术 正文
技术 2022年11月6日
0 收藏 350 点赞 658 浏览 2499 个字

本来想用在北京欢乐谷手机上用touch事件来模拟局部左右内容滚动里,但在touchmove上下滚动时由于禁止了默认事件而body滚动条不能滚动,虽然可以根据touchmove的坐标来判断方向,但体验效果不理想。

后来在查询相关资料后原来可以直接在css中使用overflow:auto;出来的滚动条用CSS3的-wekit-scrollbar{display:none}来隐藏;

*拓展
*::-webkit-scrollbar 滚动条整体部分
*::-webkit-scrollbar-thumb 滚动条里面的小方块,能向上向下移动(或往左往右移动,取决于是垂直滚动条还是水平滚动条)
*::-webkit-scrollbar-track 滚动条的轨道(里面装有Thumb)
*::-webkit-scrollbar-button 滚动条的轨道的两端按钮,允许通过点击微调小方块的位置。
*::-webkit-scrollbar-track-piece 内层轨道,滚动条中间部分(除去)
*::-webkit-scrollbar-corner 边角,即两个滚动条的交汇处
*::-webkit-resizer 两个滚动条的交汇处上用于通过拖动调整元素大小的小控件

touch事件来模拟局部左右内容滚动

判断很简单,touchmove的最后坐标减去touchstart的起始坐标,X的结果如果正数,则说明手指是从左往右划动;X的结果如果负数,则说明手指是从右往左划动;Y的结果如果正数,则说明手指是从上往下划动;Y的结果如果负数,则说明手指是从下往上划动。

if ( X > 0 ) {
alert("left 2 right");
}
else if ( X < 0 ) {
alert("right 2 left");
}
else if ( Y > 0) {
alert("top 2 bottom");
}
else if ( Y < 0 ) {
alert("bottom 2 top");
}
else{
alert("just touch");
}

这再逻辑上没有任何问题。但在实际操作中,手指的上下滑动其实是很难直上直下的,只要稍微有点斜,就会被X轴的判断先行接管。
那么接下来加上特别的判断技巧:增加的判断也很简单,无非就是判断哪个的差值比较大。

if ( Math.abs(X) > Math.abs(Y) && X > 0 ) {
alert("left 2 right");
}
else if ( Math.abs(X) > Math.abs(Y) && X < 0 ) {
alert("right 2 left");
}
else if ( Math.abs(Y) > Math.abs(X) && Y > 0) {
alert("top 2 bottom");
}
else if ( Math.abs(Y) > Math.abs(X) && Y < 0 ) {
alert("bottom 2 top");
}
else{
alert("just touch");
}

code

function touchTable(){
var moveTable = document.getElementById("showtime_bj"); //item_ID
var $win =$(window);
var tableWidth = moveTable.offsetWidth;
var halfWidth = tableWidth/2;
var objTouches = null;
var startx,starty,tableLeft,distx=0,disty=0;
console.log(tableWidth) //绑定touchstart事件
moveTable.addEventListener("touchstart",touchStart,false);
function touchStart(e){
objTouches = e.changedTouches[0];
tableLeft = parseInt(moveTable.style.left)
startx = parseInt(objTouches.pageX);
starty = parseInt(objTouches.pageY);
// document.body.addEventListener('touchmove', bodyScroll, false);
} //绑定touchmove事件
moveTable.addEventListener("touchmove",touchMove,false);
function touchMove(e){
objTouches = e.changedTouches[0];
distx = parseInt(objTouches.pageX) - startx;
disty = parseInt(objTouches.pageY) - starty; //判断touch滑动的方向
if (disty > 10 || disty < -10) {
document.body.removeEventListener('touchmove', bodyScroll, false); //向上向下滑动时恢复body的滚动条事件
}else if (distx > 0 ||distx < 0) {
document.body.addEventListener('touchmove', bodyScroll, false); //向上向下滑动时取消body的滚动条事件
moveTable.style.left = ((tableLeft + distx < -halfWidth) ? -halfWidth : (tableLeft + distx > 0) ? 0 : tableLeft + distx) + "px";
}
}
//绑定touchend,touchcancel事件,当手指离开时事件
moveTable.addEventListener("touchend touchcancel",touchEnd,false);
function touchEnd(e){
document.body.removeEventListener('touchmove', bodyScroll, false); //恢复body的滚动条事件
} function bodyScroll(e){
e.preventDefault();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,701
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741