Horizontal Scrolling with jQuery [Updated]

The large majority of websites on the internet prefer vertical, portrait pages and for a good reason. Vertical pages work well with the way we read and the hardware that we use. But sometimes, just sometimes, it is nice to be different and build a website around the horizontal axis. This, however, poses limitations because the computer mouse, and it’s mouse wheel, does not scroll sideways – but this can be rectified with good-ol’ jQuery.

To get horizontal scrolling, the Mouse Wheel Plugin by Brandon Aaron (GitHub, Download) will be used to detect mouse wheel movements like a keypress and of course, jQuery itself will be used. Check out the Demo.

HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="js/jquery.mousewheel.js"></script>

JavaScript

$(document).ready(function() {
	$('html, body, *').mousewheel(function(e, delta) {
		this.scrollLeft -= (delta * 40);
		e.preventDefault();
	});
});

The JavaScript can just be added into the Head tag. Note that scrolling applies to html, body and * (everything) – this enables it to work across different browsers. The event.preventDefault() just disables vertical scrolling.

Demo

For more information, check out the CSS-Tricks page which has some helpful comments.


… As an Alternative

Update 11/11/12: A very promising jQuery plugin (which I’ve yet to actually test) is available called Sly which is worth researching.

Check Out Sly


  Recent Articles

Blur a Background Image (with help from blur.js)

Blur a Background Image (with help from blur.js)

Recently, in a very small project of mine, I wanted to create a full-size image as the background and to blur it a little to create an abstract but interesting backing image. Initially, we...

View Full Post →

Showing your E-mail Online

Showing your E-mail Online

We all know about spam and up until recently there wasn't an e-mail address shown on this blog (just a contact form) for this very reason. Most of us have also tried different methods of s...

View Full Post →

11 Comments

  1. daslicht says:

    Nice ! That combined with an iScroll* like inertia scrolling but with magnetism (scrolling snaps to its items so that anytime whole pages are shown)

    Would be awesome :)

    *http://cubiq.org/iscroll-4

  2. Jana says:

    Great demo, thanks!!

  3. Dominic says:

    Hey there!

    I was wondering if you managed to got this horizontal scrolling working in firefox?

    Cheers,
    Dominic

    • Dominic says:

      Nvm :-)

      It works perfectly also in firefox, just made a mistake.

      Thanks for this great demo!

      Cheers

  4. Alex says:

    Any idea how you make it so that you can just scroll when the mouse is over the images, but no over the rest of the page? Otherwise you can’t have vertical scrolling (I’ve removed the css to hide vertical scrolling).

    • Edd Turtle says:

      In the demo we used the $(‘html, body, *’) selectors, have you tried replacing that with a <div> tag containing images or something? Would be interesting to know how easy it is.

  5. Alex says:

    Yeah, I tried but it doesn’t seem to make a difference. Actually, the only that seems to matter is the *

  6. Alex says:

    Cheers. I think I’ll end up using this, just for ease of implementation for my needs.

    http://www.htmldrive.net/items/show/966/jQuery-Horizontal-automatic-Scrollbars-with-mouse

Leave a Reply