I have a body class like this:
<body class="horizontal">
I try to target with the following code:
'use strict';
// Horizontal scrolling
// http://www.dte.web.id/2013/02/event-mouse-wheel.html
(function() {
if(document.body.className === 'horizontal') {
alert('It exists');
function scrollHorizontally(e) {
e = window.event || e;
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
// Scroll to the left or right in `document.documentElement` and `document.body`
document.documentElement.scrollLeft -= (delta * 75); // Multiplied by 75
document.body.scrollLeft -= (delta * 75); // Multiplied by 75
e.preventDefault();
}
if (window.addEventListener) {
window.addEventListener('mousewheel', scrollHorizontally, false); // IE9, Chrome, Safari, Opera
window.addEventListener('DOMMouseScroll', scrollHorizontally, false); // Firefox
} else {
window.attachEvent('onmousewheel', scrollHorizontally); // IE 6/7/8
}
};
})();
But this is just not working. The alert
not popping up. I would like to prefer a pure javascript solution, to not include Jquery because of this.
EDIT: I uncommented the horizontal scroller function, then it's working. So the script is causing the issue.
The entire script is called at the bottom of the body.