jQuery Mousewheel Scroll Horizontally

前端 未结 4 2131
醉梦人生
醉梦人生 2020-12-14 21:38

I\'m currently developing a horizontally website that can enable my mouse scroll to scroll to the left and right...

My jQuery included sequence:

<         


        
4条回答
  •  鱼传尺愫
    2020-12-14 21:51

    After 3 days of searching for the answer, I finally found a solution without the Jquery plugins!

    // http://www.dte.web.id/2013/02/event-mouse-wheel.html
    (function() {
    function scrollHorizontally(e) {
        e = window.event || e;
        var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
        document.documentElement.scrollLeft -= (delta*40); // Multiplied by 40
        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);
    }
    })();
    

    If this code still won't work, the problem is not here, it's in your CSS.

提交回复
热议问题