如何解决Android设备上touchend无法触发问题

移动项目开发过程中,经常需要用到滑动的事件来处理一些效果。通常情况下,我们会通过  touchstart->touchmove->touchend   的过程来定义这个事件。这些事件的触发顺序是  touchstart, touchmove, touchmove ….. touchend  。绝大部分平板或手机也正如我们想象的那样有序执行着。但是以 Android 4.0.4 为首的一些可恶分子却有些不听话:他们的 touchend 事件没有如预期的那样触发。

监听这些事件我们会发现,当只是轻点一下屏幕时,touchend 可以正常触发。但是只要当 touchmove 被触发之后,touchend 就不会再被触发了,而且 touchmove 也没有持续触发。

在网上搜集了一些资料显示,这是 Android 上浏览器的 bug

On Android ICS if no preventDefault is called on touchstart or the firsttouchmove,

further touchmove events and the touchend will not be fired.

正如提到的我们只需要在 touchstart 或者 touchmove 里执行一下 e.preventDefault(); 就可以避免这个 bug。但是,问题来了:添加了 preventDefault 之后,正常的 scroll 事件也被屏蔽了!我们意外的发现滚动条也不能用了!

于是,我们开始尝试各种添加 preventDefault 事件的时机:闭包,延迟,判断等一一用上。最终焦点落在了 firsttouchmove 上,于是有了以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var touchY = 0;
$(this)
.on("touchstart", function (e) {
var touch = e.touches[0];
touchY = touch.clientY;
})
.on("touchmove", function (e) {
var touch = e.touches[0];
if (Math.abs(touch.clientY - touchY) < 10) {
e.preventDefault();
}
})
.on("touchend", function () {
// 你的滑动事件
});

基本上主要的思想就是在 touchmove 的时候判断出纵轴的位移量,当小于某个值的时候就认为是在执行左右滑动,且需要执行 preventDefault 来确保 touchend 可以正常触发。

VSCode 插件离线安装方法

1.打开 VSCode 插件市场网址 Extensions for the Visual Studio family of product,输入你想要的插件名称,比如这里我想要安装的是 Markdown All in One 插件

2.点击进入插件主页,点击右侧的 Download Extension 链接,得到下载下来的离线安装包,以 .vsix 为扩展名结尾

3.打开 VSCODE - EXTENSIONS - install from VXIS - reload 即可

如何在CentOS上部署golang

安装

yum install golang

环境变量

新建 go 目录作为项目目录

mkdir -p $HOME/go

用 cat 的方法在尾部增加配置配置 golang 的 GOROOT GOPATH

1
2
3
4
5
cat >>$HOME/.bash_profile<<EOF
export GOROOT=/usr/lib/golang
export GOPATH=\$HOME/golang
export PATH=\$PATH:\$GOROOT/bin
EOF

然后让配置生效

source $HOME/.bash_profile

检查

go env

输出内容即部署成功

更换节点

1
2
3
4
go env -w GO111MODULE=on
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/
设置不走 proxy 的私有仓库,多个用逗号相隔
go env -w GOPRIVATE=*.corp.example.com