鼠标按下时锁定scollbar,鼠标按下时解锁

时间:2015-11-26 06:16:28

标签: javascript jquery

iam尝试在鼠标按下事件时锁定当前位置的滚动条,并且还需要在鼠标按下事件时解锁

$('#table td').on('mousedown', function(){
$('#container').scrollLeft(0);
});

1 个答案:

答案 0 :(得分:0)

   var keys = {37: 1, 38: 1, 39: 1, 40: 1};

function preventDefault(e) {
  e = e || window.event;
  if (e.preventDefault)
      e.preventDefault();
  e.returnValue = false;  
}

function preventDefaultForScrollKeys(e) {
    if (keys[e.keyCode]) {
        preventDefault(e);
        return false;
    }
}

function disableScroll() {
  if (window.addEventListener) // older FF
      window.addEventListener('DOMMouseScroll', preventDefault, false);
  window.onwheel = preventDefault; // modern standard
  window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
  window.ontouchmove  = preventDefault; // mobile
  document.onkeydown  = preventDefaultForScrollKeys;
}

function enableScroll() {
    if (window.removeEventListener)
        window.removeEventListener('DOMMouseScroll', preventDefault, false);
    window.onmousewheel = document.onmousewheel = null; 
    window.onwheel = null; 
    window.ontouchmove = null;  
    document.onkeydown = null;  
}

$(document).ready( function(){
$('#table td').on('mousedown', function(){

        disableScroll();

});

$('#table td').on('mouseup', function(){

    enableScroll();

});
});

检查this solution

基于this question

希望这会有所帮助