0

Ok, using overflow:hidden; the page don't scroll and don't shows scrollbars.

What i would like to achieve is to disable body from scrolling but showing scrollbars, like to make scrollbars static/disabled without having to hide them.

ユーザーがモーダル内をスクロールできるようにする必要がありますが、同時にモーダルのスクロール中にページをスクロールしないでください。

ボディスクロールバーを非表示にせずに可能ですか?

どうもありがとう、私は困っていて、まともなコードを取得できません:/ 私のjsは次のようになります(ただし、これによりボディスクロールバーが非表示になり、とにかく表示したい:/)

function blockScrolling(){
  var scrollPosition = [
        self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
        self.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop
      ];
      var html = jQuery('html'); // it would make more sense to apply this to body, but IE7 won't have that
      html.data('scroll-position', scrollPosition);
      html.data('previous-overflow', html.css('overflow'));
      html.css('overflow', 'hidden');
      window.scrollTo(scrollPosition[0], scrollPosition[1]);

}
function unblockScrolling(){
   // un-lock scroll position
      var html = jQuery('html');
      var scrollPosition = html.data('scroll-position');
      html.css('overflow', html.data('previous-overflow'));
      window.scrollTo(scrollPosition[0], scrollPosition[1])
}
function modals(){
  $('.modal').hide();
  $('.modal').on('show shown',function(){
    blockScrolling();

  });
  $('.modal').on('hide hidden',function(){
    unblockScrolling();
  });
}
4

1 に答える 1

1

スクロール イベントを設定し、scrollTop を 0 または最後の既知の位置に戻し、完了したらスクロール イベントのバインドを解除するだけです。

function modals(){
  $('.modal').hide();
  $('.modal').on('show shown',function(){
    var top = $("body").scrollTop();
    (function(pos) {
        $(window).scroll(function(e) {
           $("body").scrollTop( 0 );
           //OR
           $("body").scrollTop( pos );
        });
    })(top);
  });
  $('.modal').on('hide hidden',function(){
    $(window).unbind("scroll");
  });
}
于 2013-07-27T08:37:30.607 に答える