如何在touchend事件中获取touchstart的值?

3

当触发touchend事件时,我想获得touchstart.pageX的值,但是我没有获得确切的值。它给我touchend事件的pageX值。我做错了什么?

(function($){

    $.fn.extend({ 

        //pass the options variable to the function
        swipeTest: function(options) {
            var touchStart;            
            var options =  $.extend(defaults, options);

            //initilaized objects
            function init(thisObj){
                thisObj.addEventListener('touchstart', function(e) {
                    var touch = e.touches[0] || e.changedTouches[0];
                    touchStart = touch;
                    console.log('Value of touchStart.pageX in touchstart method: ' + touchStart.pageX);
                }, false);

                thisObj.addEventListener('touchend', function(e) {
                    var touch = e.touches[0] || e.changedTouches[0];
                    console.log('Value of touchStart.pageX in touchend method: ' + touchStart.pageX);
                }, false);
            }

            return this.each(function() {
                init(this);   

            });
        }
    });

})(jQuery);

在我从左到右滑动元素后,控制台显示以下内容。
Value of touchStart.pageX in touchstart method: 132
Value of touchStart.pageX in touchend method: 417
Value of touchStart.pageX in touchstart method: 32
Value of touchStart.pageX in touchend method: 481

为什么两种方法返回的值不同呢?虽然我指向了同一个变量!!
1个回答

1

在结束事件中,您应该获取触摸变量的.target值。那是触摸开始的地方。实际上,您甚至不需要touchStart变量。结束事件中的触摸包含了您所需的所有信息。

//in your touchend handler, after touch = blah blah blah
var startnode = touch.target; //gets you the starting node of the touch
var x = startnode.pageX
var y = startnode.pageY

不,我并不困惑这些。问题是为什么touchStart.pageX会给出touchEnd事件的值。 - coure2011
当 touchend 触发时,我想要 touchStart 的值。 - coure2011
啊,好的,现在更有意义了。请查看更新后的答案。 - Ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接