首页 文章

打开弹出窗口时防止正文滚动

提问于
浏览
0

首先我知道同样的问题已被问过好几次,但我不能让它正常工作 . 在网站上我实现我打开一个包含一些信息的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 回答

  • 1

    正文溢出工作正常但你也给了正在创建滚动条的HTML标签 overflow-x: scroll .

    你必须删除你的CSS中的HTML样式

    html{
       overflow-x: hidden;
    }
    

    然后身体溢出将工作正常 .

    http://prntscr.com/gzs7xs

相关问题