用鼠标滚轮在div中水平滚动

Dan*_*uła 20 html scroll

如何使用鼠标滚动在na div中水平滚动,或用jquery拖动?


我试过拖延,在我的代码中,它没用.

现在我有一个水平滚动条.是否有可能用鼠标滚轮在我的div中滚动内容?

Tau*_*man 48

尝试使用鼠标滚轮进行水平滚动.这是纯JavaScript:

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

这里有一个演示,但document.bodywindow为针对性的元素:http://rawgit.com/tovic/dte-project/master/full-page-horizo​​ntal-scrolling.html

  • 谢谢,这正是我想要的.最后一个答案不建议使用JQuery插件.顺便说一句:如果其他人遇到任何问题,请尝试在所有html之后添加它.示例 - http://jsbin.com/usabigE/2/edit (3认同)
  • 如果要在元素一直向左或向右滚动后垂直滚动,可以用`if语句包装`e.preventDefault();`:if((delta> 0 && document.getElementById( 'yourDiv').scrollLeft> 0)||(delta <0 && document.getElementById('yourDiv').offsetWidth + document.getElementById('yourDiv').scrollLeft <el.scrollWidth))`(在Chrome和Firefox上测试过) ) (2认同)

Izh*_*zmi 12

我已经从@ Anonymous-Lettuce的答案中重写了代码.跳过元素的宽度重新计算,因为它是不必要的,有时是不合需要的.这也提供了额外的功能,通过scroll-amountpx该插件.

像这样使用它:

$(document).ready(function(){
    $('#your_div').hScroll(100); // You can pass (optionally) scrolling amount
});
Run Code Online (Sandbox Code Playgroud)

这是升级后的插件jquery.hscroll.js:

jQuery(function ($) {
    $.fn.hScroll = function (amount) {
        amount = amount || 120;
        $(this).bind("DOMMouseScroll mousewheel", function (event) {
            var oEvent = event.originalEvent, 
                direction = oEvent.detail ? oEvent.detail * -amount : oEvent.wheelDelta, 
                position = $(this).scrollLeft();
            position += direction > 0 ? -amount : amount;
            $(this).scrollLeft(position);
            event.preventDefault();
        })
    };
});
Run Code Online (Sandbox Code Playgroud)

这是相同的缩小版本jquery.hscroll.min.js:

jQuery(function(e){e.fn.hScroll=function(l){l=l||120,e(this).bind("DOMMouseScroll mousewheel",function(t){var i=t.originalEvent,n=i.detail?i.detail*-l:i.wheelDelta,o=e(this).scrollLeft();o+=n>0?-l:l,e(this).scrollLeft(o),t.preventDefault()})}});
Run Code Online (Sandbox Code Playgroud)

这是JSFiddle


Iva*_*vić 6

几天前写了这个插件。

 $.fn.hScroll = function( options )
 {
   function scroll( obj, e )
   {
     var evt = e.originalEvent;
     var direction = evt.detail ? evt.detail * (-120) : evt.wheelDelta;

     if( direction > 0)
     {
        direction =  $(obj).scrollLeft() - 120;
     }
     else
     {
        direction = $(obj).scrollLeft() + 120;
     }

     $(obj).scrollLeft( direction );

     e.preventDefault();
   }

   $(this).width( $(this).find('div').width() );

   $(this).bind('DOMMouseScroll mousewheel', function( e )
   {
    scroll( this, e );
   });
}
Run Code Online (Sandbox Code Playgroud)

试试看

$(document).ready(function(){
     $('#yourDiv').hScroll();
});
Run Code Online (Sandbox Code Playgroud)

div 的子元素的宽度应该比父元素宽。

像 4000 像素什么的。

 <div id="yourDiv">
       <div style="width: 4000px;"></div>
 </div>
Run Code Online (Sandbox Code Playgroud)


Mic*_*umo 5

我想对@Taufik 给出的答案稍加改进。

es2015模块的上下文中:

const el = document.querySelector('#scroller');

function scrollHorizontally(e) {
  e = window.event || e;
  e.preventDefault();
  el.scrollLeft -= (e.wheelDelta || -e.detail);
}

function init() {
  if (!el) {
    return;
  }

  if (el.addEventListener) {
    el.addEventListener('mousewheel', scrollHorizontally, false);
    el.addEventListener('DOMMouseScroll', scrollHorizontally, false);
  } else {
    el.attachEvent('onmousewheel', scrollHorizontally);
  }
}

export default init;
Run Code Online (Sandbox Code Playgroud)

主要区别在于: el.scrollLeft -= (e.wheelDelta || -e.detail);

在使用e.wheelData直接在滚动补偿装置我们可以有惯性,这样滚动可以逐步放缓。我发现这对我很有效。

像这样在你的代码中使用(假设上面的代码在一个名为scroller.js的文件中):

import scroller from './scroller.js';
scroller();
Run Code Online (Sandbox Code Playgroud)