Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal scrolling with mouse wheel in a div

Tags:

html

scroll

How to scroll horizontal in na div with mouse wheel, or drag with jquery?


I've tried draggable, but in my code it isn't useful.

Now I've got a horizontal scrollbar. Is there a posibility to scroll the content in my div using the mouse wheel?

like image 508
Daniel Koczuła Avatar asked Jul 28 '12 12:07

Daniel Koczuła


People also ask

How do I make my mouse wheel scroll horizontally?

Scroll horizontally using a keyboard and mousePress and hold SHIFT. Scroll up or down using your mouse wheel (or another vertical scrolling function of your mouse).

How do I scroll horizontally in a div?

Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line. Here the scroll div will be horizontally scrollable.

How do I create a horizontal scrolling container?

To enable horizontal scrolling, we can use the CSS property overflow-x. If we assign the value scroll to the overflow-x property of the container element, the browser will hide horizontally overflowing content and make it accessible via horizontal scrolling.


1 Answers

Try this for horizontal scrolling with mouse wheel. This is pure JavaScript:

(function() {     function scrollHorizontally(e) {         e = window.event || e;         var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));         document.getElementById('yourDiv').scrollLeft -= (delta * 40); // Multiplied by 40         e.preventDefault();     }     if (document.getElementById('yourDiv').addEventListener) {         // IE9, Chrome, Safari, Opera         document.getElementById('yourDiv').addEventListener('mousewheel', scrollHorizontally, false);         // Firefox         document.getElementById('yourDiv').addEventListener('DOMMouseScroll', scrollHorizontally, false);     } else {         // IE 6/7/8         document.getElementById('yourDiv').attachEvent('onmousewheel', scrollHorizontally);     } })(); 

Here’s a demo, but with document.body and window as a targetted element: https://taufik-nurrohman.github.io/dte-project/full-page-horizontal-scrolling.html

like image 165
Taufik Nurrohman Avatar answered Oct 09 '22 00:10

Taufik Nurrohman