简体   繁体   English

带滚动条的错误箭头键导航

[英]Bugged arrow keys navigation with scrollTop

First of all I'm new to JavaScript/jQuery and English isn't my mother tongue. 首先,我是JavaScript / jQuery的新手,而英语不是我的母语。

In this case I can't use anything more than jQuery/JavaScript (I mean no other plugin/library, anything that I haven't coded myself). 在这种情况下,我只能使用jQuery / JavaScript(我的意思是没有其他插件/库,任何我自己没有编码的东西)。

I wanna make a website with arrow keys navigation, using overflow:hidden on my body, and scrollTop. 我想创建一个带有箭头键导航的网站,并使用overflow:hidden隐藏在我的身体上和scrollTop。

Here is what I already have : 这是我已经拥有的:

<div id="slide1">
    <div id="states">
        <div id="selector"></div>
        <div id="state1"><p>INTRO</p></div>
        <div id="state2"><p>STAGE 1</p></div>
        <div id="state3"><p>STAGE 2</p></div>
        <div id="state4"><p>STAGE 3</p></div>
    </div>
</div>

<div id="slide2">
</div>

<div id="slide3">
</div>

<div id="slide4">
</div>

As i named it a bit randomly, i'll have to explain : - slide1 is my first "state" or "screen". 正如我随机命名的那样,我将不得不解释:-slide1是我的第一个“状态”或“屏幕”。 It's a big container where I put informations about how to navigate through the website, as it's the first thing the user will see, we directly land on that state. 这是一个很大的容器,我在其中放置了有关如何浏览网站的信息,因为这是用户首先看到的内容,因此我们直接进入该状态。 - slide 2, 3, 4 : other big blocks will see if they navigate through the website, step by step. -幻灯片2、3、4:其他大块广告将逐步浏览它们是否浏览网站。 - state 1, 2, 3, 4 are buttons at the top of the document (position:fixed). -状态1、2、3、4是文档顶部的按钮(位置:固定)。 By clicking on it, the user can jump to slide 1, 2, 3 or 4. - selector is only a design thing, if you're on slide x he puts himself on state x, that shows the user where he is in the page. 通过单击它,用户可以跳至幻灯片1、2、3或4。-选择器仅是设计内容,如果您在幻灯片x上,他会将自己置于状态x,这将向用户显示他在幻灯片中所处的位置。页。

Now the JQuery part : 现在,JQuery部分:

Variables I use : 我使用的变量:

var pressedKeys = {};
var state = [0,1200,2400,3600];
var slidestate = [0, 25, 50, 75];
var n = 0;

var state: contains in an array values I use for my scrollTop (for now each are 1200px height long.) var slidestate : contains in an array values my selector (the thing that shows the user where he is) margin (in %) var state:在数组中包含我用于scrollTop的值(现在每个值都长1200px高。)var slidestate:在数组值中包含我的选择器(向用户显示他所在的位置)边距(以%为单位)

Obtaining keyCode : 获取keyCode:

$(document).keyup(function(e) {
if (e.keyCode == 40 || e.keyCode == 39) {
    n++;
    if (n>3){n--;}
}
else if (e.keyCode == 37 || e.keyCode == 38){
    n--;
    if (n<0){n++;}
    }

So n = the state where I am. 所以n =我所在的州。 I don't want it to be < 0 or > 3 that's why I use "if n>3 n--" in that case, as I have only 4 states (and my array start with a 0 index, so 0 1 2 3 = the 4 states I need). 我不希望它是<0或> 3,这就是为什么在这种情况下使用“ if n> 3 n--”的原因,因为我只有4个状态(并且我的数组以0索引开头,所以0 1 2 3 =我需要的4个状态)。

Using var n to scroll (keyboard) : 使用var n滚动(键盘):

$("html:not(:animated),body:not(:animated),#basmontagne:not(:animated)").animate({

     scrollTop:state[n]
     },1000);

$(" #selector ").animate({left:slidestate[n]+'%'},1000);

});

I don't think I have to explain anything here, I use n as the index of my array containing values I need for every state. 我认为这里不需要解释任何内容,我将n用作包含每个状态所需值的数组的索引。 Animation duration is 1 second. 动画持续时间为1秒。

Using var n to scroll (.click event) : 使用var n滚动(.click事件):

$("#state1").click(function() {
        n=0;

        $("html:not(:animated),body:not(:animated),#basmontagne:not(:animated)").animate({
        scrollTop:state[n] },1000); 

    $(" #selector ").animate({left:slidestate[n]+'%'},1000);
});

Same as below but with my and an onClick event. 与下面相同,但带有my和onClick事件。

The problem 问题

The code below IS working. 下面的代码正常工作。 But there is some things I'm not able to fix. 但是有些事情我无法解决。 It all seems laggy, sometimes scrollTop seems to not be working; 一切似乎都有些迟钝,有时scrollTop似乎无法正常工作;

Case 1 : I'm at the 4th state, I press "down" arrow key once, then press "up" arrow key once. 情况1:我处于第4种状态,请按一次“向下”箭头键,然后按一次“向上”箭头键。 It should jump to state 3, but it doesn't. 它应该跳到状态3,但不是。 I have to press "up" once again, and then it jumps to state 2. what the heck? 我必须再次按“向上”,然后它跳到状态2。到底是什么?

Case 2 : I don't know why but sometimes, when I navigate through the site, the selector moves, but scrollTop doesn't do anything. 情况2:我不知道为什么,但是有时候,当我在站点中导航时,选择器会移动,但是scrollTop不会执行任何操作。 So my selector goes to state 3 where I'm only on state 1. Again what the heck? 因此,我的选择器进入状态3,而我仅处于状态1。这又是什么呢? It should be the same value of n for scrollTop and selector, I don't get why it's all bugged like this. 对于scrollTop和选择器,它应该与n的值相同,我不明白为什么它们都像这样臭虫。

Anyone got any idea about this ? 任何人对此有任何想法吗?

Ok, so I couldn't clearly understand what you are trying to do on the extreme cases, ie, n>3 and n<0. 好的,所以我不清楚您在极端情况下(n> 3和n <0)要做什么。 If your attempt was to just do nothing then you can reduce the redundancy in that bit by using, 如果您尝试不执行任何操作,则可以使用以下方法来减少冗余:

if ((e.keyCode == 40 || e.keyCode == 39) && (n < 3))
    n = n + 1;
else if ((e.keyCode == 37 || e.keyCode == 38) && (n > 0))
    n = n - 1;

instead of, 代替,

if (e.keyCode == 40 || e.keyCode == 39) {
    n++;
    if (n>3){n--;}
}
else if (e.keyCode == 37 || e.keyCode == 38){
    n--;
    if (n<0){n++;}
}

that makes it a little more debuggable, 这使得它更易于调试,

Coming to your main issues... Case 1 and Case 2 are because of the same problem, actually not a problem but something that was used incorrectly. 涉及到您的主要问题...情况1和情况2是由于相同的问题,实际上不是问题,而是使用不正确的东西。

The :animated filter, read more about animated filter :animated过滤器, 详细了解动画过滤器

So by writing, $("html:not(:animated),body:not(:animated),#basmontagne:not(:animated)") .animate({ scrollTop:state[n] },1000); 因此,通过编写$("html:not(:animated),body:not(:animated),#basmontagne:not(:animated)") .animate({ scrollTop:state[n] },1000);

you are selecting the html, body and #basmontagne elements which are not in animation . 您正在选择不在动画中的html,body和#basmontagne元素。

The issue of value of n and scrollTop not being synced at all times, precisely happens when the value of n changes in middle of an animation , because the html, body etc are in animation at that moment and are skipped by the jQuery selector, thus animation callback doesn't work at that instance. n的值和scrollTop的值始终不同步的问题恰好发生在n的值在动画中间变化 ,因为html,body等当时处于动画状态,并被jQuery选择器跳过,因此动画回调在该实例上不起作用。

In this fiddle here if you keep your console open you will see the value of n and scrollPosition are always on track. 这里,如果您保持控制台打开状态,将会看到n值和scrollPosition始终处于正常状态。

If in your code this does not work it may be because the var n is globally defined and some other section of code is modifying it. 如果在您的代码中不起作用,则可能是因为var n是全局定义的,并且代码的其他部分正在对其进行修改。

Lastly, you may also want to check the .stop() in jQuery which may make the animation better. 最后,您可能还想检查jQuery中的.stop() ,这可能会使动画更好。

Hope that helps.!! 希望有帮助!!

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

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