Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:26:32+00:00 2026-06-13T09:26:32+00:00

I generate dynamically a html page and I’d like to be able to navigate

  • 0

I generate dynamically a html page and I’d like to be able to navigate through it up and down with the arrow keys, scrolling to the next anchor each time I press UP or DOWN.

Here’s my code (doesn’t work)

$(document).keydown(function(e){
    if (e.keyCode == 40) {
        console.log('keydown');
        if ( next === undefined ) {
            next = $('.js_anchor').next();
        } else {
            next = next.next();
        } 
        $(document).animate({scrollTop: next}, 2000,'easeInOutCubic');
    }
});

Any ideas ? Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:26:34+00:00Added an answer on June 13, 2026 at 9:26 am

    Use a variable to store the anchor list and then the key index to the current one like so:

    $myAnchors = $('.js_anchor');
    currentAnchor = -1;
    
    $(document).keydown(function(e){
        if (e.keyCode == 40) {
            console.log('keydown');
            if ($myAnchors.length < currentAnchor+1) {
                currentAnchor++;
                $(window).animate({scrollTop: $myAnchors.eq(currentAnchor).offset().top}, 2000,'easeInOutCubic');
            }
        }
    });
    

    This have some bad effects if the user scrolls by himself and presses the down arrow it might scroll up… Use this function to determine which to scroll to:

    function getAnchorOffset(prevnext){
        //prevnext = 'next' or 'prev' to decide which we want.
        currentPosition = $(window).scrollTop();
        for(k in $myAnchors){
            if($myAnchors[k].offset().top<currentPosition && $myAnchors[k].offset().top>closestOffset){
                 closestOffset = $myAnchors[k].offset().top;
                 key = k;
            }else if($myAnchors[k].offset().top>currentPosition){
                break;
            }
    
        }
        if(prevnext=='next'){
            return $myAnchors[key+1].offset().top;
        }else{
            return closestOffset; 
        }
    }
    

    And replace

    $(window).animate({scrollTop: $myAnchors.eq(currentAnchor).offset().top}, 2000,'easeInOutCubic');
    

    by

    $(window).animate({scrollTop: getAnchorOffset('next')}, 2000,'easeInOutCubic');
    

    Please take note that it has NOT been tested but should be close to working, if not already working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code should not generate any HTML button dynamically. Coz there is no div
I have a page which generates HTML dynamically and does an echo of it.
I'm using the dompdf HTML to PDF converter to dynamically generate PDF files based
I have a page with a ScriptManager, a generic HTML drop-down list ( <select>
Would it be possible using only JavaScript and HTML to dynamically generate a favicon,
I am using Struts1.3, and in this Jsp page dynamically generate check boxes depends
I need to generate HTML(cshtml) page when I call asp.net controller by Ajax, and
I have a dynamically generated HTML portion on a page with numerous repaeted elements
html code meta tag description, it is generated dynamically and we have quotes in
How do I serve a dynamically generated image in Django? I have an html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.