Using arrow keys with jQuery scrollTo

前端 未结 3 818
忘掉有多难
忘掉有多难 2020-12-11 09:57

I have successfully implemented the scrollTo jQuery plugin which scrolls to the next div with the class \"new\" when a link is clicked. However, I would also like to be able

3条回答
  •  生来不讨喜
    2020-12-11 10:35

    Just for giving more idea, working with arrays.

    var panel_arr = new Array();
    $(document).ready(function(e) {
    
        $('.parallax-panel-wrapper').each(function(i, element){ 
            panel_arr.push( $(this).attr("id") );
        });
    
        var current_parallax_panel_no   = 0;
        $(document).keydown(function (evt) {
            if (evt.keyCode == 40) { // down arrow
                evt.preventDefault(); // prevents the usual scrolling behaviour
                if(current_parallax_panel_no < (panel_arr.length-1)) current_parallax_panel_no++;
                scrollByArrowKeys(1);               
            } else if (evt.keyCode == 38) { // up arrow
                evt.preventDefault(); // prevents the usual scrolling behaviour
                if(current_parallax_panel_no >= 1) current_parallax_panel_no--;
                scrollByArrowKeys(0); 
            }
        });
    
        function scrollByArrowKeys(add_more){
            scrollToThis = (($("#" + panel_arr[current_parallax_panel_no]).offset().top)  + add_more ; // get element top
            $.scrollTo(scrollToThis, 800);      
        }
    
    });
    

提交回复
热议问题