Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing vertical scroll to horizontal

Tags:

html

css

scroll

This seems to have been asked before without a sufficient answer. I have absolutely no knowledge of how to do this (including no plugins). I want my website to scroll horizontally when I actively scroll down vertically.

Some examples of what I mean: http://hotdot.pro/en/ and http://mashup.ikm.gda.pl/

To be clear, I don't care about the parallax effect that both of these websites utilize. I just want to scroll horizontally.

Thanks!

like image 238
Andrew Weatherly Avatar asked Jul 08 '14 18:07

Andrew Weatherly


People also ask

How do I change my scroll bar from vertical to horizontal?

Click File > Options. On the Advanced tab, scroll to the Display section. Select Show horizontal scroll bar and Show vertical scroll bar, and then click OK.

How do I turn on horizontal scrolling?

Horizontal scrolling can be achieved by clicking and dragging a horizontal scroll bar, swiping sideways on a desktop trackpad or trackpad mouse, pressing left and right arrow keys, or swiping sideways with one's finger on a touchscreen.

How do I scroll horizontally with normal mouse?

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 change the vertical scroll bar?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.


1 Answers

This can be solved in native JavaScript, without any library.

Example code

function transformScroll(event) {
  if (!event.deltaY) {
    return;
  }

  event.currentTarget.scrollLeft += event.deltaY + event.deltaX;
  event.preventDefault();
}

var element = document.scrollingElement || document.documentElement;
element.addEventListener('wheel', transformScroll);

Explanation

While this solution does not lean on any library, it also takes other usage scenario's into account. When scrolling on a touchpad for example, the other solutions that lean on a single delta value all fall apart.

There are always multiple ways of scrolling with an input device. Laptops have touchpads, phones have touchscreens, mouses have scroll wheels. On top of that, you can modify your scroll direction for a scroll wheel by holding shift.

When preventing the default behaviour, we should also include the other possible directions a user can scroll in. Luckily screens only have two dimensions, so adding deltaY to deltaX covers all cases in this scenario.

like image 98
Guido Bouman Avatar answered Nov 09 '22 10:11

Guido Bouman