使用pointer.js进行移动和桌面事件规范化

The mobile revolution brought in a variety of new challenges, one being interaction and event handling.  We have a set of new touch events and the traditional desktop events, and click events work on both devices, but mousemove and touchmove aren't always in sync, and there are a number of other nuances you run into with different devices.  That's why Mozilla's uber-talented Web Developer Potch created pointer.js, a utility to help developers more easily manage events across devices.

移动革命带来了许多新挑战,其中之一就是交互和事件处理。 我们提供了一组新的触摸事件和传统的桌面事件,单击事件在这两种设备上均有效,但是mousemove和touchmove并不总是同步的,并且在不同设备上会遇到许多其他细微差别。 这就是Mozilla极具才华的Web开发人员Potch创建了pointer.js的原因 ,该实用程序可帮助开发人员更轻松地管理跨设备的事件。

pointer.js文档 (pointer.js Documentation)

The pointer.js documentation explains the project as well and simply as it should, so I wont reinvent the wheel.  Straight from the docs:

该pointer.js文档也很好地解释了该项目,因此我不会重新发明轮子。 直接来自文档:

活动类型 (Types of Events)

The following events are generated:

生成以下事件:

  • pointerdown: based on mousedown/touchstart

    pointerdown :基于mousedown / touchstart

  • pointerup: based on mouseup/touchend

    pointerup :基于mouseup / touchend

  • pointermove: based on mousemove/touchmove

    pointermove :基于mousemove / touchmove

  • pointerleave: based on mouseout/touchleave

    pointerleave :基于mouseout / touchleave

  • pointerclick: a 'fast click' event based on a sequence of the above events. Additional heuristics are applied to determine whether or not to generate a pointerclick.

    pointerclick :基于上述事件序列的“快速单击”事件。 应用其他启发式方法来确定是否生成pointerclick

事件对象 (Event Objects)

pointer events have the following custom properties:

pointer事件具有以下自定义属性:

  • maskedEvent: the event that triggered the pointer event.

    maskedEvent :触发​​指针事件的事件。

  • touch: boolean- is maskedEvent a touch event?

    touch :布尔值-maskedEvent是否为触摸事件?

  • mouse: boolean- is maskedEvent a mouse event?

    mouse :布尔值-maskedEvent是鼠标事件吗?

  • x: page-normalized x coordinate of the event.

    x :事件的页面标准化x坐标。

  • y: page-normalized y coordinate of the event.

    y :事件的页面标准化y坐标。

Simple APIs are the best APIs, and Potch nailed this one.

简单的API是最好的API,而Potch则选择了这一点。

pointer.js用法 (pointer.js Usage)

Here are a few example usages of pointer.js:

这是pointer.js的一些用法示例:


var element = document.getElementById("myElement");

element.addEventListener("pointerdown", function(detailedEvent) {
	// Do something
});

element.addEventListener("pointerleave", function(detailedEvent) {
	// Do something
});

element.addEventListener("pointerclick", function(detailedEvent) {
	// Do something
	if(detailedEvent.mouse) {
		// desktop
	}
	else {
		// mobile
	}
});


Potch wrote the JS such that you are adding custom event listeners in a traditional way via addEventListener.  I also love that Event object is true, not a new object that provides the original event as one property.  The additional information added to the object is useful in identifying the platform and which event was actually fired.

Potch编写了JS,这样您就可以通过addEventListener以传统方式添加自定义事件侦听器。 我也很喜欢Event对象为true,而不是一个将原始事件作为一个属性提供的新对象。 添加到对象的其他信息对于标识平台以及实际触发了哪个事件很有用。

Event normalization was one of the original reasons for the first event frameworks...and they didn't even have to deal with mobile devices!  Potch has done well with this event normalization utility and I can't wait to use it on my next redesign!

事件规范化是第一个事件框架的最初原因之一,而且它们甚至不必处理移动设备! Potch使用此事件规范化实用程序做得很好,我迫不及待想在下一次重新设计时使用它!

翻译自: https://davidwalsh.name/pointer-event

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vis.js是一个用于可视化网络、图表和数据的JavaScript库。它提供了许多功能,包括节点移动和位置设置。以下是vis.js节点移动相关的教程。 1. 开始使用vis.js 在开始使用vis.js之前,需要引入vis.js库和vis.css样式文件。可以从vis.js官方网站上下载最新版本的vis.js库和vis.css样式文件,然后将它们添加到HTML页面中。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vis.js Node Move Tutorial</title> <link rel="stylesheet" type="text/css" href="vis.css"> <script type="text/javascript" src="vis.js"></script> </head> <body> <div id="network"></div> <script type="text/javascript"> // your code here </script> </body> </html> ``` 2. 创建一个简单的网络 在vis.js中,可以使用vis.Network对象来创建网络。首先,需要创建一个HTML元素,用于显示网络。然后,可以创建一个vis.Network对象,将其绑定到该HTML元素,并设置节点和边的数据。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vis.js Node Move Tutorial</title> <link rel="stylesheet" type="text/css" href="vis.css"> <script type="text/javascript" src="vis.js"></script> </head> <body> <div id="network"></div> <script type="text/javascript"> // create an array with nodes var nodes = new vis.DataSet([ {id: 1, label: 'Node 1'}, {id: 2, label: 'Node 2'}, {id: 3, label: 'Node 3'}, {id: 4, label: 'Node 4'}, {id: 5, label: 'Node 5'} ]); // create an array with edges var edges = new vis.DataSet([ {from: 1, to: 2}, {from: 1, to: 3}, {from: 2, to: 4}, {from: 2, to: 5} ]); // create a network var container = document.getElementById('network'); var data = {nodes: nodes, edges: edges}; var options = {}; var network = new vis.Network(container, data, options); </script> </body> </html> ``` 3. 启用节点移动 要启用节点移动,需要在创建vis.Network对象时,将moveable选项设置为true。可以通过options对象来设置该选项。 ```javascript var options = {moveable: true}; var network = new vis.Network(container, data, options); ``` 现在,可以在网络中拖动节点并放置到新的位置。 4. 监听节点移动事件 如果希望在节点移动时执行一些代码,可以监听节点移动事件。可以通过vis.Network对象的on方法来注册事件处理程序。 ```javascript network.on('dragEnd', function(params) { console.log(params.nodes[0] + ' was moved to x:' + params.pointer.canvas.x + ', y:' + params.pointer.canvas.y); }); ``` 上面的代码将在节点移动结束时,将节点ID和新位置的坐标输出到控制台。 5. 限制节点移动范围 有时,希望限制节点移动的范围,以保持网络的视觉效果。可以通过设置节点位置的边界来实现。可以在options对象中设置nodes选项,将其设置为一个对象,该对象包含每个节点的初始位置和位置边界。 ```javascript var options = { moveable: true, nodes: { 1: {x: 100, y: 100, fixed: true, borderWidth: 1, shape: 'circle', color: {background:'red', border:'black'}}, 2: {x: 300, y: 100, borderWidth: 1, shape: 'circle', color: {background:'blue', border:'black'}, physics: false}, 3: {x: 100, y: 300, borderWidth: 1, shape: 'circle', color: {background:'green', border:'black'}, physics: false}, 4: {x: 300, y: 300, borderWidth: 1, shape: 'circle', color: {background:'yellow', border:'black'}, physics: false} } }; ``` 如上所示,节点1的位置已经被固定,而其他节点则没有被固定,并且可以在屏幕上移动。 6. 停用节点移动 要停用节点移动,设置moveable选项为false即可。 ```javascript var options = {moveable: false}; var network = new vis.Network(container, data, options); ``` 现在,节点将无法移动。 以上就是vis.js节点移动相关的教程。通过这些教程,您可以在vis.js中轻松地启用节点移动,监听节点移动事件,限制节点移动范围,并停用节点移动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值