-1

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.

Lanti
  • 2,299
  • 2
  • 36
  • 69
  • 3
    Did the DOM even load before that function is executed? – Sebastian Simon Feb 17 '16 at 19:15
  • 1
    Is this JavaScript code before or inside of the `` tag? You'll need to show a complete web page with HTML and ` – apsillers Feb 17 '16 at 19:15
  • 3
    [How to use the console](https://developer.chrome.com/devtools/docs/console) – adeneo Feb 17 '16 at 19:17
  • your code is working check this >> https://jsfiddle.net/vp6htpf8/ as others said you have to put { – Vivek Solanki Feb 17 '16 at 19:18
  • 2
    You would be better off using `document.body.classList.contains('horizontal')`. –  Feb 17 '16 at 19:18
  • @VivekSolanki Yes, with the “load type” set to “onload”, it obviously works. – Sebastian Simon Feb 17 '16 at 19:18
  • No need for `attachEvent`--that's some historical artifact. –  Feb 17 '16 at 19:19
  • The `scrollHorizontally` function is irrelevant if you want the alert to popup. Check your console for errors because there's something else going on if it isn't failing when you include that script. – Mike Cluck Feb 17 '16 at 19:20
  • No it's not, because I uncommented it than the script is executing the alert, but if `scrollHorizontally` present, then even `alert` not executing. – Lanti Feb 17 '16 at 19:21
  • @torazaburo - IIFE no? – j08691 Feb 17 '16 at 19:21
  • duplicate of [Uncaught SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function](http://stackoverflow.com/questions/24573061/uncaught-syntaxerror-in-strict-mode-code-functions-can-only-be-declared-at-top) – apsillers Feb 17 '16 at 19:38

1 Answers1

0

just remove "'use strict';" and it will work see this

Vivek Solanki
  • 452
  • 1
  • 7
  • 10
  • It works! Thank You! Can you give me an explanation why causing this? – Lanti Feb 17 '16 at 19:25
  • No, with use strict it only runs if I uncommenting `scrollHorizontally` (which why I got lots of downvoting, because the "smart ones" thought presenting in the code that part was irrelevant to the question). I use Firefox latest. https://jsfiddle.net/vp6htpf8/3/ – Lanti Feb 17 '16 at 19:28
  • @apsillers Firefox 44.0.2 x64 Running this there's NO ANY ALERT, just only if I uncommenting the `scrollHorizontally`: https://jsfiddle.net/vp6htpf8/2/ – Lanti Feb 17 '16 at 19:31
  • @apsillers yeah same for me , browser is causing the problem – Vivek Solanki Feb 17 '16 at 19:31
  • Than I officialy want rehabilitation with upvotes. :D – Lanti Feb 17 '16 at 19:32
  • 3
    @Lanti You've already been told to look in the browser's error console (press ctrl-shift-K). The error message would have told you immediately what the problem is ("in strict mode code, functions may be declared only at top level or immediately within another function"). – JJJ Feb 17 '16 at 19:34
  • I checked in Chrome finally, it works and there's no console errors. Firefox giving the console error that you described. – Lanti Feb 17 '16 at 19:36