Page 1 of 1

Smooth scrolling when a user clicks an anchor

Posted: Fri Oct 25, 2013 6:57 am
by killfrog47
I am trying to make my personal website a little slicker looking. I know the direction I want to go with it but I am having trouble getting it to work. I want a one page website but I dont really know how to get it to scroll smooth. I tried some random scripts I found on stackoverflow but they didnt work. I googled the problem but still nothin =/ Does anyone have any pointers? What should I search for?

Re: Smooth scrolling when a user clicks an anchor

Posted: Fri Oct 25, 2013 9:19 am
by Temor
I think I know what you mean. I may be wrong though

http://flesler.com/jquery/scrollTo/

ScrollTo is what I'm using. It's really easy to use :)

Re: Smooth scrolling when a user clicks an anchor

Posted: Fri Oct 25, 2013 9:27 am
by killfrog47
Yeah! I saw that but IDK why I didnt look more into it. How do I use it? Would it be
$('#homeNavBtn').scrollTo('#home');
along those lines?

Re: Smooth scrolling when a user clicks an anchor

Posted: Sat Nov 02, 2013 4:30 am
by Helx
<a href="#about" onclick="$('html, body').animate({ scrollTop: $('#about').offset().top }, 400);event.preventDefault();">About</a>
Clicking will scroll to:
<div id="about">asdfdsf sd fdhujgsdkxjhcgf</div>
Using jQuery. Always have the link use href to an id (how you should be doing it anyway), so there's something to fall back on (eg. user disabled JavaScript). I never got this to work without the onclick event, I guess it may just be caching.

That 'preventDefault' thing will stop the page from flashing.

Hopefully that helps?

Re: Smooth scrolling when a user clicks an anchor

Posted: Sat Nov 02, 2013 9:28 pm
by Temor
I completely forgot about this thread, sorry about that.

Re: Smooth scrolling when a user clicks an anchor

Posted: Tue Nov 05, 2013 1:20 am
by killfrog47
Helx wrote:
<a href="#about" onclick="$('html, body').animate({ scrollTop: $('#about').offset().top }, 400);event.preventDefault();">About</a>
Clicking will scroll to:
<div id="about">asdfdsf sd fdhujgsdkxjhcgf</div>
Using jQuery. Always have the link use href to an id (how you should be doing it anyway), so there's something to fall back on (eg. user disabled JavaScript). I never got this to work without the onclick event, I guess it may just be caching.

That 'preventDefault' thing will stop the page from flashing.

Hopefully that helps?
This does help! Thanks Here is what I did:
<script>
$(document).ready(function(){
  $('#nav').localScroll({target:'body'})
});
$(document).ready(function(){
  $('.topBtn').localScroll({target:'body'})
});
</script>
Then for my HTML I have this:
<nav class="links">
  <ul id="nav">
    <li><a href="#home" class="navBtn">Home</a></li>
        <li><a href="#about" class="navBtn">About Me</a></li>
        <li><a href="#resume" class="navBtn">Resume</a></li>
        <li><a href="#portfolio" class="navBtn">Portfolio</a></li>
    <li><a href="#contact" class="navBtn">Contact</a></li>
  </ul>
</nav>
Im using github so I can work on my laptop and desktop and not have to worry about flash drives and stuff. This is my full page (and its working =D) https://github.com/killfrog47/tomascord ... index.html

I couldnt get it to work I didnt even think about doing onclick... lol
Temor wrote:I completely forgot about this thread, sorry about that.
No worries haha forced me to learn =)

Re: Smooth scrolling when a user clicks an anchor

Posted: Tue Nov 05, 2013 1:27 am
by ScTech
Why the two document ready calls?

Re: Smooth scrolling when a user clicks an anchor

Posted: Tue Nov 05, 2013 7:32 pm
by killfrog47
ScTech wrote:Why the two document ready calls?
I did it because I was getting trouble with it and I dont know too much about jquery or javascript lol. I figured it worked and I dont want to go messing it up so I left it