html - Jquery scrolling on multiple hyperlink -


i'm working on singlepage website, choose keep header , footer fixed , got trouble while using hyperlink hashtag. got 4 links 4 parts of page,

i want display content in middle, avoid datas hidden both header , footer.

i have found code:

jquery(document).ready(function($) {      $(".scroll").click(function(event){              event.preventdefault();         $('html,body').animate({scrolltop:$(this.hash).offset().top}, 500);     }); }); 

i can't make work own site.

i put id

<a name="timeline" class="anchor_title"><center><h1>our progress</h1></center></a>  <a name="map" class="anchor_title"><center><h1>our gathering data</h1></center></a> 

does explain me how example catch name of each hyperlink , place on jquery code.

the script found have make of href attribute :

<a href="#timeline" class="anchor_title"><center><h1>our progress</h1></center></a>  <a href="#map" class="anchor_title"><center><h1>our gathering data</h1></center></a> 

further in html these links refer elements same id :

<div id="timeline"></div> <div id="map"></div> 

in script, class have match selector of links :

$(document).ready(function() {  $('.anchor_title').click(function(event) {      event.preventdefault(); var offset = $(this.hash).offset().top-$('#header').height(); $('html, body').stop().animate({scrolltop: offset}, 500); }); }); 

note how height of (fixed) header subtracted position should scroll to, otherwise end beneath (at top of screen) instead of right below.

edit - here's basic example :

http://codepen.io/anon/pen/bnzznq

there few things noted :

for preventing animation buildup, stop() method has been added function.

the first element on page needs top padding (or alternatively :before pseudo element) in same amount height of header placed correctly respect fixed position.

special html5 elements header , section being used in markup of example. document needs correct declaration : <!doctype html>. other type of declaration elements have converted divs class defined :

<section id="first"></section>  section {height: 70vh} 

would become (along change of css) :

<div id="first" class="section"></div>  .section {height: 70vh} 

hope solves let me know if needs other tweaks.

new edit - html5 elements make bit of difference selector in script well!

$('#header').height()  $('header').height() 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -