简体   繁体   English

手机触摸事件

[英]Mobile Phone Touch Event

I want to create a slider. 我想创建一个滑块。 Here is my javascript code: 这是我的JavaScript代码:

<div id="ghgh" style='width:50px;height:50px;position:absolute;top:0;left:0;background:#000'>
</div>
<script type="text/javascript">
$(document).bind('mousemove',function(ev){
$('#ghgh').offset({left:(ev.pageX-25)});});
</script>

This, however, only works in computers but not touch screen. 但是,这仅适用于计算机,而不适用于触摸屏。 I have tried to use events like touchmove,slide,scroll,etc. 我试图使用诸如touchmove,slide,scroll等事件。 yet none of them works. 但是它们都不起作用。 What events should I use in order to make it work in touch screens? 我应该使用什么事件才能使其在触摸屏上工作?

First of all: put your styles and scripts in separate files. 首先:将样式和脚本放在单独的文件中。

For touch events, you may have a look at hammer.js 对于触摸事件,您可以看看Hammer.js

Example from their website: 他们网站上的示例:

Hammer(el).on("swipeleft", function() {
    alert('you swiped left!');
});

EDIT: more specific example: 编辑:更具体的例子:

Hammer(document.body).on('drag', function (event) {
    var gesture = event.gesture,
        pageX = gesture.center.pageX,
        pageY = gesture.center.pageY,
        deltaX = gesture.deltaX,
        deltaY = gesture.deltaY;
    // console.log(event); Uncomment this to see all properties of the event object in your webinspector console
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM