jQuery- Creating an offset from the top of page for internal anchor tags

jquery smooth scroll to anchor
jquery jump to anchor
jquery scroll to id from different page
jquery smooth scroll to div
scroll to particular div position when click on link
jquery jump to anchor onclick
javascript smooth scroll to element
window.scrollto smooth

I have an absolute positioned menu at the top of the page, let's say 50px in height. So, when someone visits a URL on my site like:

http://www.example.com/page.html#anchor

The page is scrolled 50px too high (covered by the menu).

I am trying to make it so that any time a link is visited with an internal anchor, the page will be offset by 50px, but this is not working:

if (document.location.href.indexOf('#') > -1) {

  var leavespace = 50;
  $('html,body').animate({scrollTop:leavespace}, 'slow');

}

I get the slow animated scrolling, but it is still 50px too high.

Your code works, make sure you have it in a ready function

$( document ).ready(function() {

  if (document.location.href.indexOf('#') > -1) {

  var leavespace = 50;
  $('html,body').animate({scrollTop:leavespace}, 'slow');
  }

});

Animated scroll to anchor/id function with jQuery Script, Using the name attribute of the anchor tag has become obsolete so we have to find a new way to scroll to a certain point on the page when a link is being. for this works similar to our scroll back to top widget and it should be part of body").​animate({ scrollTop: $($(this).attr("href")).offset().top }, 500); }); });. Here’s a neat little jQuery trick to fancy up your internal anchor links, by making them scroll smoothly to their target as opposed to jumping instantly. Internal anchor links are very common place, e.g. “back to top” links, or FAQ pages which list all the questions at the top.

You can use hashchange event for this purpose:

$(window).on("hashchange", function () {
    var leavespace = 50
    window.scrollTo(window.scrollX, window.scrollY + leavespace);
});

Working example

jQuery: Smooth Scrolling Internal Anchor Links, Here's a neat little jQuery trick to fancy up your internal anchor links, Making them visually scroll to their target not only looks a bit fancy, it also gives like the scrolled element not to bump the top of the page, i'd like to offset  When you use the URL anchor (the #fragment part), the browser window will scroll itself (instantly) to bring anchor at the top of the page, leaving the content behind the fixed header. So here we need for offsetting anchor hash tag links to adjust for fixed header to let content appear below the fixed item.

To make it without jquery, I implemented it like (based on Elena's response):

window.onhashchange = function (){
        var leavespace = 50
        window.scrollTo(window.scrollX, window.scrollY - leavespace);
    };

Smooth Scrolling, And before you reach for a library like jQuery to help, there is also a native Scroll to specific values // scrollTo is the same window.scroll({ top: 2500, left: For example, if you click a #hash link, the native behavior is for the Here's the code to perform a smooth page scroll to an anchor on the same page. Contrast this with .position(), which retrieves the current position relative to the offset parent. When positioning a new element on top of an existing one for global manipulation (in particular, for implementing drag-and-drop), .offset() is more useful..offset() returns an object containing the properties top and left.

Create a scrolling anchor link with jQuery, Very simply put, this will function like a normal internal anchor link or the top of the document var catTopPosition = jQuery('#cat').offset().top;  jQuery- Creating an offset from the top of page for internal anchor tags. is visited with an internal anchor, the page will be offset by 50px, but this is not

How To Use jQuery To Scroll To A Specific Element, Create internal links and jump to different parts of the page. Now create the jQuery Scroll features for the click event on the anchor tags. 'scrollTop' : $​targetEle.offset().top. } of content then you should provide a button (or image or link) at the bottom so that users can go to the top of the page by clicking it. The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels. When used to set the offset:

How to Add Smooth Scrolling for Anchor Links, This tutorial offers an example of how to add smooth scrolling for anchor links. For child themes, please contact the author for advice on adding smooth scroll element by its ID , for example #top , you need to tell jQuery to look for links with sites and allows both a back to top button and named anchors to smooth scroll. The “offset” variable is the offset of the element, and the “offsetTop” variable uses the “offset” variable to withdraw the “top” value. As a result, we get the distance of an element from the top of the page. The “totalScroll” variable is how much the browser should scroll.