Animate page to id with jQuery
Using Page scroll to id jQuery plugin for smooth scrolling between same-page sections.
Page scroll to id works by connecting links in the form of <a href="#id">link</a>
, to sections within the document, in the form of <div id="id">target</div>
. The plugin replaces the default browser behaviour of “jumping” to page elements that have id equal to the hash (#) in the address bar, by preventing the url change and animating the page.
Normally, your markup should contain a list of links (as the page navigation) and the target elements (as the page sections) with your content
<ul id="navigation"> <li><a href="#section-1">Section 1</a></li> <li><a href="#section-2">Section 2</a></li> <li><a href="#section-3">Section 3</a></li> <li><a href="#section-4">Section 4</a></li> </ul> <div id="content"> <div id="section-1"> Section 1 content </div> <div id="section-2"> Section 2 content </div> <div id="section-3"> Section 3 content </div> <div id="section-4"> Section 4 content </div> </div>
Download the plugin and extract jquery.malihu.PageScroll2id.min.js in the directory containing your html files.
Include jQuery library (if your project doesn’t use it already) and jquery.malihu.PageScroll2id.min.js in your document’s head tag or at the very bottom of your html, just before the closing body tag (recommended for better performance)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="jquery.malihu.PageScroll2id.min.js"></script>
After files inclusion, call mPageScroll2id
function on links you want the plugin to handle
<script> (function($){ $(window).load(function(){ $("#navigation a").mPageScroll2id(); }); })(jQuery); </script>
You may change "#navigation a"
to some other selector according to your markup.
To change the animation speed, set the duration (in milliseconds) in scrollSpeed
option parameter
$("#navigation a").mPageScroll2id({ scrollSpeed:900 });
Page scroll to id provides a wide range of options and features include vertical and/or horizontal scrolling, ready-to-use classes for links highlighting, user defined callbacks, advanced scroll-to position offset and more. For more info and plugin documentation visit plugin homepage.
Setting padding on the elements being scrolled to is resulting in inconsistent placement of the nav menu even with position: fixed. It scrolls to different place from above than from below, and the same padding on different elements produces different results. This is using the wordpress plugin. I can’t provide link because in maintenance mode. Do you offer support via pm?
Send me an e-mail 🙂
Your plugins are very sleek! Very cool demos. Its inspiring!
This was working fine, but when I added a jQuery Tooltip to the page, which works great…I now get an error of :
TypeError: $(“a[rel=’m_PageScroll2id’]”).mPageScroll2id is not a function
[Break On This Error]
$(“a[rel=’m_PageScroll2id’]”).mPageScroll2id().address();
Do you know what this might be and why?
Here is my code:
$(document).ready(function(){
$(window).load(function(){
/*$(“#light_blue_phone”).tooltip({tipClass: ‘tooltip1’, effect: ‘slide’});
$(“#tan_phone”).tooltip({tipClass: ‘tooltip2’, effect: ‘slide’});
$(“#dark_blue_phone”).tooltip({tipClass: ‘tooltip3’, effect: ‘slide’});*/
/* call both mPageScroll2id and address functions on our selector */
$(“a[rel=’m_PageScroll2id’]”).mPageScroll2id().address();
/* call “externalChange” method of jquery address plugin to scroll page on browser back/forward */
$.address.externalChange(function(event){
if(event.value){
var eVal=event.value.replace(“/”,””);
if(eVal===””){
eVal=”top”;
}
/* call “history” method of mPageScroll2id and pass jquery address hash value */
$().mPageScroll2id(“history”,{
scrollTo:eVal
});
}
});
});
});
$(document).ready(function() {
$(“#light_blue_phone”).tooltip({tipClass: ‘tooltip1’, effect: ‘slide’});
$(“#tan_phone”).tooltip({tipClass: ‘tooltip2’, effect: ‘slide’});
$(“#dark_blue_phone”).tooltip({tipClass: ‘tooltip3’, effect: ‘slide’});
});
Very cool, plugin! I could not get this to work with easing effect in WordPress until I found this plugin.
Quick questions – It scrolls just a little past where I want it to how and where can I add a negative offset and what would be the code to do so?
Also, How and where would I add code so that my nav menu is highlighted on the section where the screen is at..
Thanks Much.. Scott
Hello and thanks for your comments.
If it seems to scroll past where you visually want it, you can add a top padding to the target element or insert another tag (e.g.
<a id="your-id"></a>
) above the element you’re scrolling to and act as your anchor point.The plugin scrolls the page to the exact position of the target element (its position includes its padding, but not its margin). If you check the demo, I’ve also added a padding to the sections I’m scrolling to in order to give them the offset of the top menu.
To highlight a navigation menu item while scrolling (via the plugin or manually), check the code in custom.js of the CSS template.
Thank Malihu, I think it was just the wp admin bar that was hiding my page title that was making me think it was scrolling past 🙂
Great Plugin
Thanks Scott
This was my next go but could not get it working.. may have been a conflict or something but really cool men… If anyone gets this working on wordpress please share a short screen cast on screenr.com or something 🙂
http://tympanus.net/codrops/2010/04/30/rocking-and-rolling-rounded-menu-with-jquery/
Hello! I started using your plug in today on one of my pages. When I implemented the plug-in and started using it, the scrolling worked great, but it made my sidebar disappear! I have a sidebar on the site that includes navigation. Any ideas on how to make it come back? It disappeared for the page I am using your plug-in on only.
I really need some help..
What i am trying is to have a vertical scrolling (top bottom) with 10 sections and 7 of them i want to use the horizontal scrolling in diffrent sections/categories but somehow combining the 2 and changing some values it doesnt work..
Is it possible to hide the various target divs, an only make them visible once they have been activated via the function (then hide everything else)?
Thanks!
Will this plugin work with any WordPress template, or does it have to be a “one page” type of template? Do you have a starter template that you recommend that will work well with this plugin?
Thanks!
It works with any template. The most obvious implementation would be on a single-page template of course, but you can also use it on any type of template for back-to-top links, jump to comments section, etc.
HI !!! this is a nice plugin. Unfortunately we have a problem with touch scrolling on the iPad. Maybe you have a solution for the following problem:
On iPad the first touch on the navigation works like charm, but touching the next link won´t work. Now if I scroll down or up for a pixel or more the scrolling effect works again.
Doe you have an idea?
Thanks in advance,
Hello,
Unfortunately iPad’s implementation of position:fixed CSS rule is a bit buggy. Seems that it requires a touch-swipe to re-position a “fixed” element within the viewport and since we scroll the page via javascript (without swipe), the bug happens.
I don’t know when they’ll implement a fix for this, so currently the only thing you can do is either remove the fixed navigation and have a non-fixed menu at the top, or scroll the menu via javascript like the one I’ve made on the CSS template:
http://manos.malihu.gr/tuts/animate-page-to-id/css-template/
Hello!
Thank you very much indeed malihu for a very helpful plugin! Having the exact same issues on my ipad and iphone I’m thankful for getting an explanation to it as it was driving me mad. After a quick google search I found this solution which I presume might work.
http://14islands.tumblr.com/post/30313367126/solved-position-fixed-scrollto-bug-ios
I’m however completely lost when it comes to javascript so I have no idea how incorporate it.
Thanks! I’ll check it and see if I can implement it in the plugin.
Oh, that would be fantastic! Apple are supposedly fixing this for IOS 6 though one can never be too certain. Thank you again for your help!
Hey great work. Everything works fine but I am building a fluid site with a fixed header and when I click an anchor it scrolls but lands at the top position of my fixed header. How do make it land right where the header element ends?
Hello,
This is normal. Just give the target element (the element you’re scrolling to) a top padding equivalent to your fixed header height.
Hi there. This script is really great, and works perfectly on my desktop browsers, but I’m having some troubles porting it over to work in Safari on the iPhone. I’ve been toying with the code, and have been able to make the transitions work on my mobile, and in the development simulator for iPhone/iPad. I feel as if there is something very simple that I am missing. Any suggestions would be welcome. My function code to call is as follows:
(function($){
$(window).load(function(){
$(“a[rel=’m_PageScroll2id’]”).mPageScroll2id(
{
scrollSpeed:1700,
autoScrollSpeed:true,
scrollEasing:”easeInOutExpo”,
scrollingEasing:”easeInOutCirc”,
callback:function(){},
pageEndSmoothScroll:true,
layout:”auto”
});
});
})(jQuery);
The symptoms seem the same as @Botond, but only in the mobile browser is it jumping without a smooth animation.
Thanks very much,
Derek
Hello,
The script automatically removes animations when layout is “auto” (page scrolls both horizontally and vertically) and touch device is detected.
I’ve made some tests on iOS 5.1 (on an ipad) and seems that touch devices do not handle animating root elements on both axis. This is a touch device OS related issue so there’s no fix or workaround. This applies only to “auto” layouts when viewed from such devices.
Removing the animations is essential in order for the plugin to work well and be accessible from anywhere (desktop or mobile).
Ahh. I see. Thank you for the quick response. This explains accurately why some of your examples work in the emulator, and some don’t. I had been trying to ascertain some coding difference there, but had no idea that it was a limitation of the software in that particular hardware set.
Derek
WordPress plugin not working, when I user an HTML5 Reset theme.
Theme source: http://html5reset.org/#wordpress (Unfortunately) I can’t give you a direct link, the site is not public yet.
I downloaded the plugin and followed the instruction regarding the implementation. When I click on the anchor it jumps to the right place, but without smooth scrolling. I think that the jquery is not loaded…
Do you have any ideas how could I solve this problem?
Thanks,
Boti
From what you say either jquery, jquery ui or jquery.malihu.PageScroll2id.js is not loaded. Check if paths are correct for those files and try opening your page with Firefox and inspect it with Firebug to see if any errors appear on the console.
hi malihu and how do we make smooth work , then? should i install jquery to wordpress? how to do it? not very expert on it sorry to be asking... hugs
Hello, this is a great tool, i’m wondering how can i freeze the header, something like this: http://www.pedrote.com
Thanks !!!
Give it
position:fixed;
in the css.Thanks !!! But after possition:fixed, the sections names shows in the same place as the logo
Hey, how could I configure this so rather than scrolling so the specified element is at the top of the page, it scrolls until it’s in the center. Thanks for the great work!
Question: Will this work on a div within a page? In other words, the entire page will not move, just a specific div on the page will scroll?
Nope. The plugin scrolls only the root element (html or body).
Hi man,
thank you for your greit plugin.
I’ve already tried other scripts, but I think your is the best for my design.
I’ve a problem.
my page simulates a chess board.
how can I make the menu follows the horizontal scrolling?
If you try my demo, you’ll notice that the memu is working only in the first row.
and, another “little” question.
How can I make your script work with li element instead of link?
i want to integrate fancybox with the links.
Thank you man,
sorry for my english,
Stefano
take a look here
Hello!
The css-template menu is made for vertical navigation only.
If you wanna make the menu follow both x and y axis, edit custom.js and replace NavigationMenu function the with the following one:
$.fn.NavigationMenu=function(speed,easing){ var $this=$(this); var topPosition=$this.position().top; var updatePosition; var leftPosition=$this.position().left; var updatePositionX; $(window).scroll(function(){ updatePosition=$(this).scrollTop()+topPosition; updatePositionX=$(this).scrollLeft()+leftPosition; $this.stop(true,false).animate({top:updatePosition,left:updatePositionX},speed,easing); $().NavigationMenuHighlight(); }); }
You also need to give a fixed width to
#navigation-menu
in css.On the second question, the plugin works by getting the href hash (#) value of elements, so I can’t really suggest giving href attribute to list elements.
Hope this helps
Thank you for your comments
Hey man is the single-css file directly downloadable I’m having problems getting an image to expand to full browser width
The download archive contains all css template files inside css-template folder.
teset
sadasdasd
yooooo!
fff!!!
ttteeesttt
testing55
Just started delving into this library, thanks for the help.. whats cross browser compatitibility like? I know webkit doesnt support.. thanks in advance! will link you when ive finished building 😉
Hello,
The updated jquery and wordpress plugins, as well as the css template work on every desktop browser and touch device (I’ve tested scripts successfully on iOS 5.1 on iPad).
Hi,
Im struggling to get this to work for my situation…
I’ve got a jqueryui dialog, on which is a scrollable div. Im trying to get the div to scroll to a certain section using your plugin
I am impressed about the reduced code to your above horizontal scrolling example with fullscreen images and next/prev. navigation.
Do you also have the same with an example of vertical scrolling?
I would appreciate it to see it in action.
Gracias!!
The equivalent of this script for vertical scrolling:
http://manos.malihu.gr/animate-page-to-id-with-jquery
I am new to all this just started learning css/js/jquery yesterday. but I was trying the same thing without “easeInOutExpo” and it still works fine, I dint actually try working it with the easeInOutExpo thing cause I think for some reason it is not able to find the function, anyways this is what exactly what I used:
$(function() {
$(‘ul.link a’).bind(‘click’,function(event){
var $anchor = $(this);
$(‘html, body’).stop().animate({
scrollTop: $($anchor.attr(‘href’)).offset().top
}, 3000);
event.preventDefault();
});
});
It is exactly what you did but this way I don’t have to go every time and call the method everywhere a link is defined, it automatically activates when a link is clicked. I am not sure what adv/disadv this has so please let me know if any and if you can, can you brief me on what difference would it make if I used easeInOutExpo.
I have included the file but I dont know why it cant read the method, any suggestion there too would be great help.
Muito bom, parabéns pelo tutorial!!!!
Obrigado!
Google Translate helps 🙂
Pretty solid system, thanks!
Is it also possible to use prev & next button?
For instance if you are at C3, you could go back to C2 or next to C4…
I haven’t implement an automatic way of scrolling to next/previous id yet. I might give it a go in the future. For now, you can always do it manually 😉
I’ve done some testing with horizontal scrolling with next/previous scrolling. It has some extra stuff like fullscreen background images etc., but to get an idea:
http://manos.malihu.gr/tuts/horizontal_single-page_template/page_srolling.html
Thnx for the great work you’ve done for me, it gives great examples.
Were can I find the source of your example for: “horizontal_single-page_template/page_srolling.html”
Im trying to create a website that has horizontal navigation.
Every page that I navigate to needs full width (so no other page, left or right, is shown at the same time).
The page content should be around 900 width and floating in the middle.
The last bits wont be the problem, but the navigation part is.
Hope that the code from the link will help me..
greetings,
Mark
Nice work!