首页 > 解决方案 > 由于另一个函数,我无法执行函数

问题描述

我想通过向两个方向滑动手指来更改模态窗口中的照片,而互联网上的这段代码有所帮助:

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 上可以正常使用。

标签: javascriptjquery

解决方案


推荐阅读