Horizontal page animation with jQuery
Using Page scroll to id jQuery plugin for smooth scrolling between same-page sections in horizontal layout.
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>
In order to create a horizontal layout for the markup above, you’ll need few basic css rules
body{ width: 400%; } #navigation, #content{ width: 100%; } #content > div{ width: 25%; float: left; }
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 and set layout
option parameter to "horizontal"
<script> (function($){ $(window).load(function(){ $("#navigation a").mPageScroll2id({ layout:"horizontal" }); }); })(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.
Hi,
I’m just trying to strech the page completely to 100% width. I just don’t want to see pag2 when i’m seeing page 1. How do I do it?
Thanks in Advance.
Hi,
Please check this demo:
http://manos.malihu.gr/tuts/horizontal-animate-page-to-id-jquery-full-width.html
I think it’s similar to what you need.
Ultimate
Thanks for the great plugin,
This has been really helpful!
Hi Malihu,
I’m trying to make this horizontal page animation responsive, to work with iPhone etc.
Here’s the page I’m working on:
http://bit.ly/Pu2Mbd
Can you help?
best regards,
Sambodhi Prem
Hi,
thanks for the great plugin,
this is the second time I use it but I just want it to start automatically, and there are some divs inside that I do not want to scroll or animate with it..any ideas? please?
hey @Luis Santamarina thx man!!!!!!!!
your solution are great man!! thank you
Awesome script, thanks.
I have set this up for my website and have left the overflow-y to scroll as I have lots of vertical content. Is there a way of scrolling back to top vertically as well as horizontally in one action?
http://www.safwanonline.co.uk/new/
Here is my current set up and as you can see if you are scrolled down on the web design area then click another menu option, it scrolls horizontally but the content in that area is at the top.
I have tried playing around with the animate() values with “top” as well as left but my jquery knowledge is limited.
Would really appreciate any help.
Thanks
Hello,
I’ve made a jQuery plugin that does exactly what you need. If you have time to implement it, I’d suggest using it as it provides more options and features (scrolling both axis, deep linking incorporation etc.). It’s easy to implement and works well on touch devices.
You can find it here:
http://manos.malihu.gr/animate-page-to-id-with-jquery
Thanks mate, will take a look at it.
Why doesn’t it work when I try to use it with jquery or callback?
I actually would like to use this slide behaviour with the change of hash, but it starts to misbehave even before trying that
Here’s a Fiddle to look at
http://jsfiddle.net/besign/jr7JR/2/
I’ve changed Links #c1 and #c4 to what I want and you can see inmediately that after those are triggered, the position starts to miss.
And if you start with a hash, same happens:
http://jsfiddle.net/m/ute/#c4
Any ideas? Great Post with this info btw, I’m dreaming of implementing this, but in the explained way!
Hello,
I’ve made a “proper” jquery plugin for vertical and horizontal page animation that works very well with deep-linking scripts. Check it here:
http://manos.malihu.gr/animate-page-to-id-with-jquery
Example of horizontal page scrolling with deep-linking:
http://manos.malihu.gr/tuts/animate-page-to-id/plugin/demo-horizontal-layout.html
I believe this will do exactly what you need.
This has been really helpful!
I have a question about background images. I’ve been trying to place a background image, and have had no luck getting it to appear. I’ve tried placing it in the body, in the containers, and no matter where I put it in my CSS file, it’s just not working. I’m 100% I’m linking to the file the right way, and can’t figure out why it’s not working.
Any tips?
Great post and great site malihu. 🙂
Keep up the great work.
Bookmarked ♥
Great post!
I used this for my project for school.
But I can’t seem to center my divs in the middle of the screen (horizontally). I have a slideshow on C1 and i want it to stay in the middle of the page, no matter the screen size. Is this possible?
any help would be great!
omg
You have a fantastic post here, really helpful.
thank you!
Thanks 🙂
This post will be updated very soon with a proper jquery plugin, wordpress plugin, css template and complete guides.
Hi, i have a problem, its olny working fine to #c6 when i make #7 this go back to #c1 .
you can check in the demo i made. http://www.mitomante.com.ar/test
mitomante == c1/cuentos == c2/comics==c3/poemas==c4
cuentos-mondob == c5/cuentos-compendio==c6/cuentos-cuentosrobots==c7….this go back to c1.
thank you!
this is the solution I found
the width of the container is 10000px, and the width of every element (# c1,2,3), was 1500px.
if the sum of them passes 10000px (1500×7), then you can include the width of the container based on the total width of the elements .
Hi Luis….
i didn’t understand what you mean. Can you help me out?! im heaving the same problem…
Hi Luiza, what Luis meant is that in the css section, change the content of the div “#container”. Default width is 10,000. If you have 7 pages, change the “width” to 12,000. The more pages you add just add another 1500.
Got it ! thanks!
Hi malihu,
great stuff mate…I need to put rollover images instead of text in the menu..Its not working..
and I want to put twitter feed in c1 and want to put image thumbnail in c5 with jquery scroll.. how do I do it man.
Looking forward for easy solution.
Thanks
Hello, I’m having problems in the transition of some pages, you can help me?
cv.griza.com.br
Specifically 5 >> 6
Hello Daniel,
There’s a bug in jquery that resets to zero, an animate value greater than 9999 pixels.
To fix it, please check this post:
http://manos.malihu.gr/jquery-thumbnail-scroller
and specifically the section “Scrolling long content” where I provide a quick-simple solution.
Hi malihu,
great stuff mate…I need to put rollover images instead of text in the menu..Its not working..
and I want to put twitter feed in c1 and want to put image thumbnail in c5 with jquery scroll.. how do I do it man.
Looking forward for easy solution.
Thanks
Is it possible to set these scrollers to go to different pages within the site instead of just different divs located on the same page? Say if i had index.html and I wanted to link it in a way such as:
Photography
It seems that the scrolling doesn’t work anymore. Sorry this is probably a dumb question, im pretty new to all this.
Thanks,
Sam
Ok, forgive me if I’m missing something simple…
I want to reverse the animation. Have the div swipe to the right instead of the default left. I’ve been trying to days to figure this out. Any help would be appreciated!
how to adjust de screen on the middle , any ideias?
margin: auto; /* in your css */
Hi Manos,
First off thanks much for the AWESOME posts! They’ve been very helpful.
I’ve a question for you. I’m using the Horizontal page animation with jquery on my new site. I would like to have the background image to “stretch” & “shrink” depending on monitor size. I currently have the image dimensions at 1920×1280.
The image scales vertically but not horizontally.
May I please request some assistance with this?
I could sure use your input. Please reply with the e-mail provided.
Thanks in advance,
Mark
I have the same problems adding “c7” can you help me
this not work on php
Hi Malihu, just a quick question on your script. If I have dynamically created pages in a textpattern site, my page contains nine images per page. I would like to animate within the pages using your script. is it easy to achieve as the page numbers increase as I add images to the page(s). (example: http://codecanyon.net/item/tonic-gallery-jquery-xml-portfolio-gallery/full_screen_preview/120710)
Thanks for the answer. Amazing site, you are very generous with your talent.
hi there, I’m sorry to ask such a basic question but I’m new to this! Could this be used to scroll between html pages?
Thanks in advance,
Yes, you can create a single-page website with this script (with ajax, load() etc.), although it’s gonna be a bit hard to create such website if you don’t have enough experience in js and css.
I have a listbox inside div the height of the listbox depends on the number of elements , this way the scroll bar is shown to div when i click the first or last element of the list box my page scroll bar move how can i stop this behaviour it only appear in safarie 4.0 IE 7 works fine
I need to place listbox inside div becz in safarie if the listbox is empty it show disabled scroll bars
Hey very nice article you got here!!!!!! i have been looking for something like this..
but anyway malihu.. mind if i ask something? uhm…
i tried adding another set of page ” c7″ but i cant seem to find a way for it to have its own page when i click the anchor ‘content 7’ it just goes back to content 1 page.. i double checked everything but i don’t think i know whats wrong thats why..
do you mind replying to my email (this is my first time to comment so if i am gonna receive a mail regarding your reply no need to mail your reply)
[email protected]
Thanks alot
heya,
I’ve bee scouring internet trying to find various scrollers, this seems to be the only one I found that does the trick with the CMS i’m using.
Now the thing is I liked the vertical scroller but didn’t seem to work. I am trying to reference a with a nested .html. this one worked by changing “left” to “top” although I don’t think this is really the best way to do it.
Now for the sake of my site I need the overflow-y set to auto so as content is arbitrarily added it will force the scroll bar. for some reason as I jump down to my “id” points the page itself disappears or is interpreted as a shorter and shorter page so the scroller dissappears, then if I click my button to load different content into that the information remains the same so the new page loads at whatever y-axis the scroller left it at.
Is there anyway to either properly adopt your vertical scroller with the technique you used here, or somehow ‘clear’ the xy axis of this function.
I’ll keep trying but I thought i’d go to the source.
Thanks for any help in advance, and awesome and simple work, the way it should be.
Hi jay,
I need to check your implementation online to see if I can help
“shahid saifi” is right, really helpful&fantastic!
I am a newbie, can you suggest how to change the code that would page is start from DIV id=”c2″ (Content 2) or “c3” (Content 3)
Thanks in advance
Run Animate2id function when page loads like this:
$(window).load(function(){
Animate2id(“#c3″,”easeOutQuint”); return false
});
Put this code at the beginning of the script, right before Animate2id function, changing “#c3” to the div id you want.
Malihu thanks!
but I’m probably quite kettle, is doesn’t work (
It’s correct?
$(window).load(function(){
Animate2id(“#c3″,”easeOutQuint”); return false
});
function Animate2id(id,ease){
var animSpeed=2000;
var $container=$(“#container”);
if(ease){
var easeType=ease;
} else {
var easeType=”easeOutQuart”;
}
$container.stop().animate({“left”: -($(id).position().left)}, animSpeed, easeType);
}
Yes it is correct. The reason it doesn’t work is because of wordpress quotes (happened because you copied the code from my comment). In your code, delete and re-type the double-quotes on the function and it’ll be ok.
yep, all is right.
thank you very much!!!
That´s insane man :0, very useful!
You have a fantastic post here, really helpful.
Awesome. Works great in most browsers but the menu gets corrupted in chrome 6……. but it works great in safari. Don’t understand why that is.
Indeed! I’ll check it as soon as possible. Thanks for checking it Chichi
Code and files updated to work in Chrome 6. Seems Chrome acts weird when animating “body”, so I added a wrapper and scroll this instead using .position().left instead of srollLeft 😉
Nice one, bookmarked and added to my linklist 🙂