简体   繁体   English

如何触发任何箭头键

[英]How to trigger any of the arrow keys

I have an application (HTML5, JavaScript, Jquery, jquery Mobile) with a slider. 我有一个带滑块的应用程序(HTML5,JavaScript,Jquery,jquery Mobile)。 The handle can be moved via touch and you can navigate through the years 1861 to 2000. There are symbols on my map which are visible/non visible depending on the year. 手柄可以通过触摸移动,您可以浏览1861年到2000年。我的地图上有符号,根据年份可见/不可见。 See my example image. 看我的示例图片。

I also want the handle to move, when the user clicks a specific button ("forward"). 当用户单击特定按钮(“前进”)时,我还希望手柄移动。 the handle should go through each year until the user clicks a different button. 句柄应该每年进行,直到用户点击不同的按钮。 I managed that with one click the handle goes + 1 year, and the handle, the year and the map update the changed year. 我只需点击一下即可处理该句柄+ 1年,并且句柄,年份和地图会更新已更改的年份。

  function moveLeft(){ var slider1 = $("#slider").val(); var $slider = $("#slider"); slider1++; $slider.val(slider1).slider("refresh"); var wert1 = slider1; var start = new Date().getTime(); //start = hideLayer2(wert1, start); $('#jahr').text(wert1); var $ausgabe = $("#ausgabe"); $ausgabe.text(wert1); gewaehltesJahr = wert1; 

I built a for loop (for (i =slider1; i< 2000; i++)) for this function but the handle and everything will only update, if the function reached the year 2000. I want to see the update of every single year. 我为这个函数构建了一个for循环(for(i = slider1; i <2000; i ++))但是句柄和一切只会更新,如果函数到达2000年。我想看到每一年的更新。 Even if I go through the code with a debugger, it will only update year, handle and map when it finished the loop und exited the function. 即使我使用调试器查看代码,它只会在完成循环并退出函数时更新年份,处理和映射。

Following code for example: If i start in 1861 und initialise the function the handle and map will jump directly into 1870 after the alert "alert("vor") in the last line. 下面的代码例如:如果我在1861年开始并初始化该函数,则句柄和地图将在最后一行的警报“alert(”vor“)之后直接跳转到1870。

 function vor(){ var slider1 = $("#slider").val(); var $slider = $("#slider"); for( var s = slider1; s < 1870; s++){ //$slider.val(s).slider("refresh"); $("#slider").slider('value',s); alert(s); var wert1 = s; var start = new Date().getTime(); //start = aktuelles Datum hideLayer2(wert1, start); $('#jahr').text(wert1); var $ausgabe = $("#ausgabe"); $ausgabe.text(wert1); gewaehltesJahr = wert1; } alert("vor"); } 

The handle, when in focus can also be moved by pressing the arrowkeys. 按下箭头键时,也可以移动手柄。

 $('.ui-slider-track .ui-btn.ui-slider-handle').focus(); 

I tried du imitate the pressing of one key to go a year forward but it isn't working either.I tried to set a trigger but it didn't work. 我试着模仿按下一把钥匙向前走一年,但它也没有用。我试图设置一个触发器,但它没有用。 Can anybody help? 有人可以帮忙吗?

 var kEvent = document.createEvent('Event'); kEvent.initKeyEvent("keypress", true, true, null, false, false, false, false, 38, 0); document.activeElement.dispatchEvent(kEvent); 

For loop gets executed instantly, so you have to use javascript timers! for循环立即执行,所以你必须使用javascript计时器!

//Global flag to decide animation
//Make this false to clear the animation loop from anywhere
var animate_slider = false;

//Global timer for animation
var animation_timer;

//Call animation function
function vor(){ 
    animate_slider = true;
    animation_timer = setInterval(function(){
        if(!animate_slider){
            clearInterval(animation_timer);
        }
        else{
            var slider1 = $("#slider").val();
            var $slider = $("#slider");
            $("#slider").slider('value',s);
            var wert1 = s;
            var start = new Date().getTime(); //start = aktuelles Datum
            hideLayer2(wert1, start); 
            $('#jahr').text(wert1); 
            var $ausgabe = $("#ausgabe");
            $ausgabe.text(wert1); 
            gewaehltesJahr = wert1;
            if(slider1 == maxValue){
                clearInterval(animation_timer);
            }
        }
    },1000);
}

Note: 注意:

Triggering arrow keys is not necessary & pointless, cause it'll also result in instance execution of all clicks. 触发箭头键不是必需且毫无意义的,因为它还会导致实例执行所有点击。 So even there you have to use timers, thus you can bypass that & change you main method with timers. 因此,即使在那里你也必须使用计时器,因此你可以绕过它并用计时器改变你的主要方法。

Thanks so much, I edited it a little bit and now it works! 非常感谢,我编辑了一点,现在它的工作原理!

   var animation_timer;
function vor(){ 
    animate_slider = true;
    animation_timer = setInterval(function(){
        if(!animate_slider){
            clearInterval(animation_timer);
        }
        else{
            var slider1 = $("#slider").val();
            var $slider = $("#slider");
           // $("#slider").slider('value',slider1);
             slider1++;
            var wert1 = slider1;
            $slider.val(slider1).slider("refresh");
            var start = new Date().getTime(); //start = aktuelles Datum
            hideLayer2(wert1, start); 
            $('#jahr').text(wert1); 
            var $ausgabe = $("#ausgabe");
            $ausgabe.text(wert1); 
            gewaehltesJahr = wert1;
            if(slider1 == 2000){
                clearInterval(animation_timer);
            }
        }
    },1000);
}

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

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