admin管理员组文章数量:1221067
I am trying to implement an infinite scroll in JS, I am using $(window).scroll() to detect the scroll position, every thing is working fine, I just have one issue I want the url to change if the user clicks the 'Home' key and then change again if the user press the 'End' key.
So when Home is clicked the scroll position will be move to the top and when End is clicked the scroll position will be moved to the bottom.
My Question is, how do you detect these behaviors (pressing Home/End)?
Youssef
I am trying to implement an infinite scroll in JS, I am using $(window).scroll() to detect the scroll position, every thing is working fine, I just have one issue I want the url to change if the user clicks the 'Home' key and then change again if the user press the 'End' key.
So when Home is clicked the scroll position will be move to the top and when End is clicked the scroll position will be moved to the bottom.
My Question is, how do you detect these behaviors (pressing Home/End)?
Youssef
Share Improve this question asked Aug 13, 2014 at 10:17 YoussefYoussef 3001 gold badge4 silver badges14 bronze badges 1- Nice. I came here at the same point in my code/testing for my infinite scroll implementation. Is the keypress event the solution that you stuck with? – Brian Layman Commented Sep 12, 2016 at 3:17
4 Answers
Reset to default 9KeyCode is 35 for END 36 for HOME!
function myKeyPress(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}else
if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
alert(String.fromCharCode(keynum));
}
Home, PageUp, PageDown, End and some other keys are only triggered by onkeydown, not onkeypress see this post
In an onkeydown
callback, check for e.key === 'Home'
and e.key === 'End'
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
its simple to detect please look at following code :
<input type="text" id="txtKey" onkeyup="keyPressEvent(event)" />
function keyPressEvent(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 35) {
alert('end key press');
}
if (code == 36) {
alert('home key press');
}
return false;
}
本文标签: javascriptDetecting if HomeEnd Keys is pressed in JSStack Overflow
版权声明:本文标题:javascript - Detecting if HomeEnd Keys is pressed in JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739360906a2159812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论