千家信息网

jQuery如何实现触摸事件

发表于:2025-02-13 作者:千家信息网编辑
千家信息网最后更新 2025年02月13日,这篇文章主要为大家展示了"jQuery如何实现触摸事件",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"jQuery如何实现触摸事件"这篇文章吧。触摸事件//
千家信息网最后更新 2025年02月13日jQuery如何实现触摸事件

这篇文章主要为大家展示了"jQuery如何实现触摸事件",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"jQuery如何实现触摸事件"这篇文章吧。

触摸事件

// Define some variablesvar ball = $('
').appendTo('body'),startPosition = {}, elementPosition = {};// Listen for mouse and touch eventsball.on('mousedown touchstart',function(e){ e.preventDefault(); // Normalizing the touch event object e = (e.originalEvent.touches) ? e.originalEvent.touches[0] : e; // Recording current positions startPosition = {x: e.pageX, y: e.pageY}; elementPosition = {x: ball.offset().left, y: ball.offset().top}; // These event listeners will be removed later ball.on('mousemove.rem touchmove.rem',function(e){ e = (e.originalEvent.touches) ? e.originalEvent.touches[0] : e; ball.css({ top:elementPosition.y + (e.pageY - startPosition.y), left: elementPosition.x + (e.pageX - startPosition.x), }); });});ball.on('mouseup touchend',function(){ // Removing the heavy *move listeners ball.off('.rem');});

以上是"jQuery如何实现触摸事件"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0