Smooth scrolling when a user clicks an anchor

JavaScript related questions should go here.
Post Reply
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Smooth scrolling when a user clicks an anchor

Post 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?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Smooth scrolling when a user clicks an anchor

Post 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 :)
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Smooth scrolling when a user clicks an anchor

Post by killfrog47 »

Yeah! I saw that but IDK why I didnt look more into it. How do I use it? Would it be [syntax=javascript]$('#homeNavBtn').scrollTo('#home');[/syntax] along those lines?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Smooth scrolling when a user clicks an anchor

Post by Helx »

[syntax=javascript]<a href="#about" onclick="$('html, body').animate({ scrollTop: $('#about').offset().top }, 400);event.preventDefault();">About</a>[/syntax]

Clicking will scroll to:

[syntax=xhtml]<div id="about">asdfdsf sd fdhujgsdkxjhcgf</div>[/syntax]

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?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Smooth scrolling when a user clicks an anchor

Post by Temor »

I completely forgot about this thread, sorry about that.
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Smooth scrolling when a user clicks an anchor

Post by killfrog47 »

Helx wrote:[syntax=javascript]<a href="#about" onclick="$('html, body').animate({ scrollTop: $('#about').offset().top }, 400);event.preventDefault();">About</a>[/syntax]

Clicking will scroll to:

[syntax=xhtml]<div id="about">asdfdsf sd fdhujgsdkxjhcgf</div>[/syntax]

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:
[syntax=javascript]<script>
$(document).ready(function(){
$('#nav').localScroll({target:'body'})
});
$(document).ready(function(){
$('.topBtn').localScroll({target:'body'})
});
</script>[/syntax]

Then for my HTML I have this:
[syntax=xhtml]
<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>
[/syntax]

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 =)
ScTech
Posts: 92
Joined: Sat Aug 24, 2013 8:40 pm

Re: Smooth scrolling when a user clicks an anchor

Post by ScTech »

Why the two document ready calls?
<?php while(!$succeed = try()); ?>
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Smooth scrolling when a user clicks an anchor

Post 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
Post Reply