Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Scroll element to the middle of the screen instead of to the top with an anchor link

I'm building a one-page site with a fixed-positioned navigation bar which scrolls smoothly to the different section elements through anchor links. The default behaviour for scrolling to an element is to align it to the top of the browser window. Instead, I want to align the element to the middle of the screen.

I use this markup for navigation:

<nav class="main-nav">   <a href="#top">Top</a>   <a href="#section-1">Section 1</a>   <a href="#section-2">Section 2</a>   <a href="#section-3">Section 3</a>   <a href="#section-4">Section 4</a>   <a href="#section-5">Section 5</a> </nav> 

I use kswedberg's jQuery Smooth Scroll plugin to smooth the scrolling. I initiate it like this:

$('.main-nav a').smoothScroll({   offset: 0,   speed: 700 }); 

I want to set the offset to be ((window).height / 2) - (element height / 2) so that it's vertically centered, but I need help to figure out how to execute it properly.

I need it to:

  • Get the height of the window and divide it by two
  • Get the height of the element and divide it by two
  • Subtract the former from the latter
  • If possible, align it to the top as per default if the element is higher than the window

Since there are many anchor links I assume I either need to check the height of the element the anchor link that was clicked links to, or initiate smoothScroll for every anchor link.

Does anybody know how to do this?

like image 645
Nils Kaspersson Avatar asked Aug 09 '13 15:08

Nils Kaspersson


People also ask

How do I make my page scroll to the top in jQuery?

In jQuery, the scrollTo() method is used to set or return the vertical scrollbar position for a selected element. This behavior can be used to scroll to the top of the page by applying this method on the window property. Setting the position parameter to 0 scrolls the page to the top.

How can get vertical scroll position in jQuery?

jQuery scrollTop() Method The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.

How do I smooth scroll to Element?

To scroll to an element, just set the y-position to element. offsetTop . The SmoothScroll.


2 Answers

Here's how to do it with plain JQuery using scrollTo()

 $('.main-nav a').on('click', function(e) {    var el = $( e.target.getAttribute('href') );   var elOffset = el.offset().top;   var elHeight = el.height();   var windowHeight = $(window).height();   var offset;    if (elHeight < windowHeight) {     offset = elOffset - ((windowHeight / 2) - (elHeight / 2));   }   else {     offset = elOffset;   }   var speed = 700;   $('html, body').animate({scrollTop:offset}, speed); }); 

This is a combination of straker's code and code from this question: jQuery scrollTo - Center Div in Window Vertically

like image 88
Santiago Angel Avatar answered Sep 21 '22 22:09

Santiago Angel


The API provides a way to execute a smoothScroll not bound to an element. You'll want to execute this method inside an onclick event for the anchor tags so that you can have access to it's target. Then you can calculate what you need to to get to the desired position. Since offset is now an absolute offset instead of a relative offset, you'll need to get the exact position to scroll to.

$('.main-nav a').on('click', function(e) {    var el = $( e.target.getAttribute('href') );   var elOffset = el.offset().top;   var elHeight = el.height();   var windowHeight = $(window).height();   var offset;    if (elHeight < windowHeight) {     offset = elOffset - ((windowHeight / 2) - (elHeight / 2));   }   else {     offset = elOffset;   }    $.smoothScroll({ speed: 700 }, offset);   return false; }); 
like image 22
Steven Lambert Avatar answered Sep 24 '22 22:09

Steven Lambert