How to disable scrolling temporarily?

前端 未结 30 3294
萌比男神i
萌比男神i 2020-11-21 05:16

I\'m using the scrollTo jQuery plugin and would like to know if it is somehow possible to temporarily disable scrolling on the window element through Javascript? The reason

30条回答
  •  萌比男神i
    2020-11-21 05:25

    No, I wouldn't go with event handling because:

    • not all events are guaranteed to reach body,

    • selecting text and moving downwards actually scrolls the document,

    • if at the phase of event detaching sth goes wrong you are doomed.

    I've bitten by this by making a copy-paste action with a hidden textarea and guess what, the page scroll whenever I make copy because internally I have to select the textarea before I call document.execCommand('copy').

    Anyway that's the way I go, notice the setTimeout():

    document.body.setAttribute('style','overflow:hidden;');
    // do your thing...
    setTimeout(function(){document.body.setAttribute('style','overflow:visible;');}, 500);
    

    A momentum flashing exists as the scrollbars disappear momentarily but it's acceptable I thing.

提交回复
热议问题