JavaScript Scroll horizontally instead of vertically in certain div with images?

I tried many solutions I found on the web. None worked. I'm trying to make a website scroll horizontally when I'm scrolling vertically. I tried to accomplish this via js - nothing. Then I read that I should be able to do this simply using css. Again - nothing. Here's the code as it is right now:

<style type="text/css">
#b {
    width:100%;
    height:100vh;
    white-space:nowrap;
    position:fixed;
    left:0;
    top:0;
    font-size:0;
    overflow-x:auto;
}
#b img {
    width:auto;
    height:100%;
}
</style>
</head>
<body>
    <div id="b">
        <img src="a.jpg"/>
        <img src="b.jpg"/>
    </div>
</body>

What is wrong? / What can I do?

Answer:1

The selected answer doesn't work in my browser (FireFox).

Here is a solution I've gotten to work pretty nicely.

HTML:

<body>
    <div id="b">
        <div class="img_holder">
            <img/>
            <img/>
            <img/>
            <img/>
            <img/>
            <img/>
            <img/>
            <img/>
        </div>
    </div>
</body>

When you scroll, you are essentially moving .img_holder left and right within #b. This method requires that .img_holder is as wide (or wider) than the sum of the images you'll be placing in it, as well as all the padding and margins. #b is the desired width to be viewed on the page.

CSS:

#b {
    width: 400px;
    border: 1px solid #111;
    padding: 0px;
    margin: 0px;
    overflow-x: scroll;
    overflow-y: hidden;
}
.img_holder {
    padding: 0px;
    margin: 0px;
    height: 100px;
    width: 910px;
}
img {
    height: 90px;
    width: 100px;
    padding: 5px;
    background: #123;
    display: inline-block;
}

JavaScript:

var scroller = {};
scroller.e = document.getElementById("b");

if (scroller.e.addEventListener) {
    scroller.e.addEventListener("mousewheel", MouseWheelHandler, false);
    scroller.e.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
} else scroller.e.attachEvent("onmousewheel", MouseWheelHandler);

function MouseWheelHandler(e) {

    // cross-browser wheel delta
    var e = window.event || e;
    var delta = - 20 * (Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))));

    var pst = $('#b').scrollLeft() + delta;

    if (pst < 0) {
        pst = 0;
    } else if (pst > $('.img_holder').width()) {
        pst = $('.img_holder').width();
    }

    $('#b').scrollLeft(pst);

    return false;
}

Make sure that you use conditions to stop from scrolling below zero and above the max width of the container (if conditions near bottom of code). Also, the delta value when scrolling is very small, and so to prevent the user from having to scroll a bunch I've multiplied it by 20. You can choose any value you'd like to adjust scrolling speed.

Also works nicely if you want to hide the horizontal Scroll Bar:

#b {
    overflow-x:hidden:
}

As shown here

Answer:2

Here is what you need:

Example: http://css-tricks.com/examples/HorzScrolling/

Code: http://css-tricks.com/snippets/jquery/horz-scroll-with-mouse-wheel/

$(function() {

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

      this.scrollLeft -= (delta * 30);

      event.preventDefault();

   });

})

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
<script type='text/javascript' src='/js/jquery.mousewheel.min.js'></script>
Answer:3

I'm writing a node wrapper to interact with an external api and am having a difficult time testing the asynchronous createJob method. Below is the test case code: api_key = "...

I'm writing a node wrapper to interact with an external api and am having a difficult time testing the asynchronous createJob method. Below is the test case code: api_key = "...

  1. mocha test asynchronous function
  2. mocha tests async
  3. mocha test asynchronous callback
  4. mocha test asynchronous code
  5. mocha test asynchronous calls
  6. mocha asynchronous tests
  7. mocha test async function
  8. mocha test async await
  9. mocha test async error
  10. mocha test async method
  11. mocha test async code
  12. mocha test async done
  13. mocha test async promise
  14. mocha test async await function
  15. mocha async tests timeout
  16. mocha async test example
  17. mocha async test order
  18. mocha async test fail
  19. mocha async test case
  20. mocha dynamic tests async

I'm writing a module that will create a dynamic menu on the fly. How to run a directive after adding new <li> with css class dropdown which is also added by ng-class. The code: angular.module('...

I'm writing a module that will create a dynamic menu on the fly. How to run a directive after adding new <li> with css class dropdown which is also added by ng-class. The code: angular.module('...

  1. dynamically adding dropdown using angularjs
  2. dynamically add dropdown in javascript
  3. dynamically add dropdown in jquery
  4. dynamically add dropdown using jquery
  5. dynamically add dropdown on button click javascript
  6. dynamically adding dropdown using jquery
  7. dynamically add dropdown items bootstrap
  8. dynamically adding dropdown using angular 4
  9. dynamically adding dropdown values javascript
  10. dynamically add dropdown in html
  11. dynamically add dropdown mvc
  12. bootstrap dynamically add dropdown
  13. javascript dynamically add dropdown options
  14. jquery dynamically add dropdown options
  15. angularjs dynamically add dropdown

i want to show a pop up with red color for some words i want to show variable "keyword name" in red color as well as " Todays Position" word . These both words should be in red color font. ...

i want to show a pop up with red color for some words i want to show variable "keyword name" in red color as well as " Todays Position" word . These both words should be in red color font. ...

  1. write alert in javascript
  2. write alert in php
  3. write alert in c#
  4. write alert in html
  5. write alert in jquery
  6. write alert in jsp
  7. write alert in code behind c#
  8. write in alert
  9. response.write with alert message

Here is the problem. The company I work for has a number of sites, and many of them use the same jQuery UI widgets, like datepicker. I would really like to have just one copy of each widget in an ...

Here is the problem. The company I work for has a number of sites, and many of them use the same jQuery UI widgets, like datepicker. I would really like to have just one copy of each widget in an ...

  1. link javascript file to html
  2. link javascript file to another javascript file
  3. link javascript file to html page
  4. link javascript file in asp.net
  5. link javascript file
  6. link javascript file to html5
  7. link javascript file php
  8. javascript link file download
  9. link external javascript file
  10. link external javascript file to html
  11. link external javascript file to html page
  12. javascript link js file
  13. django link javascript file
  14. dreamweaver link javascript file
  15. wordpress link javascript file
  16. link to javascript file in different folder
  17. link one javascript file to another
  18. w3schools link javascript file
  19. link to javascript file in head
  20. mvc link javascript file