javascript - 由于另一个函数,我无法执行函数
问题描述
我想通过向两个方向滑动手指来更改模态窗口中的照片,而互联网上的这段代码有所帮助:
function swipedetect(el, callback) {
var touchsurface = el,
swipedir,
startX,
startY,
distX,
distY,
threshold = 150, //required min distance traveled to be considered swipe
restraint = 100, // maximum distance allowed at the same time in perpendicular direction
allowedTime = 300, // maximum time allowed to travel that distance
elapsedTime,
startTime,
handleswipe = callback || function (swipedir) {}
touchsurface.addEventListener('touchstart', function (e) {
var touchobj = e.changedTouches[0]
swipedir = 'none'
dist = 0
startX = touchobj.pageX
startY = touchobj.pageY
startTime = new Date().getTime() // record time when finger first makes contact with surface
e.preventDefault()
}, false)
touchsurface.addEventListener('touchmove', function (e) {
e.preventDefault() // prevent scrolling when inside DIV
}, false)
touchsurface.addEventListener('touchend', function (e) {
var touchobj = e.changedTouches[0]
distX = touchobj.pageX - startX // get horizontal dist traveled by finger while in contact with surface
distY = touchobj.pageY - startY // get vertical dist traveled by finger while in contact with surface
elapsedTime = new Date().getTime() - startTime // get time elapsed
if (elapsedTime <= allowedTime) { // first condition for swipe met
if (Math.abs(distX) >= threshold && Math.abs(distY) <= restraint) { // 2nd condition for horizontal swipe met
swipedir = (distX < 0) ? 'left' : 'right' // if dist traveled is negative, it indicates left swipe
} else if (Math.abs(distY) >= threshold && Math.abs(distX) <= restraint) { // 2nd condition for vertical swipe met
swipedir = (distY < 0) ? 'up' : 'down' // if dist traveled is negative, it indicates up swipe
}
}
handleswipe(swipedir)
e.preventDefault()
}, false)
};
但是当我想以这种方式使用它时:
let modall = document.getElementById('modall');
swipedetect(modall, function(swipedir) {
if (swipedir == 'right') plusSlides(-1);
if (swipedir == 'left') plusSlides(1);
});
closeModal()
此模式中的功能和其他功能停止工作。有人可以说为什么吗?
PScloseModal()
和其他功能只有在我单击某个元素时才会执行。在我粘贴刷卡代码之前一切正常。刷代码只能在触摸屏上使用,但不能在电脑上使用,因此closeModal()
在 PC 上可以正常使用。
解决方案
推荐阅读
- sql - 关于 SQLite 和级联外键的查询
- android - 我应该为哪些 ABI 编译我的原生代码(Android API >= 21)?
- api - 为什么我不能让距离矩阵 api 在两个国家之间工作
- java - Storm Supervisor 找不到 KafkaSpout 类
- rest - 带有过滤器的 Google Photos API 以获取带有相册信息的照片?
- python - python压缩4Gb bz2 EOFError:已找到流的结尾嵌套子文件夹
- c - 如何使用strstr循环字符串以在C中查找子字符串
- c# - 在 C# 中读取 Excel (xlsx) 文件会在包含 Kanji 的列中添加额外字符
- jenkins - 在 jenkins 管道作业中找不到 npm 错误
- http - 通过 Drive API 从 URL 上传图片