简体   繁体   English

向上翻页,向下翻页,向上和向下键在我的页面上不起作用

[英]Page up, page down, up and down keys don't work on my page

I'm making a portfolio to show my projects. 我正在制作作品集以展示我的项目。 So when someone clicks on Project 1 in the menu, a new div (div#project1) will slide in from the left like a new page to show Project 1. 因此,当有人单击菜单中的Project 1时,新的div(div#project1)将像新页面一样从左侧滑入以显示Project 1。

Problem 问题

  1. Page up, page down, up and down arrows don't work on this div (div#project1) automatically. 向上,向下,向上和向下箭头在此div(div#project1)上不自动起作用。
  2. I need to click on the div (div#project1) first before I can scroll with the keyboard. 我需要先单击div(div#project1),然后才能使用键盘滚动。

How can I make it automatic? 我如何使其自动? Looking for javascript answers! 寻找javascript答案! Thank you 谢谢

 function project() { document.getElementById("project1").toggleClass('show'); document.getElementById('project1').addEventListener('click', function() { this.classList.toggle('focus'); // or whatever... this.focus(); }); } HTMLElement.prototype.addClass = function(string) { if (!(string instanceof Array)) { string = string.split(' '); } for (var i = 0, len = string.length; i < len; ++i) { if (string[i] && !new RegExp('(\\\\s+|^)' + string[i] + '(\\\\s+|$)').test(this.className)) { this.className = this.className.trim() + ' ' + string[i]; } } } HTMLElement.prototype.toggleClass = function(string) { if (string) { if (new RegExp('(\\\\s+|^)' + string + '(\\\\s+|$)').test(this.className)) { this.className = this.className.replace(new RegExp('(\\\\s+|^)' + string + '(\\\\s+|$)'), ' ').trim(); } else { this.className = this.className.trim() + ' ' + string; } } } HTMLElement.prototype.removeClass = function(string) { if (!(string instanceof Array)) { string = string.split(' '); } for (var i = 0, len = string.length; i < len; ++i) { this.className = this.className.replace(new RegExp('(\\\\s+|^)' + string[i] + '(\\\\s+|$)'), ' ').trim(); } } 
 .project { position: fixed; top: 0; width: 100vw; transition: all .5s; z-index: 100; left: 100vw; background: #fff; overflow-y: scroll; bottom: 0; } .show { left: 0vw!important; } 
 <div id="menu"> <h2 onclick="project()">Project 1</h2> </div> <div id="project1" class="project" onclick="project()"> <h1>Project 1</h1> <center> <img src="x.png"> </center> </div> 

You need to execute the focus method outside of the inner click listener callback. 您需要在内部click侦听器回调之外执行focus方法。 And when you hide the project details it is probably good to remove the focus from that: 当您隐藏项目详细信息时,最好从中删除焦点:

function project() {
    const proj = document.getElementById("project1");
    proj.classList.toggle('show');
    proj.addEventListener('click', function() {
        this.classList.toggle('focus');
        this.blur(); // Remove focus
    });
    proj.focus(); // Set the focus on the project details that just appeared.
}

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

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