jQuery scrollTo code assistance -
can please explain why won't work? ideally, when link @ top clicked, should scroll down other class. don't understand lot of code, detailed explanation great!
$('a.scrollto').click(function(){ $('html, body').animate({ scrolltop: $( $.attr(this, 'href') ).offset().top-25 }, 500); return false; });
you have make sure each anchor class "scrollto" has href points class (or id more suggested). example:
<a href="#gotothisanchor" class="scrollto">scroll #gotothisanchor</a>
clicking above anchor scroll down element has id="gotothisanchor
//put's click event listeners on anchor elements class name 'scrollto' $('a.scrollto').click(function(){ $('html, body').animate({ //animates html , body (this makes scroll effect) scrolltop: $( $.attr(this, 'href') ).offset().top-25 //choose element scroll in case choose element specified in "href" of anchor clicked. offset().top gives position offset top of .your_class_name }, 500); //500 time in milliseconds take scroll. return false; //cancels anchor following it's href });
i suggest using id instead of class name scroll to, or else scrolltop scroll first element "class name" sees.
you want have anchor click on scroll down #rulesa:
<a href="#rulesa" class="scrollto">scroll #rulesa</a>
clicking anchor scroll element has id="rulesa". notice id contained in href.
my apologies, using following code work:
$('a.scrollto').click(function(){ $('html, body').animate({ scrolltop: $("#rulespng").offset().top-25 }, 500); return false;
but have recreate function each id anchor want scroll to. fine, if want a.scrollto
scroll 1 anchor point: #rulespng
but if use original code more flexible:
scrolltop: $( $.attr(this, 'href') ).offset().top-25
you need 1 function listed @ top (which wrote in original question), , work anchors $('a.scrollto')
, depending on href attribute of anchor explained above.
Comments
Post a Comment