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 8516091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:19:30+00:00 2026-06-11T05:19:30+00:00

I want to scroll to a div each time user presses j key. Here

  • 0

I want to scroll to a div each time user presses j key. Here is the code for it.

$(function() {
    function scroll(direction) {

        var scroll, i,
                positions = [],
                here = $(window).scrollTop(),
                collection = $('.message_box');

        collection.each(function() {
            positions.push(parseInt($(this).offset()['top'],0));
        });

        for(i = 0; i < positions.length; i++) {
            if (direction == 'next' && positions[i] > here) { scroll = collection.get(i); break; }
            if (direction == 'prev' && i > 0 && positions[i] >= here) { scroll = collection.get(i); break; }
        }

        if (scroll) {
        $('html, body').animate({"scrollTop": $(scroll).offset().top-50});
            $(scroll).css('color', 'blue');
            $(scroll).mouseleave(function() {
            $(this).css('color', 'black');
            });
        }

        return false;
    }

    $("#next,#prev").click(function() {        
        return scroll($(this).attr('id'));        
    });
$('body').keyup(function(event) {
  if (event.which == 74) {
     return scroll('next');
   }
});
$('body').keyup(function(event) {
  if (event.which == 75) {
     return scroll('prev');
   }
});

});

I need to subtract 50 from the offest of the div to scroll to which is this.

$('html, body').animate({"scrollTop": $(scroll).offset().top-50});

It will scroll the first time but not the rest of the times. I always get the integer 218 which is the offset of the first div to scroll to.
DEMO – http://jsfiddle.net/XP5sP/6/
Can someone help me ?

  • 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-11T05:19:32+00:00Added an answer on June 11, 2026 at 5:19 am

    The problem is that you’re always moving the scrollTop value to 50 pixels before the first matched element, so it’s always identifying that element as the one you need to scroll to in your if statement because its position is greater than the current scrollTop value.

    Modify the relevant section of your code to this:

    if (direction == 'next' && positions[i] > here + 50) {
        scroll = collection.get(i);
        break;
    }
    

    That way it accounts for the window being scrolled to 50 pixels above the current element.

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

Sidebar

Related Questions

I want the scrollLeft function to scroll a div each time when I click
I want to detect a div (dynamic content div) end, when user scroll down
I want to horizontally scroll a div by clicking on the button Left and
I want to scroll to a div + 100px in the y axis. How
http://subzerostudio.com/temp/verticalscroller/temp.html I want this scroll to effect the div#container instead of the body,html but
On hover I want my div to scroll down. I know i can use
I have scroll on a div. And I want to change my cursor sign,
What I want: <div style=overflow:scroll;width:100%;height:50%;> &nbsp; <div style=height:500px;width:400px;border:solid 3px red;> </div> </div> <div style=overflow:scroll;width:100%;HEIGHT:50%;>
I want to detect the presence of a scroll bar in a DIV using
I have a GridView in a div with Vertical scroll, but I want to

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.