我已经实现了一个模式,但我不希望页面在模式关闭时滚动到顶部。它会自动执行此操作。我缺少管理下面代码的代码吗?感谢您的帮助!
HTML
<a href="#target-content" id="button">Open A Modal</a>
<div id="target-content">
<a href="#" class="close"></a>
<div id="target-inner">
<h2>Modal Heading</h2>
<p>Modal Content</p>
</div>
</div>
CSS
#target-content {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
pointer-events: none;
opacity: 0;
-webkit-transition: opacity 200ms;
transition: opacity 200ms;
}
#target-content:target {
pointer-events: all;
opacity: 1;
}
#target-content #target-inner {
position: absolute;
display: block;
padding: 48px;
line-height: 1.8;
width: 70%;
top: 50%;
left: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
box-shadow: 0px 12px 24px rgba(0, 0, 0, 0.2);
background: white;
color: #34495E;
}
#target-content #target-inner h2 { margin-top: 0; }
#target-content #target-inner code { font-weight: bold; }
#target-content a.close {
content: "";
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: #34495E;
opacity: 0.5;
-webkit-transition: opacity 200ms;
transition: opacity 200ms;
}
#target-content a.close:hover { opacity: 0.4; }
最佳答案
href
属性中的 #
会将您的页面发送到顶部。
<a href="#" class="close"></a> <!-- moves scroll position to top of page -->
一种解决方案是将 #
替换为您希望页面移动或停留的特定 ID。
试试这个:
<a href="#target-content" class="close"></a>
在此处了解有关超链接和主题标签的更多信息:What is href="#" and why is it used?
关于html - 模式关闭时页面滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32787962/