I forgot where i got this idea of this script, but i remember this is not my own.
About the script:
This is to make some scroll effect inside a page anchors to target (like #content). Click Here For The DEMO
How to use:
Simply add this scroll.js script with jquery. Set the anchor tags which want to use the effect class=”scroll”
HTML file:
TEST <div align="center"> <table border="1" cellspacing="1" width="500" id="table1"> <tr> <td> <div align="left" id="top"> <table border="0" cellpadding="0" cellspacing="0" width="100" id="table2"> <tr> <td><a href="#home" class="scroll">HOME</a></td> </tr> <tr> <td><a href="#about" class="scroll">ABOUT</a></td> </tr> <tr> <td><a href="#contact" class="scroll">CONTACT</a></td> </tr> </table> </div></td> </tr> <tr> <td height="500" valign="top"><span id="home">HOME AREA</span> <a href="#top" class="scroll">Go Top</a></td> </tr> <tr> <td valign="top" height="700"><span id="about">ABOUT AREA</span> <a href="#top" class="scroll">Go Top</a></td> </tr> <tr> <td valign="top" height="800"><span id="contact">CONTACT AREA </span> <a href="#top" class="scroll">Go Top</a></td> </tr> </table> </div>
scroll.js script:
$(document).ready(function(){ $(".scroll").click(function(event){ //prevent the default action for the click event event.preventDefault(); //get the full url - like mysitecom/index.htm#home var full_url = this.href; //split the url by # and get the anchor target name - home in mysitecom/index.htm#home var parts = full_url.split("#"); var trgt = parts[1]; //get the top offset of the target anchor var target_offset = $("#"+trgt).offset(); var target_top = target_offset.top; //goto that anchor by setting the body scroll top to anchor top $('html, body').animate({scrollTop:target_top}, 500); }); });
——————————–
Updated from post discussion:
From Kasper’s Comment
$(document).ready(function(){ $(".scroll").click(function(event){ event.preventDefault(); var offset = $($(this).attr('href')).offset().top; $('html, body').animate({scrollTop:offset}, 500); }); });
Hi, interesting post. I have been thinking about this topic,so thanks for writing. I will probably be coming back to your site. Keep up great writing
Thanks.
great post ,, many thanks
am having problems implementing localscroll…could you give me a hadnnd?
I got all the file and linked them in my page so whats the next step?
Thanks
Sorry for the delay…
can you send a detailed mail to me?
i will look for the solution…
Is there a way to delay the scroll effect so it does not scroll so quickly?
Hi Ray,
Sorry for the delay.
I didn’t see anything like this, will try to control the time delay.
This is what I was trying to emulate:
http://ftdesigner.net/
Hi, Im having issues with my links only scrolling up… I have links at the top of the page that I would like to scroll down to the anchor, but as of now, they will only scroll up.. any thoughts? Thanks!
Hi Will,
have you tried the demo provided here?
How is the demo working?
Is there a way implement this scroll effect after loading an url. I have an anchor hash after my url but it just loads the page, instantly on my hash. I’m wondering if there’s a way to:
Click link – loads the url
scrolls down to the hash on the new page
Is something like this possible? A delay in the scroll, only after the page has loaded, or an auto scroll to hash effect?
Thank you
Thanks Kyle Hinton,
Yes possible, will try on free time.
thanks so much for this easy to follow post! however i have one question: i have my site set up in a long horizontal div… with the navigation in a seperate div on the left hand side… when i click and go to the anchor, and the page scrolls to the top, i want the overflow to be hidden on the top and bottom with no scroll bar and my navigation to remain on the side…
does this make sense?? can you give me any suggestions?
thanks!
Hi Sarah,
can you upload the demo somewhere and give me the link?
Already i have done something like this, but don’t know it will suit for your need.
However i will write about it in the next post.
Thank you.
I’m almost willing to bet that its because the span tags on the sample code above do not contain ID’s.
Thanks Mike.
Code updated in the post.
Hi, first off get plugin. But I’m having problems with targeting a specific area. Heres an example of my markup.
Contact
back to top
Scroll.js will go to the contact area of my page, but I want it to move further down. I’ve been fiddling around with it but can’t seem to get it to work.
—
I figured out a way to get the javascript to move into the position that I want. Giving each span# a css position: relative and adjusting the top attribute allows me to let the scroll function exactly where I want it to.
I just added this to a FAQ page in a WordPress site. Very cool stuff.
WordPress-specific:
1. You must use ‘jQuery’ instead of ‘$’
2. If using Thesis theme, you must add it to the custom_functions.js file. http://www.customizethesis.com/adding-custom-javascript/
Thanks Jason.
there is a problem…the scroll horizontal doesn’ work….
Any way to alter this to work within a container div?? I have buttons in one DIV and images in another DIV.
You save my life!
Thank you.
Pingback: kndesign.com.ar » Blog Archive » efectos jquery para tener a mano
Pingback: Links of the Week: April 23rd | Bainbridge Studios
thank you so much!
great post!
Nice i have already implimented this is several things i was working on in School
works perfectly!!!
Thanks
Hi, thank you for the great tutorial!
I have only one question. Is it possible to set the duration of the scroll effect?
I don’t know javascript.
thanks again
This doesn’t seem to work for me in IE.
Anyone else get this to work in IE? (IE8 tested, no success)
Same thing. Can’t get it to scroll back up once it scrolls down in IE8.
I have tested the demo (I have given in this post) in IE8. It is working.
If you’re testing this locally, make sure you didn’t get a “to help protect your security, internet explorer has restricted this webpage from running scripts…” message! It works fine for me 🙂
Because it works in Opera shakes the screen?
Thank you very much for this wonderful snippet. I’ve implemented it onto my website (a small sign up link on the bottom of the page) and it works perfectly. There aren’t many elegant and simple solutions for a smooth scroll and your solution is one of them!
That is nice.
Thanks for your feedback Avery.
Just wanted to say thank you! I was searching for smooth scrolling solutions, and yours was by far the easiest to understand for a beginner like me. I’ve bookmarked your site for future reference, thanks so much!
Thanks Austin. 🙂
Thanks,
its nice,
but instead of table can’t we use div there??
Yes, we can use it for div type pages.
Thanks.
Pingback: 2010 review – Pagination and anchor scroll are most wanted | Beschi's Works
Lovely! So simple to get it working. Exactly what I was looking for. Thank you.
Pingback: Man! Week 5 was AWESOME! « ART342~W11
Yosh,
i’m a designer and i want to learn about jquery…i’m sorry i am so baaaaaaaaaaad at coding ..the fact is you gave us the code HTML with JS i think i have to place the .js code inside right??After i went to the jquery website and try to download it, but it give me code that i don’t know what to do with!!
but i really don’t know..please can you give me a file with everything inside , i think it’s gonna be easier for me to learn, i’m so lost with everything.
Looks so simple for you but honestly for me it’s a headheache!!LOL
Thank you very much.
Hi bab,
please com on chat or mail… 🙂
The code above is great, but it has a drawback in a scenario where you put your navigation links in one div and scroll a separate div in such a setup, that both divs are always on screen (the scrolling of the content in the second div does not move the links).
In such a situation, if you click the same link on the left twice in a row, the script scrolls slightly off…
Id like to thank people at ##javascript on freenode, in particularly ‘very’ for a solution and ‘pigdude’ for his time in trying.
The modified code:
// initialize lasttrgt variable (we will need this to check if the link was already clicked)
var lasttrgt;
$(document).ready(function(){
$(“.scroll”).click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url – like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name – home in mysitecom/index.htm#home
var parts = full_url.split(“#”);
var trgt = parts[1];
//check if the target link was previously a target (this would mean that the link was clicked before)
if (trgt!==lasttrgt) lasttrgt=trgt; else return;
//get the top offset of the target anchor
var target_offset = $(“#”+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$(‘#content_field_right’).animate({scrollTop:target_top}, 300);
});
});
Hi v0lZy,
thanks for your comments.
All my codes are some basic examples – how to use JQuery for various purpose. Hope somebody will extend the basic and do good.
Hope your comments will help someone to understand a little bit more. 🙂
Hello, I tried the original script and it worked perfectly; and I tried this edited script to move just a div, and it didn’t work… any ideas what I could be doing wrong? all I did was swap scripts.
An additional modification that solves some issues with refreshing in the case described in my previous post above. Big thanks to ‘very’ again, i take no credit.
(just replace #content_field_right with the div you want to scroll)
// initialize lasttrgt variable (we will need this to check if the link was already clicked)
var lasttrgt;
$(document).ready(function(){
//scroll to top when the page is loaded or refreshe
$(‘#content_field_right’).scrollTop(0);
$(“.scroll”).click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url – like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name – home in mysitecom/index.htm#home
var parts = full_url.split(“#”);
var trgt = parts[1];
//check if the target link was previously a target (this would mean that the link was clicked before)
if (trgt!==lasttrgt) lasttrgt=trgt; else return;
//get the top offset of the target anchor
var target_offset = $(“#”+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$(‘#content_field_right’).animate({scrollTop:target_top}, 300);
});
});
An even better version …
// initialize lasttrgt variable (we will need this to check if the link was already clicked)
var lasttrgt;
$(document).ready(function(){
//scroll to top when the page is loaded or refreshe
$(‘#content_field_right’).scrollTop(0);
$(“.scroll”).click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url – like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name – home in mysitecom/index.htm#home
var parts = full_url.split(“#”);
var trgt = parts[1];
//check if the target link was previously a target (this would mean that the link was clicked before)
if (trgt!==lasttrgt) lasttrgt=trgt; else return;
//get the top offset of the target anchor
var target_offset = $(“#”+trgt).position();
var target_top = target_offset.top + $(‘#content_field_right’).scrollTop();
//go to that anchor by setting the element scroll top to anchor top
$(‘#content_field_right’).animate({scrollTop:target_top}, 300);
});
});
Your code could be a lot shorter, it’s useless to get the anchor name from the url since you can get it from the href attribute.
$(".scroll").click(function(event){
event.preventDefault();
var offset = $($(this).attr('href')).offset().top;
$('html, body').animate({scrollTop:offset}, 500);
});
Hi Kasper,
Nice try. But $(this).attr(‘href’) – will return a value, not will be a selector for an element. So this won’t work.
Have you tried?
Thanks for your comment. I will try to short the code.
OR simply,
we can write like this,
event.preventDefault();
var offset = ($("#"+$(this).attr('href').split('#')[1]).offset().top);
$('html, body').animate({scrollTop:offset}, 500);
Hey Beschi, yes I have tried. And I know $(this).attr(‘href’) returns a value, that’s why you have to use $($(this).attr(‘href’))
Sorry Kasper,
I have tested.
Works great.
Thanks. 🙂
I found a great page that give numerous downloadable examples of how to change the text inside an anchor using jQuery. They are really helpful for web 2.0 and web 3.0 sites.
http://www.ajaxera.com/jquery-change-text-in-anchor/
Hi!
Thank you so much for this scroll effect, really useful!
I don’t know why but since some days it doesn’t work anymore on my website.
I didn’t change anything but now there is no scroll effect on my links…
Can you help me?
You can check this on http://www.menomysofa.com/vers3
Every link on the website should work…
Hi,
I have checked this page: http://www.meonmysofa.com/vers3/
and it was working last night. Tell me still u r facing the problem.
Thanks.
Beschi A.
Great trick Beschi! I’ve used it a few times already.
I’ve looked through the posts and didn’t see any reference to this, but I’m trying to combine this with another effect that requires the anchor ID to be in the URL, like index.htm#about.
Any thoughts on how to achieve this?
Hi Vince,
thanks. Exactly what I thought, and tried. But not completed on that time.
I’ll give you the hint, refer this and try: https://beski.wordpress.com/2009/04/21/ajax-pagination-with-anchor-navigation/
Worked first time! Thank you very much!
Sanj
Thanks.
THAAAAAAAAAAANNNNNKSSSSSSSSSSSS A LOOOOOOOOTTTTT!!!!! =)
I’m having trouble implementing this with WordPress. Is there anything special that I have to do? I have the scroll.js and jquery-1.2.6.min.js in a folder named js in the current theme folder, and I called both of them in the header.php before the body tag.
I saw earlier a comment saying something about $ being replaced for wordpress, but I didn’t understand what the user meant.
Any help would be greatly appreciated.
Thanks!
I think you are talking about: http://api.jquery.com/jQuery.noConflict/
http://www.buchhandlung-arnold.de Very nice blog dude! How do u get so much visitors? My blog is very sad – only 10 visitors per day. Btw: Nice article!
http://www.pixotdesign.com
Pixot is a web design company. We specialize in design for a web application, web startups and e-commerce. We provide high quality web designs based on usability rules. We can guarantee unique designs at a competitive price. If you are launching a new business and you need an unique website design this solution is the best for you.
this is a great feature that I really thought all browsers would have built-in by now. it always bugged me that anchors jump you around with no reference of where you went or how you got there… (I thought at least Chrome would have it…) until then, this js is perfect, thanks
Thanks for this, useful little bit of script 🙂
I had issues with this scrolling to random locations when the page was scrolled. I also added a backscroll amount (anchor_height) to keep an adjacent element visible. Here’s my fixed version:
$(“.scroll”).click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url – like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name – home in mysitecom/index.htm#home
var parts = full_url.split(“#”);
var trgt = parts[1];
//get container offset
var container = $(‘div.list_container’);
var container_offset = container.offset();
var window_offset = $(window).scrollTop();
var anchor_height = $(‘.chart_nav_anchor_line’).height();
//get the top offset of the target anchor
var target_offset = $(“#”+trgt).offset();
var target_top = target_offset.top – container_offset.top – anchor_height + container.scrollTop();
//goto that anchor by setting the body scroll top to anchor top
$(‘div.list_container’).animate({scrollTop:target_top}, 500);
});
The demo appears to be protected? I get a “Authentication required” pop up.
Yentl (Design 311),
Thanks for the info. Will rectify this soon.
Worked a charm, small footprint and functional. Thanks dude.
Looks like your demo and download no longer working?
That’s not really the most effecient way to do this, I’ve managed to strip out 3 lines of code and make it more jQuery friendly. Also this code works with standard page anchor markup.
$(document).ready(function(){
$(“.scroll”).click(function(event){
// Prevent the default action for the click event
event.preventDefault();
// Use the jQuery object to get the href attribute instead.
var parts = $(this).attr(‘href’).split(“#”);
// Work out the top of the target anchor (minusing 20 to show 20 pixels above the anchor for aesthetic reasons
var target_top = $(‘a[name=’ + parts[1] + ‘]’).offset().top – 20;
$(‘html, body’).animate({scrollTop:target_top}, 1200);
});
});
Thanks Ben,
Please take a look at the updated part.
Maybe it’s me, but I don’t see any changes :).
Hi, how do i implement the html code to index.php? help. 😦
When someone writes an paragraph he/she maintains the thought of a user in his/her brain that how a user can understand it.
Thus that’s why this paragraph is perfect. Thanks!
Did you have any luck getting this scroll effect working when clicking an anchor from a different page? I noticed you were going to have a go a couple of years back, so any update on this would be VERY useful. Thanks
demo is fucked up
Pingback: Jump to a specific class using jquery pagination | 我爱源码网