使用Google Voice服务可以免费拨打美国或加拿大境内的任何电话,而国际电话(非北美地区)的费用也比传统电话便宜,比如拨打中国大陆的价格为1美分/分钟,香港的价格为2美分/分钟,台湾市话:2美...
JS 33 - 在触控装置侦测手势的滑动方向!
大家好!
今天我们要实作在触控装置中侦测手势的方向。
我们进入今天的主题吧!
备注:前几天和今天的范例将会於近期补上。
程序码
(function (xPos, yPos, xGap, yGap, timeGap, el) {
Felix(document).on('touchstart', start, false);
Felix(document).on('touchmove', move, false);
Felix(document).on('touchend', end, false);
function start(e) {
/* 元素没有 data-swipeable="true" 的属性时,停止事件监听器。 */
if (e.target.dataset.swipeable !== 'true') return;
el = e.target;
timeGap = Date.now();
xPos = e.touches[0].clientX;
yPos = e.touches[0].clientY;
xGap = 0;
yGap = 0;
}
function move(e) {
if (!xPos || !yPos) return;
xGap = xPos - e.touches[0].clientX;
yGap = yPos - e.touches[0].clientY;
}
function end(e) {
/* 滑动到其他元素时,停止事件监听器。 */
if (el !== e.target) return;
let dir = '';
const time = Date.now() - timeGap;
/* 设定触发条件,预设为 10px。 */
const condition = parseInt(el.dataset.swipeCondition || '10', 10);
/* 设定持续条件,预设为 1s。 */
const timeout = parseInt(el.dataset.swipeTimeout || '1000', 10);
(function (x, y) {
if (x > y) {
if (x <= condition || time >= timeout) return;
dir = xGap > 0 ? 'left' : 'right';
} else {
if (y <= condition || time >= timeout) return;
dir = yGap > 0 ? 'up' : 'down';
}
})(Math.abs(xGap), Math.abs(yGap));
xPos = null;
yPos = null;
timeGap = null;
if (dir === '') return;
const event = new CustomEvent(event, {
detail: { direction: dir },
bubbles: true,
cancelable: true
});
el.dispatchEvent(event);
}
})(null, null, null, null, null, null);
实测
范例连结制作中。
差不多也到尾声了。
如果对文章有任何疑问,也欢迎在下方提问和建议!
我是 Felix,我们明天再见!
国内公司、香港公司、美国公司是目前绝大部分跨境卖家的身份选择。国内公司身份自然不必多说,90%+都是。香港和美国公司则少的多。但选择的人多并不表示国内公司就是最佳的经营跨境电商身份选择。 国内公司 首...
在深入探讨提高网站速度之前,让我们探讨一下为什么它对您的小型企业如此重要。 更快的网站意味着: 更好的用户体验 您的网站性能会影响用户的体验 - 当您的网站加载速度更快时,用户更有可能与之互动并花费更...
网站速度优化对于创造积极的用户体验至关重要。 积极的用户体验是快乐用户的营销代言词。 快乐的用户访问您的网站并购买东西。 不满意的用户离开是因为他们厌倦了等待您的网站加载。 营销人员称之为“跳出率”...
为什么网站速度很重要 到目前为止,您应该不需要说服网站速度对您的在线业务至关重要。 这是因为网站性能会影响您的品牌声誉、SEO 排名和转化率。 以下是发生这种情况的主要原因: 品牌口碑👍 老实说,当...