弹出窗口打开时阻止身体滚动

时间:2017-10-20 12:53:02

标签: javascript html css scroll overflow

首先我知道这几个问题已被问过几次,但我无法正常工作。在网站上我实现我打开一个包含一些信息的popUp然后阻止身体的其余部分滚动。我想允许滚动弹出窗口而不是菜单。还要避免在主体上使用“position:fixed”,因为它会将页面移动到顶部。为了防止身体滚动,我错过了什么?身体已经拥有“溢出:隐藏”的属性。这是我开发的代码:

http://www.produccionsf2f.com/equipo/

HTML:

覆盖身体的叠加层:

<div id="scid-overlay" class="x-section scid-transition-eio overlay-open" style="margin: 0px;padding: 0px; background-color: hsla(0, 0%, 1%, 0.69);"><div class="x-container max width" style="margin: 0px auto;padding: 0px;height: 0;">

实际弹出窗口:

<div id="scid-overlay" class="x-section scid-transition-eio overlay-open" style="margin: 0px;padding: 0px; background-color: hsla(0, 0%, 1%, 0.69);">

JS:

jQuery(function($){
      var overlay = $('#scid-overlay');
    var visiblePanel;

    $(".scid-hook-link").click(function(event){
        openPanel(event);
    });

     $('.scid-close-button').click(function(event) {
         closePanel(event);
     });

     overlay.click(function(event) {
         closePanel(event);
     });

    function openPanel(event){
      event.preventDefault();

      var id = $(event.target).attr('id').replace('hook', 'popup');
      $(event.target).closest('figure').addClass(' scid-popup');
      document.querySelector('#' + id).className += (' scid-panel-open');
      overlay.removeClass('overlay-close');
      overlay.addClass('overlay-open');
      sizePanel(id);

      window.location.hash = "#" + id;
      visiblePanel = document.querySelector('.scid-panel-open');
    };

    function closePanel(event){
      if(event){
        event.preventDefault();
      }

      if(visiblePanel) {
        var id = visiblePanel.id;
        console.log(id)
        console.log(visiblePanel);
        document.querySelector('.scid-popup').classList.remove('scid-popup');
        visiblePanel.classList.remove('scid-panel-open');
        overlay.removeClass('overlay-open');
        overlay.addClass('overlay-close');
        sizePanel(id);        
      }

    }

    function sizePanel(id) {
      var panel = document.querySelector( '#' + id);
      if (panel.offsetHeight == 0) {
          panel.style.height = panel.scrollHeight + 'px';
      } else {
          panel.style.height = 0 +'px';
          window.location.hash = "";
            visiblePanel = null;
      }
    };

    window.onhashchange = function() {
    if(visiblePanel && window.location.hash != '#' + visiblePanel.id){
      closePanel();
    }
  }
});

CSS:

.overlay-close {
    height: 0;
    opacity: 0;
}
.overlay-open {
    height: 100%;
    opacity: 1
    overflow: scroll;
    overflow-y: auto;
    visibility: visible;
    z-index: 69;
}

1 个答案:

答案 0 :(得分:1)

正文溢出工作正常,但您还将overflow-x: scroll提供给正在创建滚动条的HTML标记。

你必须删除你的CSS中的html风格

html{
   overflow-x: hidden;
}

然后身体上的溢出将正常工作。

http://prntscr.com/gzs7xs