The selected answer doesn't work in my browser (FireFox).
Here is a solution I've gotten to work pretty nicely.
HTML:
<body>
<div id="b">
<div class="img_holder">
<img/>
<img/>
<img/>
<img/>
<img/>
<img/>
<img/>
<img/>
</div>
</div>
</body>
When you scroll, you are essentially moving .img_holder
left and right within #b
. This method requires that .img_holder
is as wide (or wider) than the sum of the images you'll be placing in it, as well as all the padding and margins. #b
is the desired width to be viewed on the page.
CSS:
#b {
width: 400px;
border: 1px solid #111;
padding: 0px;
margin: 0px;
overflow-x: scroll;
overflow-y: hidden;
}
.img_holder {
padding: 0px;
margin: 0px;
height: 100px;
width: 910px;
}
img {
height: 90px;
width: 100px;
padding: 5px;
background: #123;
display: inline-block;
}
JavaScript:
var scroller = {};
scroller.e = document.getElementById("b");
if (scroller.e.addEventListener) {
scroller.e.addEventListener("mousewheel", MouseWheelHandler, false);
scroller.e.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
} else scroller.e.attachEvent("onmousewheel", MouseWheelHandler);
function MouseWheelHandler(e) {
// cross-browser wheel delta
var e = window.event || e;
var delta = - 20 * (Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))));
var pst = $('#b').scrollLeft() + delta;
if (pst < 0) {
pst = 0;
} else if (pst > $('.img_holder').width()) {
pst = $('.img_holder').width();
}
$('#b').scrollLeft(pst);
return false;
}
Make sure that you use conditions to stop from scrolling below zero and above the max width of the container (if
conditions near bottom of code). Also, the delta
value when scrolling is very small, and so to prevent the user from having to scroll a bunch I've multiplied it by 20. You can choose any value you'd like to adjust scrolling speed.
Also works nicely if you want to hide the horizontal Scroll Bar:
#b {
overflow-x:hidden:
}
As shown here