【问题标题】:Page scrolls down when I open modal window当我打开模式窗口时页面向下滚动
【发布时间】:2017-05-09 13:59:10
【问题描述】:

我有一个长页面,上面有几个打开模式窗口的锚标记。 (a href #)

当我点击它时,模式窗口打开,背景主页向下滚动一点。当我关闭模式窗口时,它会向上滚动一点,但不会滚动到顶部。这会导致糟糕的用户体验。

在没有滚动条的页面上打开模式很好。

我研究了各种解决方案,但没有一个对我有用。

.modal {
    display: none; /* Hidden by default */
    position: absolute; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
	    overflow-y: auto;
    width: 100%; /* Full width */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
	font-family: 'Georgia', monospace, serif;
}





/* Modal Content */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    font-family: 'Georgia', monospace, serif;
    border: 1px solid #888;
    width: 400px;

    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);

}
.modal-header {
    padding: 2px 16px;
    background-color: black;
    color: white;
    font-family: 'Georgia', monospace, serif;
}


 /* Behaviour on legacy browsers */
.target:target + .modal {
    display: block;
}

/* Fallback for IE8 */
.modal.is-expanded {
   display: block;
}
.modal.is-expanded > .content {
  top: 50%;
  margin-top: -45px;
}



/* Behavior on modern browsers */
:root .modal {
  display: block;
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  transition: transform 0.3s cubic-bezier(0.5, -0.5, 0.5, 1.5);
  transform-origin: center center;
  transform: scale(0, 0);
}
:root .modal > .content {
  box-shadow: 0 5px 20px rgba(0,0,0,0.5);
}
:root .target:target + .modal {
  transform: scale(1, 1);
}

/* The Close Button */

.close-btn:hover,
.close-btn:focus {
    cursor: pointer;
}
 .modal > .modal-content .modal-header .close-btn {
   position: absolute;
    top: 18px;
    right: 18px;
    width: 15px;
    height: 15px;
    color: white;
    font-size: 18px;
    text-decoration: none;
 }

 .modal-body {padding: 2px 16px;}
 
 .modal-open {
  -moz-appearance: menuimage;
}

.modal-open::-webkit-scrollbar {
   width: 0 !important;
}
<span id="start" class="target"><!-- Hidden anchor to close all modals --></span>
    <span id="userinfo" class="target"><!-- Hidden anchor to open adjesting modal container--></span>
	
    <div class="modal">
      <div class="modal-content">
<div class="modal-header">

      <h2>User Info</h2>
	  <a class="close-btn" href="#start" ><i class="fa fa-times"></i></a>

		    </div>
			    <div class="modal-body">
content....
    </div>
      </div>
    </div>
	<a href="#userinfo"><i class="fa fa-user fa-lg"></i></a>

任何帮助将不胜感激。

【问题讨论】:

    标签: html css modal-dialog bootstrap-modal


    【解决方案1】:

    好的,我找到了解决方案。它不流畅,因为您可以看到滚动条如何上下移动几毫秒,但背景页面至少保持在顶部

    这对我有用:

    <script>
    $(document).ready(function () {
    
    $(window).on('hashchange', function (event) {
    window.scrollTo(0, 0);
    });
    
    });
    </script>

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 2014-02-07
      • 2012-03-07
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多