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

发布于 2024-12-20 21:46:37 字数 1423 浏览 4 评论 0原文

我想在触发 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

为什么我在两种方法中没有得到相同的值?我指向同一个变量!

I want to get touchstart.pageX value when touchend event is fired but I am not getting exact value. Its giving me pageX value of touchend event. What I am doing wrong?

(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);

After swipe on element I am getting this in console (swipe from left-right)

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

Whey I am not getting same value in both methods? I am pointing to same variable though!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

篱下浅笙歌 2024-12-27 21:46:37

您应该在结束事件中获取触摸变量的 .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

You should be getting the .target value of the touch variable in your end event. That's where the touch started from. You don't even need the touchStart variable, really. The touch in the end event contains all the information you need.

//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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文