如何模拟js 触发touchstartt事件

一个模拟手机手指滑动事件的jquery插件_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
一个模拟手机手指滑动事件的jquery插件
&&一个模拟手机手指滑动事件的jquery插件
你可能喜欢&nbsp&#8250&nbsp&nbsp&#8250&nbsp
Android 模拟触摸动作MotionEvent事件
模拟触屏事件需要两个步骤,一是获得事件对象MotionEvent,二是分发该事件对象。1.获得事件对象MotionEvent获得一个MotionEvent对象,这里我们使用MotionEvent的obtain()方法,参数如下MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);举个例子:onTouch(mPageView, MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, screenWidth / 2, screenHeight / 2, 0));就表示在屏幕中间模拟一次ACTION_DOWN事件2.分发该事件对象public boolean dispatchTouchEvent(MotionEvent ev)& 这个方法用来分发MotionEvent下面这个示例程序的流程是点击一个button然后触发一个EditText的获取焦点事件一、布局文件showime.xml:&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
&&&android:layout_width="fill_parent"
&&&android:layout_height="fill_parent"
&&&android:orientation="vertical"&
&&&&&&&&&&&&&&&
&&&&&Button android:id="@+id/ButtonIME"
&&&&&&&&android:layout_width="wrap_content"
&&&&&&&&android:layout_height="wrap_content"
&&&&&&&&android:padding="5px"
&&&&&&&&android:text="弹出软键盘输入法" /&
&&&&&&&&&&&
&&&&&EditText android:id="@+id/EditTextIME"
&&&&&&&&android:layout_width="wrap_content"
&&&&&&&&android:layout_height="wrap_content"
&&&&&&&&android:padding="5px"
&&&&&&&&android:text="显示输入法键盘"/&
&/LinearLayout&二、Activity,ShowIME.java文件源码:package org.shuxiang.
import android.app.A
import android.os.B
import android.os.H
import android.os.L
import android.os.M
import android.os.SystemC
import android.view.MotionE
import android.view.V
import android.view.W
import android.view.View.OnClickL
import android.widget.B
import android.widget.EditT
public class ShowIME& extends Activity
&&&&private EditT
&&&&private B
&&&&private static Handler messageH
&&&&@Override
&&&&public void onCreate(Bundle savedInstanceState) {
&&&&&&&&super.onCreate(savedInstanceState);
&&&&&&&&requestWindowFeature(Window.FEATURE_NO_TITLE);
&&&&&&&&setContentView(R.layout.showime);
&&&&&&&&&&&
&&&&&&&&bt = (Button) findViewById(R.id.ButtonIME);
&&&&&&&&bt.setOnClickListener(new OnClickListener()
&&&&&&&&&&&&@Override
&&&&&&&&&&&&public void onClick(View v) {
&&&&&&&&&&&&&&&&// TODO Auto-generated method stub
&&&&&&&&&&&&&&&&et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
&&&&&&&&&&&&&&&&et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
&&&&&&&&&&&&}
&&&&&&&&});&&&&&&&
&&&&&&&&et = (EditText) findViewById(R.id.EditTextIME);
&&&&&&&&&&&
&&&&&&&&&&&
&&&&&&&&Looper looper = Looper.myLooper();
&&&&&&&&messageHandler = new MessageHandler(looper);&&&&&&&
&&&&&&&&//此处的作用是延迟1秒,然后激活点击事件
&&&&&&&&//欢迎转载并说明转自:/a/anzhuokaifa/androidkaifa/9.html
&&&&&&&&new Thread()
&&&&&&&&&&&&@Override
&&&&&&&&&&&&public void run()
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&synchronized(this)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&try
&&&&&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&&&&&wait(1000); //1秒
&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&&&&&catch (InterruptedException e)
&&&&&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&&&&&// TODO Auto-generated catch block
&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&Message message = Message.obtain();
&&&&&&&&&&&&&&&&message.what = 1;
&&&&&&&&&&&&&&&&messageHandler.sendMessage(message);
&&&&&&&&&&&&}
&&&&&&&&}.start();&&&&&&&
&&&&class MessageHandler extends Handler
&&&&&&&&public MessageHandler(Looper looper)
&&&&&&&&&&&&super(looper);
&&&&&&&&@Override
&&&&&&&&public void handleMessage(Message msg)
&&&&&&&&&&&&Bundle bundle = msg.getData();
&&&&&&&&&&&&switch (msg.what)
&&&&&&&&&&&&{
&&&&&&&&&&&&case 1:
&&&&&&&&&&&&&&&&//模拟点击按钮
&&&&&&&&&&&&&&&&bt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
&&&&&&&&&&&&&&&&bt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&//以下代码模拟点击文本编辑框
&&&&&&&&&&&&&&&&//et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
&&&&&&&&&&&&&&&&//et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&default:
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&}
&&&&&&&&&&&&&&&
上一篇: public void drawArc( RectF oval, float startAngle, float sweepAngle,boolean useCenter, Paint paint ) oval :指定圆弧的外轮廓矩形区域。 startAngle: 圆弧起始角度,单位为度。 sweepAngle: 圆弧扫过的角度,顺时针方向,单位为度。 useCenter: 如果为
下一篇: 本文通过FrameLayout和LinearLayout来布局,并通过捕捉onTouchEvent事件来实现画面的随意移动,并同时显示移动后画面坐标。 控制view移动的函数主要是scrollTo和scrollBy,两者的差别如下: scrollTo让我们的layout视图相对于屏幕的左上角进行偏移; scrollB用户名:shahdza
文章数:114
评论数:76
访问量:510427
注册日期:
阅读量:1297
阅读量:3317
阅读量:445412
阅读量:1130983
51CTO推荐博文
【唠叨】&&&&cocos2dx游戏引擎的重点是在于移动设备的跨平台开发,而移动设备上的游戏大部分都是通过屏幕触碰来进行的。比如主菜单的按钮触碰,打飞机中飞机的触碰移动,都需要用到触碰操作。想一想之前讲的菜单按钮CCMenu,菜单项的点击响应事件,其实就是触碰。&&&&cocos2dx引擎中实现触碰的类CCTouchDelegate。而CCLayer类默认继承了CCTouchDelegate接口,所以CCLayer的子类无须再重新使用这些接口,这样就很方便地为我们提供了触摸操作相关的操作。&&&&触碰事件主要分为两类:单点触碰 和 多点触碰。&&&&本节组织结构如下:&&&&&&&&一、单点触碰TargetedDelegate、以及参数CCTouch类。&&&&&&&&二、多点触碰StandardDelegate、以及参数CCSet类。&&&&&&&&三、两类触碰的区别。【Demo下载】&&&&&【3.x】&&&&请移步: 【单点触碰】&&&&单点触碰TargetedDelegate,顾名思义,就是只接受一个点的触摸响应事件。&&&&在使用触碰之前,必须要先注册触碰委托。单点触碰的注册委托函数为addTargetedDelegate。1、开启单点触碰事件addTargetedDelegate&&&&写在onEnter()中。//
/**&&&&&&&&
开启单点触碰TargetedDelegate
注册触碰:addTargetedDelegate("触碰事件委托的对象","优先级","是否拦截触屏事件");
优先级的值越小,就越高越早被响应。
当第3个参数为true时,表示对本次触屏事件进行拦截,也就是说当触屏事件响应了本次触屏委托后,将不会再继续响应其他触碰委托。
void&HelloWorld::onEnter()&
//注册触碰响应事件
CCDirector::sharedDirector()-&getTouchDispatcher()-&addTargetedDelegate(this,0,false);
CCLayer::onEnter();//一定不要忘了调用父类的onEnter
//2、关闭单点触碰事件removeDelegate&&&&写在onExit()中。//
/**&&&&&&&&
关闭单点触碰TargetedDelegate
注销触碰:removeDelegate("注销触碰事件委托的对象");
void&HelloWorld::onExit()
//注销触屏响应事件
CCDirector::sharedDirector()-&getTouchDispatcher()-&removeDelegate(this);
CCLayer::onExit();//一定不要忘了调用父类的onExit
//3、单点触碰响应函数&&&&ccTouchBegan、ccTouchMoved、ccTouchEnded、ccTouchCancelled。&&&&其中ccTouchBegan的返回值为bool类型:&&&&&&&&当返回true时 ,表示继续响应ccTouchMoved、ccTouchEnded事件;&&&&&&&&当返回false时,则不再继续响应这两个事件。//
virtual&bool&ccTouchBegan(CCTouch*&touch,CCEvent*&event);&&&&&&&&//手指碰到屏幕时调用
virtual&void&ccTouchMoved(CCTouch*&touch,CCEvent*&event);&&&&&&&&//手指在屏幕上滑动时调用
virtual&void&ccTouchEnded(CCTouch*&touch,CCEvent*&event);&&&&&&&&//手指离开屏幕是调用
virtual&void&ccTouchCancelled(CCTouch&*pTouch,&CCEvent&*pEvent);&//取消触碰
//4、关于CCTouch类&&&&CCTouch类是用来存储用户触摸屏幕的过程中,其触摸点的相关信息。也就是保存了触碰的整个过程中,手指所在的位置坐标。&&&&需要在.h中添加引用命名空间:using namespace cocos2d; 才可以使用。&&&&常用操作如下://
class&CCTouch&:&public&CCObject&&&&&&&&
CCPoint&getLocation();&&&&&&&&&//返回当前触点的坐标
CCPoint&getPreviousLocation();&//返回前一个触点的坐标
CCPoint&getStartLocation();&&&&//返回开始触碰时的坐标
CCPoint&getDelta();&&&&&&&&&&&&//返回最近两个触点的偏移量坐标
//5、代码实战&&&&接下来将举个例子:使用单点触碰实现精灵的移动。& & 5.1、声明单点触碰响应函数&&&&先在HelloWorld.h中,声明触碰响应函数,以及类的生命周期。//
//触屏事件
virtual&bool&ccTouchBegan(CCTouch*&touch,CCEvent*&event);
virtual&void&ccTouchMoved(CCTouch*&touch,CCEvent*&event);
virtual&void&ccTouchEnded(CCTouch*&touch,CCEvent*&event);
//生命周期
virtual&void&onEnter();
virtual&void&onExit();
//&&&&&&&&5.2、开启、关闭单点触碰事件&&&&在HelloWorld.cpp中编写开启和关闭触碰事件。//
//开启触屏监听
void&HelloWorld::onEnter()&
CCDirector::sharedDirector()-&getTouchDispatcher()-&addTargetedDelegate(this,0,false);
CCLayer::onEnter();&//一定不要忘了
//关闭触屏监听
void&HelloWorld::onExit()
CCDirector::sharedDirector()-&getTouchDispatcher()-&removeDelegate(this);
CCLayer::onExit();&&//一定不要忘了
//&&&&&&&&5.3、创建测试精灵&&&&在HelloWorld::init()中创建一个CCSprite精灵,用于测试触碰。//
//添加一个CCSprite精灵
CCSprite*&sp&=&CCSprite::create("Icon.png");
sp-&setPosition(&midPos&);
this-&addChild(sp,&0,&1);&//tag标记为1
//&&&&&&&&5.4、实现触碰响应函数&&&&ccTouchBegan:将精灵的位置设置到触碰开始的位置。关键函数:touch-&getLocation()&&&&ccTouchMoved:对精灵的位置进行移动。 & & & & & &关键函数:touch-&getDelta()&&&&ccTouchEnded:将精灵的位置还原到触碰开始的位置。关键函数:touch-&getStartLocation()//
//触屏开始ccTouchBegan
bool&HelloWorld::ccTouchBegan(CCTouch*&touch,CCEvent&*&event)&
CCLOG("ccTouchBegan");
//获取CCSprite精灵
CCSprite*&sp&=&(CCSprite*)this-&getChildByTag(1);
//设置精灵的坐标为:&当前触点位置
CCPoint&pTouch&=&touch-&getLocation();
sp-&setPosition(&pTouch&);
//触屏移动ccTouchMoved
void&HelloWorld::ccTouchMoved(CCTouch*&touch,CCEvent*&event)&
CCLOG("ccTouchMoved");
//获取可视区域尺寸大小
CCSize&mysize&=&CCDirector::sharedDirector()-&getVisibleSize();
//获取CCSprite精灵
CCSprite*&sp&=&(CCSprite*)this-&getChildByTag(1);
//实现精灵的触屏移动
CCPoint&pos&=&touch-&getDelta();&&&&&&&&//获得触屏滑动的偏移量
CCPoint&currentPos&=&sp-&getPosition();&//获得精灵的当前坐标
currentPos&=&ccpAdd(currentPos,&pos);&&&//精灵+偏移量&后的坐标
sp-&setPosition(currentPos);&&&&&&&&&&&&//设置触屏移动后的坐标
//触屏结束ccTouchEnded
void&HelloWorld::ccTouchEnded(CCTouch*&touch,CCEvent*&event)&
CCLOG("ccTouchEnded");
//获取CCSprite精灵
CCSprite*&sp&=&(CCSprite*)this-&getChildByTag(1);
//设置精灵的坐标为:&触屏开始时的触点位置
CCPoint&touchStartPos&=&touch-&getStartLocation();
sp-&setPosition(&touchStartPos&);
//&&&&6、运行结果650) this.width=650;" src="/wyfs02/M02/46/9D/wKioL1PzS6fQI6SVAAb7815Wxrw807.gif" title="1.gif" alt="wKioL1PzS6fQI6SVAAb7815Wxrw807.gif" />【多点触碰】&&&&多点触碰StandardDelegate,可以支持多个点的同时触摸响应事件。1、开启多点触碰&&&&多点触碰的委托注册放在onEnter的生命函数中会造成程序异常退出。&&&&所以注册多点触碰都需要重写如下函数:registerWithTouchDispatcher。&&&&开启多点触碰,除了注册触碰委托外,还需要调用setTouchEnabled(true)来启用触碰。//
注册多点触碰,重写registerWithTouchDispatcher。
addStandardDelegate("触碰事件委托的对象","优先级")
void&HelloWorld::registerWithTouchDispatcher()
CCDirector::sharedDirector()-&getTouchDispatcher()-&addStandardDelegate(this,0);
//在HelloWorld::init()中启用多点触碰
this-&setTouchEnabled(true);
//2、关闭多点触碰&&&&与单点触碰的关闭类似,写在onExit()。//
void&HelloWorld::onExit()
//关闭触碰
CCDirector::sharedDirector()-&getTouchDispatcher()-&removeDelegate(this);
CCLayer::onExit();&//一定不要忘了
//3、多点触碰响应函数&&&&ccTouchesBegan、ccTouchesMoved、ccTouchesEnded、ccTouchesCancelled。&&&&其中ccTouchesBegan和单点触碰不同,返回值为void。//
virtual&void&ccTouchesBegan(CCSet*&touches,&CCEvent*&event);&&&&&&&//触碰开始
virtual&void&ccTouchesMoved(CCSet*&touches,&CCEvent*&event);&&&&&&&//触碰移动
virtual&void&ccTouchesEnded(CCSet*&touches,&CCEvent*&event);&&&&&&&//触碰结束
virtual&void&ccTouchesCancelled(CCSet&*pTouches,&CCEvent&*pEvent);&//取消多点触屏
//4、关于CCSet类&&&&CCSet类是一个集合类,和C++中的Set是类似的。&&&&在这里CCSet的数据元素类型为CCTouch,其主要存储的是多点触碰的触摸点CCTouch集合。&&&&使用迭代器CCSetIterator进行CCSet集合中多个触点CCTouch的遍历。//
for&(CCSetIterator&iter&=&touches-&begin()&;iter&!=&touches-&end();&iter++)&
//获取触点后,就和单点一样的处理了
CCTouch*&touch&=&(CCTouch*)(*iter);
//5、代码实战&&&&在电脑中无法看到多点触碰的效果,因为……电脑操作只有一个鼠标650) this.width=650;" src="/hi/face/i_f08.gif" alt="i_f08.gif" />。&&&&下面就讲解一个多点触碰的例子吧,至于效果,自己将代码移植到手机上看吧。。。&&&&5.1、声明多点触碰响应函数&&&&先在HelloWorld.h中,声明多点触碰响应函数,以及类的生命周期。//
//触屏事件
virtual&void&registerWithTouchDispatcher(void);
virtual&void&ccTouchesBegan(CCSet*&touches,CCEvent*&event);
virtual&void&ccTouchesMoved(CCSet*&touches,CCEvent*&event);
virtual&void&ccTouchesEnded(CCSet*&touches,CCEvent*&event);
//生命周期
virtual&void&onExit();
//&&&&&&&&5.2、开启、关闭多点触碰事件&&&&在HelloWorld.cpp中编写开启和关闭多点触碰事件。//
//在init中开启多点触碰
bool&HelloWorld::init()
//开启多点触屏。注意这句话必须要写,否则无法多点触屏
this-&setTouchEnabled(true);
//注册多点触屏
void&HelloWorld::registerWithTouchDispatcher()
CCDirector::sharedDirector()-&getTouchDispatcher()-&addStandardDelegate(this,0);
//注销多点触屏
void&HelloWorld::onExit()
CCDirector::sharedDirector()-&getTouchDispatcher()-&removeDelegate(this);
CCLayer::onExit();&&//不要忘了写!
//&&&&5.3、创建测试精灵&&&&在HelloWorld::init()中创建两个CCSprite精灵,用于测试触碰。//
//添加两个CCSprite精灵,用于多点触屏
CCSprite*&sp1&=&CCSprite::create("Icon.png");
sp1-&setPosition(&ccp(&100,&160&)&);
this-&addChild(sp1,&0,&1);
CCSprite*&sp2&=&CCSprite::create("Icon_gray.png");
sp2-&setPosition(&ccp(&200,&160)&);
this-&addChild(sp2,&0,&2);
//&&&&5.4、实现多点触碰响应函数&&&&ccTouchesMoved中:将精灵的位置设置到触点位置,sp1精灵设置到第0个触点位置;sp2精灵设置到第1个触点位置。&&&&关键函数:&&&&(1)CCTouch* touch = (CCTouch*)(*iter)&&&&(2)touch-&getID()//
//触屏开始,返回值是void
void&HelloWorld::ccTouchesBegan(CCSet*&touches,CCEvent*&event)
CCLOG("ccTouchesBegan");
//触屏移动
void&HelloWorld::ccTouchesMoved(CCSet*&touches,CCEvent*&event)
CCLOG("ccTouchesMoved");
//创建CCSet的迭代器CCSetIterator
CCSetIterator&iter&=&touches-&begin();
//遍历多点触点集合touches
for(&;iter&!=&touches-&end();&iter++)&
//获取触点后,就和单点一样的处理了
CCTouch*&touch&=&(CCTouch*)(*iter);
if(&touch-&getID()&==&0)&&&&&&//控制精灵sp1
CCSprite*&sp1&=&(CCSprite*)this-&getChildByTag(1);
sp1-&setPosition(&touch-&getLocation()&);
else&if(&touch-&getID()&==&1)&//控制精灵sp2
CCSprite*&sp2&=&(CCSprite*)this-&getChildByTag(2);
sp2-&setPosition(&touch-&getLocation()&);
//触屏结束
void&HelloWorld::ccTouchesEnded(CCSet*&touches,CCEvent*&event)
CCLOG("ccTouchesEnded");
//&&&&6、运行结果650) this.width=650;" src="/wyfs02/M01/46/9E/wKiom1PzWOfTmCYbAAusxeY8uuc353.gif" title="2.gif" alt="wKiom1PzWOfTmCYbAAusxeY8uuc353.gif" />7、分析与总结&&&&灰的那张精灵sp2为啥不动啊啊啊!因为电脑中只有一个鼠标……所以只能触碰一个点。650) this.width=650;" src="/hi/face/i_f34.gif" alt="i_f34.gif" />&&&&想要看到多点触碰的效果,就把程序移植到手机上吧。【两类触碰的区别】&&&&(1)单点使用的监听类是CCtargetedTouchDelegate,而多点使用CCtangardTouchDelegate。&&&&(2)多点触控还需要多一个步骤,就是单独写个函数来注册多点触控:registerWithTouchDispatcher。&&&&(3)多点触控要有开启才能用。 this-&setTouchEnabled(true)。&&&&(4)多点触控中的ccTouchesBegan不是布尔型。本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)javascript模拟(触发)鼠标事件 - 隔壁老王 - ITeye博客
博客分类:
  感言:
  苦逼的程序员经常遇到种种奇怪的需求,经常要想些奇怪的方法来解决问题。
  场景:
  最近用一个插件来展示大量图片,该插件封装逻辑很复杂,文档几乎为零。要修改其代码几乎是不可能完成的任务。因此我想要手工触发一个鼠标点击事件,以此激活其内部处理代码,完成相应的操作,不修改插件的任何代码、不用阅读任何插件的代码。
  简单地触发鼠标事件很容易,如果用jQuery就更容易了: $("#container").trigger("click");
  但我需要模拟鼠标在容器的某个位置点击。仔细阅读javascript的文档,找到了激活特定位置鼠标点击事件的方法。经测试,我用的插件收到mouseDown、mouseUp两个事件、且位置没有发生变化时,就会执行我需要的操作。那么我的代码如下:
function simulateClick(){
//点击位置为屏幕中间
var sx= window.innerWidth/2,sy= window.innerHeight/2,cx= sx,cy=
var eventDown = document.createEvent("MouseEvents");
eventDown.initMouseEvent("mousedown",true,true,window,0,
sx,sy,cx,cy,false,false,false,false,0,null);
var eventUp = document.createEvent("MouseEvents");
eventUp.initMouseEvent("mouseup",true,true,window,0,
sx,sy,cx,cy,false,false,false,false,0,null);
$("#container")[0].dispatchEvent(eventDown);
$("#container")[0].dispatchEvent(eventUp);
  后来又用这样方法解决了插件不支持触屏移动的问题。通过附加事件、跟踪输出,发现插件不能很好地响应touchstart、touchmove、touchend事件,其中touchmove事件直接无视,如果触屏后立即移动,那么不会触发mousedown、mousemove、mouseup事件,总之一句话,需要将touchstart/touchmove/touchend转化为mousedown/mousemove/mouseup事件。
  以上这种需要可以用自定义事件、然后触发的机制来解决。
* event为jquery的事件。真正的事件为event.originalEvent
* 使用新的方法处理。
function touchEventToMouseEvent(event,eventType){
if(!event.originalEvent || !event.originalEvent.targetTouches || event.originalEvent.targetTouches.length!=1)
var te = event.originalEvent.targetTouches[0];
var clientX = te.clientX,clientY=te.clientY,screenX=te.screenX,screenY=te.screenY;
var simEvent = new MouseEvent(eventType,{clientX:clientX,clientY:clientY,screenX:screenX,screenY:screenY,button:0,buttons:0});
return simE
function findElm(){
var elm = $(".pivot_layer[title]");
elm.bind("touchmove",function(e){
var simEvent = touchEventToMouseEvent(e,"mousemove");
if(simEvent!=null){
$(this)[0].dispatchEvent(simEvent);
elm.bind("touchstart",function(e){
console.log("touchstart");
var simEvent = touchEventToMouseEvent(e,"mousedown");
if(simEvent!=null){
$(this)[0].dispatchEvent(simEvent);
elm.bind("touchend",function(e){
console.log("touchend");
var simEvent = touchEventToMouseEvent(e,"mouseup");
if(simEvent!=null){
$(this)[0].dispatchEvent(simEvent);
$(function(){
findElm();
  注:initMouseEvent是个将要废弃的方法,推荐使用new MouseEvent()方法来代替。上述代码基于jQuery插件。
浏览: 2885147 次
来自: 北京
启动了 访问不了了啊
报这个错 /********* ...
class DbUtil {
private stat ...
巩大星 写道就是用官方的例子中的kml文件,可以在openla ...
就是用官方的例子中的kml文件,可以在openlayers中正 ...
提供了一种很好地思路,但是为啥tomcat里面访问后,也没有背 ...88702人阅读
近段时间使用html5开发一个公司内部应用,而触摸事件必然是移动应用中所必须的,刚开始以为移动设备上或许也会支持鼠标事件,原来是不支持的,好在webkit内核的移动浏览器支持touch事件,并且打包成app也毫无压力。原本以为touch事件应该跟鼠标事件是一样的道理,实践过程中虽然不难,但还是碰到了不少坑,才发现还是略有区别的。
$(document).bind(touchEvents.touchstart, function (event) {
event.preventDefault();
$(document).bind(touchEvents.touchmove, function (event) {
event.preventDefault();
$(document).bind(touchEvents.touchend, function (event) {
event.preventDefault();
很多博文中称touch的三个事件都有targetTouches,touches以及changedTouches对象列表,其实不然,touchend事件中应该是只有个changedTouches触摸实例列表的,而且这里说明一下,回调函数的event只是一个普通的object对象,实际上event中有一个originalEvent属性,这才是真正的touch事件,这个事件中才存在着上诉三个触摸实例列表,这三个实例存储了触摸事件的位置等等属性,类似于鼠标事件。其他地方基本与鼠标事件是一致的。简单介绍一下这三个触摸列表,touches是在屏幕上的所有手指列表,targetTouches是当前DOM上的手指列表,所以当手指移开触发touchend事件时,event.originalEvent是没有这个targetTouches列表的,而changedTouches列表是涉及当前事件的列表,例如touchend事件中,手指移开。接下来谈谈pc与移动端的适配问题,既然使用html5,当然是看中他的跨平台特性了,不仅仅要ios和android适配,pc上直接打开网页最好也是可以的,但是pc上只支持鼠标事件怎么办。好办,仔细观察上面代码的触摸事件,touchEvents.touchXXX,看如下代码:
var touchEvents = {
touchstart: &touchstart&,
touchmove: &touchmove&,
touchend: &touchend&,
* @desc:判断是否pc设备,若是pc,需要更改touch事件为鼠标事件,否则默认触摸事件
initTouchEvents: function () {
if (isPC()) {
this.touchstart = &mousedown&;
this.touchmove = &mousemove&;
this.touchend = &mouseup&;
若在pc上,则使用鼠标事件,在移动设备中,就使用触摸事件,就这么简单,判断是否pc也很方便,就不做多解释了。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:372354次
积分:2348
积分:2348
排名:第15737名
原创:32篇
转载:14篇
评论:111条
(1)(2)(1)(2)(3)(2)(1)(2)(2)(4)(3)(3)(9)(9)(2)
xuesongtan

我要回帖

更多关于 touchstart 触发两次 的文章

 

随机推荐