d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Decide Where My Href Will Scroll To With Js
Add Reply New Topic New Poll
Member
Posts: 12,577
Joined: Oct 16 2008
Gold: 3,007.50
Jun 19 2014 01:28pm
I have a long 1 page. And i have in my nav when you click, it scrolls you down to a certain part of the page.

Now when it scrolls down with my current JS, it brings the thing i want to see too far down and shows half of whats on top of it.

Here is some of my code:

Code
<script>
$(document).ready(function(){
$('a[href^="#media"],a[href^="#microprocessor"],a[href^="#networks"],a[href^="#web"],a[href^="#rotary"]').on('click',function (e) {
e.preventDefault();

var target = this.hash,
$target = $(target);

$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 700, 'swing', function () {
window.location.hash = target;
});
});
});
</script>


So basically i just added a <a id="media"></a> at the location I want that button to take me to.

My issue is that it scrolls too far down... I tried changing where I put the tag but I does not help.

Anything i could add to my JS that it will make the scroll go more or less given the location of the tag?
Member
Posts: 12,577
Joined: Oct 16 2008
Gold: 3,007.50
Jun 19 2014 02:02pm
nvm added some extra code after the 'scrolltop' and it worked now ^.^

( $(window).height() - $(this).outerHeight(true) ) / 3
Member
Posts: 9,412
Joined: Nov 18 2009
Gold: 20.00
Aug 17 2014 12:30am
I always just use padding on the top of my sections lol
Member
Posts: 24,488
Joined: Jul 11 2011
Gold: 1,272.50
Aug 17 2014 09:30am
repent for those class selectors :lol:

This post was edited by HighschoolTurd on Aug 17 2014 09:30am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll