我想向右滑动到下一页,向右滑动到上一页,我搜索了很长时间,似乎什么都没有用
hammer.js似乎是最有前途的
hammertime.get('swipe').set({ direction: Hammer. });下面建议的脚本似乎不起作用,还有其他建议吗?
发布于 2020-02-16 12:08:42
const hammerTime = new Hammer(someElement)
hammerTime.on('swipeleft swiperight', (event) => {
switch(event.type) {
case 'swipeleft':
window.location.href = "https://someurl.com"
break
case 'swiperight':
window.location.href = "https://someotherurl.com"
break
}
})或
hammerTime.on('swipeleft', (event) => {
window.location.href = "https://someurl.com"
})
hammerTime.on('swiperight', (event) => {
window.location.href = "https://someotherurl.com"
})发布于 2020-02-23 17:58:22
我可能已经找到了一种对我的情况有效的解决方案。
window.addEventListener('load', function() {
var el = document.getElementsByTagName('body')[0];
var hammertime = Hammer(el);
hammertime.get('swipe').set({ direction: Hammer.DIRECTION_HORIZONTAL });
/* the magic... */
hammertime.on('swipe', function(ev) {
var direction = '';
switch(ev.direction) {
case Hammer.DIRECTION_LEFT:
var right = document.querySelector('.right');
var href_right = right.getAttribute('href');
document.location.href=href_right;
break;
case Hammer.DIRECTION_RIGHT:
var left = document.querySelector('.left');
var href_left = left.getAttribute('href');
document.location.href=href_left;
break;
}
});
}, false);
这个由Hammer授权的js脚本将获取滑动手势的方向,然后检索与获取其属性href相对应的元素的类,并将用户重定向到该元素。
<div class="thumb-first">
<!-- Swipe -->
<div class="swipe">
<a class="left" href="https://www.youtube.com/"><span></span></a>
<a class="active" href="#"><span></span></a>
<a class="right" href="https://stackoverflow.com/"><span></span></a>
</div>
希望它能帮上忙!
https://stackoverflow.com/questions/60245172
复制相似问题