0

I have a template that when user use mouse wheel the template scroll horizontally .

but it just work in chrome .

is there any way to scroll horizontally in firefox and IE ?

here is a demo : http://jsfiddle.net/VhERd/

and jquery :

<script>
    $(function() {

       $("body").mousewheel(function(event, delta) {

          this.scrollLeft -= (delta * 30);

          event.preventDefault();

       });

    });
</script>
    <script src="http://css-tricks.com/examples/HorzScrolling/jquery.mousewheel.js"></script>

and at the end sry for my english ;)

alireza
  • 507
  • 4
  • 12

2 Answers2

2

It needs to work off the html element, not body element for Firefox.

$("body, html").mousewheel....
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

I've used this function, it's a bit hacky, but does the job.

function scrollPage (e) {
    var delta = e.deltaX || e.wheelDelta,
        dir = (delta > 0) ? -90 : 90;
    if (window.addEventListener && !(window.chrome || window.opera)) {
        dir *= -1;
    }
    window.scrollBy(dir, 0);
    e.returnValue = false;
    if (e.preventDefault) {
        e.preventDefault();
    }
    return false;
}

And attach the event:

if (window.addEventListener && (!window.chrome && !window.opera)) {
    window.addEventListener('wheel', scrollPage, false); // IE9+, FF
} else if (window.chrome || window.opera) {
    window.addEventListener('mousewheel', scrollPage, false); // Chrome & Opera
} else {
    document.attachEvent('onmousewheel', scrollPage); // IE8-
}

A live demo at jsFiddle.

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • @alireza Hmm... I tried to add it to your code, didn't get it working. Anyway, works with a [simple layout](http://jsfiddle.net/42t8h/1/). – Teemu Sep 13 '13 at 17:46
  • @alireza Yep, it was just a timig issue when I tried it. Here's a [working fiddle](http://jsfiddle.net/JFyFc/) with your code. I guess you can convert it to jQuery too, the main issue is the different event names (`onwheel` and `onmousewheel`), also directions are not consistent. – Teemu Sep 13 '13 at 18:06