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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:43:47+00:00 2026-05-21T08:43:47+00:00

I am using jquery to scroll to sections of my page. Here’s the code:

  • 0

I am using jquery to scroll to sections of my page.

Here’s the code:

// Scroll to element
$('#menu li a').click(function(){        
    var elementId = $(this).attr('href');   
    $('html, body').animate({ scrollTop: $(elementId).offset().top }, 1600);
    return false;
    });

<ul id="menu">
  <li><a href="#item1">1</a></li>
  <li><a href="#item2">2</a></li>
</ul>

<div id="item-1">placeholder-1</div>
<div id="item-2">placeholder-2</div>

As you can see I’m grabbing the href (#id) of an target element from the menu link and scrolling to the target element.

But the issue lies in the fact that I have target elements going right to the bottom of the page, and when the page is scrolled to the last, the top of that element can’t scroll to the top of the page as there is not enough page left at bottom… Am I making sense?

I’m trying to work out a solution using css or jquery that would mean that regardless of the height of the page (or padding/margin at bottom) the element will be at the top of the viewport when a menu item is clicked.

Do I add more padding based on the height of the viewport using javascript? Do I use some kind of overflow trick?

  • 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-05-21T08:43:47+00:00Added an answer on May 21, 2026 at 8:43 am

    Your problem is that the page isn’t as tall as you want it to be. If you try to go to #x and #x is at the bottom of <body>, then you’ll scroll to the bottom of the page and #x will be at the bottom of the window when you want it at the top.

    I think your only choice is to force the <body> to be taller. Something like this:

    $(document).ready(function() {
        viewport_height = $(window).height();
        $height_hack    = $('<div>&nbsp;</div>').height(viewport_height);
        $('body').append($height_hack);
    });
    

    Something like that will append a (visually) empty <div> to the bottom of your content, this extra <div> will have the same height as the browser’s viewport and this extra height will let you always scroll your elements right to the top of the viewport.

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

Sidebar

Related Questions

I'm using the jQuery .scroll() function to make a certain element fade to 0.2
I am using jQuery's scrollTo plugin to scroll up and down my page, using
I have the following simple jQuery: $('#features').hide(); $('#more').click(function(e) { e.preventDefault(); $('#more').hide(); $('#features').show(); }); This
Using jQuery, how do you bind a click event to a table cell (below,
Using jquery how do I focus the first element (edit field, text area, dropdown
Using jQuery, what's the best way to find the next form element on the
I am using jquery to scroll to an anchor.... It works fine with a
I'm using the Jquery Scroll Follow plugin to allow a box to follow along
I would like to implement the following feature using jQuery scrollTo function or some
I am using Jquery's Scrollto plugin to scroll up and down based on a

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.