简体   繁体   English

当用户触摸iPhone上的HTML元素时,我该如何反应?

[英]How can I react when a user touches an HTML element on an iPhone?

I'm displaying some HTML content in my iPhone app using a UIWebView. 我正在使用UIWebView在我的iPhone应用程序中显示一些HTML内容。 I have an image link, and I want it to change when the user touches it - at the moment the user puts a finger on the screen, rather than waiting until they lift their finger back off. 我有一个图像链接,我希望它在用户触摸时更改 - 在用户将手指放在屏幕上的那一刻,而不是等到他们将手指抬起来。

What CSS or JavaScript concept could accomplish this? 什么CSS或JavaScript概念可以实现这一目标? I've looked at the hover and active states in CSS, but they don't seem to be what I'm after: hover relates to touch-up rather than touch-down, while active seems to have no effect at all. 我已经看过CSS中的hoveractive状态,但它们似乎并不是我所追求的: hover与触摸相关而不是触及,而active似乎根本没有效果。

You could try this. 你可以试试这个。

I think it should be what you are looking for! 我认为它应该是你要找的!

http://articles.sitepoint.com/article/iphone-development-12-tips/2 http://articles.sitepoint.com/article/iphone-development-12-tips/2

8: Touch Events 8:触摸事件

Of course, you use your iPhone with a finger instead of a mouse; 当然,你用手指而不是鼠标来使用你的iPhone; rather than clicking, you tap. 点击,而不是点击。 What's more, you can use several fingers to touch and tap. 更重要的是,您可以使用多个手指触摸和点按。 On the iPhone, mouse events are replaced by touch events. 在iPhone上,鼠标事件被触摸事件取代。 They are: 他们是:

  • touchstart
  • touchend
  • touchmove
  • touchcancel (when the system cancels the touch) touchcancel (当系统取消触摸时)

When you subscribe to any of those events, your event listener will receive an event object. 当您订阅任何这些事件时,您的事件侦听器将收到一个事件对象。 The event object has some important properties, such as: 事件对象具有一些重要属性,例如:

  • touches — a collection of touch objects, one for each finger that touches the screen. touches - 触摸对象的集合,每个手指触摸屏幕一个。 The touch objects have, for example, pageX and pageY properties containing the coordinates of the touch within the page. 例如,触摸对象具有包含页面内触摸坐标的pageX和pageY属性。
  • targetTouches — works like touches, but only registers touches on a target element as opposed to the whole page. targetTouches - 像触摸一样工作,但只注册目标元素而不是整个页面。

The next example is a simple implementation of drag and drop. 下一个示例是拖放的简单实现。 Let's put a box on a blank page and drag it around. 让我们在空白页面上放一个框并拖动它。 All you need to do is subscribe to the touchmove event and update the position of the box as the finger moves around, like so: 您需要做的就是订阅touchmove事件并在手指移动时更新框的位置,如下所示:

 window.addEventListener('load', function() { var b = document.getElementById('box'), xbox = b.offsetWidth / 2, // half the box width ybox = b.offsetHeight / 2, // half the box height bstyle = b.style; // cached access to the style object b.addEventListener('touchmove', function(event) { event.preventDefault(); // the default behaviour is scrolling bstyle.left = event.targetTouches[0].pageX - xbox + 'px'; bstyle.top = event.targetTouches[0].pageY - ybox + 'px'; }, false); }, false); 

The touchmove event listener first cancels the default behavior of the finger move—otherwise Safari will scroll the page. touchmove事件侦听器首先取消手指移动的默认行为 - 否则Safari将滚动页面。 The collection event.targetTouches contains a list of data for each finger currently on the target div element. 集合event.targetTouches包含当前目标div元素上每个手指的数据列表。
We only care about one finger, so we use event.targetTouches[0] . 我们只关心一根手指,所以我们使用event.targetTouches[0] Then pageX gives us the X coordinate of the finger. 然后pageX给我们手指的X坐标。 From this value we subtract half the width of the div so that the finger stays in the center of the box. 从这个值我们减去div宽度的一半,使手指停留在盒子的中心。

Hope it helps! 希望能帮助到你!

尝试使用Javascript“onMouseDown”,希望移动版Safari能够启动该事件。

<a href="#any_URL" onMouseDown="callSomeFunction();return true;">Link</a>

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

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