简体   繁体   中英

How to scroll horizontal Only div on mouse wheel

HTML markup:

<div class="big">
  long width text
</div>

CSS rule:

.big{
    height:200px;
    background: red;
}

How to scroll horizontal Only BIG div on mouse wheel http://jsfiddle.net/cxD55/

设置overflow-x CSS样式,但将overflow-y样式设置为可见。

Here is the solution:

Html

<div class="big">
long text here
</div>

CSS

.big {
height: 200px;
overflow: hidden;
width:6000px;
}

JS

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

Change width according to your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM