javascript - jQuery scrollTo - Center Div in Window Vertically -


i have site uses fixed menu on top of page.

when link clicked, should scroll vertically center of target div aligns vertical center of window, offset height of header. - important div centered no matter resolution of monitor is

i'm using jquery , scrollto, can't figure out math needed this.

here's attempt:

function scrollto(target) { var offset; var scrollspeed = 600;  if (viewport()["width"] > 767 && !jquery.browser.mobile) {     // offset anchor location , offset navigation bar if navigation fixed     offset = $(target).offset().top - document.getelementbyid('navigation').clientheight; } else {     // offset anchor location since navigation bar static     offset = $(target).offset().top; }      $('html, body').animate({scrolltop:offset}, scrollspeed); } 

eventually figured out. being dumb math. offset fixed header , footer well, works resolutions.

    // scrollto: smooth scrolls target id function scrollto(target) {     var offset;     var scrollspeed = 600;         var wheight = $(window).height();         //var targetname = target;         //var windowheight = $(window).height();         //var pagecenterh = windowheight/2;         //var targetheight = document.getelementbyid(targetname).offsetheight;      if (viewport()["width"] > 767 && !jquery.browser.mobile) {         // offset anchor location , offset navigation bar if navigation fixed         //offset = $(target).offset().top - document.getelementbyid('navigation').clientheight;                 offset = $(target).offset().top - $(window).height() / 2 + document.getelementbyid('navigation').clientheight + document.getelementbyid('footer').clientheight;     } else {         // offset anchor location since navigation bar static         offset = $(target).offset().top;     }      $('html, body').animate({scrolltop:offset}, scrollspeed); } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -