jQuery or Javascript - how to disable window scroll without overflow:hidden;

后端 未结 8 1868
耶瑟儿~
耶瑟儿~ 2020-12-01 01:16

Hi is it possible to disable window scrolling without using overflow:hidden; when i\'m hover an element?

i tryed :

$(\'.chat-content\').         


        
相关标签:
8条回答
  • 2020-12-01 01:53

    If you want to scroll the element you're over and prevent the window to scroll, here's a really useful function :

    $('.Scrollable').on('DOMMouseScroll mousewheel', function(ev) {
        var $this = $(this),
            scrollTop = this.scrollTop,
            scrollHeight = this.scrollHeight,
            height = $this.height(),
            delta = (ev.type == 'DOMMouseScroll' ?
                ev.originalEvent.detail * -40 :
                ev.originalEvent.wheelDelta),
            up = delta > 0;
    
        var prevent = function() {
            ev.stopPropagation();
            ev.preventDefault();
            ev.returnValue = false;
            return false;
        }
    
        if (!up && -delta > scrollHeight - height - scrollTop) {
            // Scrolling down, but this will take us past the bottom.
            $this.scrollTop(scrollHeight);
    
            return prevent();
        } else if (up && delta > scrollTop) {
            // Scrolling up, but this will take us past the top.
            $this.scrollTop(0);
            return prevent();
        }
    });
    

    Apply the class "Scrollable" to your element and that's it!

    0 讨论(0)
  • 2020-12-01 01:54

    Try to handler 'mousewheel' event on all nodes except one

    $('body').on({
        'mousewheel': function(e) {
            if (e.target.id == 'el') return;
            e.preventDefault();
            e.stopPropagation();
        }
    })
    

    Demo: http://jsfiddle.net/DHz77/1/

    0 讨论(0)
提交回复
热议问题