简体   繁体   English

如何使用jquery在移动网站中模拟桌面网站的键盘事件?

[英]How Keyboard events of a desktop website are simulated in mobile website using jquery?

I have some function tied to keyboard arrow keys events in desktop website using JQuery. 我有一些使用JQuery绑定到桌面网站中的键盘箭头键事件的功能。

$(document).keydown(function(e) {
switch (e.which) {
    case 37: //left
         doIT("leftside");
         Break;
    case 38: //up
         doIT("upside");
         Break;
    case 39: //right
         doIT("rightside");
         Break;
    case 40: //down
         doIT("downside");
         Break;
}

} }

These are working fine on desktop website, but not on mobile website. 这些在台式机网站上运行良好,但在移动网站上却无法正常运行。
(I know we can't access arrow keys in mobile) (我知道我们无法访问手机中的箭头键)

I want those corresponding functions to be executed in mobile website. 我希望那些相应的功能可以在移动网站中执行。
(if user swipes to left/up/right/down on mobile website) (如果用户在移动网站上向左/向上/向右/向下滑动)

can anyone help me in this? 有人可以帮我吗?

I don't think you can do this, especially with swipes as swipes are a combination of different events (ie keydown, keyhold, keyup) . 我认为您无法做到这一点,尤其是对于滑动操作,因为滑动操作是不同事件(例如keydown,keyhold,keyup)的组合。

Picking up swipes via keyup and keydown events can be very difficult. 通过按键和按键事件来拾取刷卡可能非常困难。 It would be better to use a plugin which can explicitly detect swipes such as Hammer.js 最好使用可以明确检测刷卡的插件,例如Hammer.js。

jquery mobile has events for swipeleft and swiperight jQuery mobile具有swipeleftswiperight事件

For up and down yo have to use the scrollstart and scrollstop events to detect in which direction the scroll is going. 对于向上和向下,您必须使用scrollstart和scrollstop事件来检测滚动方向。

 var start = 0; $(document).on("scrollstart", function() { console.log($(window).scrollTop()); start = $(window).scrollTop(); }); $(document).on("scrollstop", function() { console.log($(window).scrollTop()); var end = $(window).scrollTop(); if (start - end < 0) { console.log("Scrolled down"); doIT("downside"); } else { console.log("Scrolled up"); doIT("upside"); } }); $(document).on("swipeleft", function() { doIT("rightside"); console.log("right"); }); $(document).on("swipeleft", function() { doIT("leftside"); console.log("right"); }); 

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

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