简体   繁体   English

JavaScript模态弹出窗口

[英]Javascript Modal Popup

I'm trying to create a simple modal popup. 我正在尝试创建一个简单的模式弹出窗口。 I have the CSS done, and the Javascript mostly done. 我已经完成了CSS,大部分都完成了JavaScript。 I'm at the point where I click a button at the button of the page and the popup appears, but it appears at the top of the page. 我要单击页面按钮上的一个按钮,然后弹出窗口出现,但它出现在页面顶部。 It appears excatly how it should if you were scrolled all the way to the top of the page. 如果您一直滚动到页面顶部,它会显得很奇特。

I however want the modal popup box to appear center based upon the current scroll position. 但是,我希望模式弹出框基于当前滚动位置显示在中心。 I also want it to stay centered when I scroll. 我也希望它在滚动时保持居中。 As of right now it doesn't float at the center of the screen. 截至目前,它还没有漂浮在屏幕中央。

What javascript property do I need to use to adjust the position of the popup div as I scroll. 滚动时,我需要使用什么javascript属性来调整弹出div的位置。

I've tried, but to no avail: 我已经尝试过,但无济于事:

function showModal(id)
{
   window.onscroll = function () { document.getElementById(divID).style.top =
                                               window.pageYOffset || 
                                               document.body.scrollTop || 
                                               document.documentElement.scrollTop; };
   document.getElementById(id).style.display = "block";
   document.getElementById(id).style.top =
                 window.pageYOffset || 
                 document.body.scrollTop || 
                 document.documentElement.scrollTop; };
}

I'm using straight vanilla javascript, no jQuery. 我使用的是纯正的Javascript,没有jQuery。

TL;DR: I've created a modal popup but it doesn't stay centered when I scroll away from the very top of the page. TL; DR:我创建了一个模式弹出窗口,但是当我滚动离开页面的顶部时,它不会居中。

try position: fixed for your css, it won't scroll with this, also check display HTML page after loading complete 尝试position: fixed针对您的CSS进行了position: fixed ,它不会与此滚动, 在加载完成后还会检查显示HTML页面

example: 例:

<style>
#floater    {float:left; height:50%; margin-bottom:-1px;}
#top        {position: fixed; top:0; left: 0; width: 100%; height: 100%; background: #fff}
#content    {clear:both; height:540px; position:relative; height: 100%; width: 100%;}
</style>
<div id="top">
    <div id="floater"></div>
    <div id="content">
        <div style="border: 1px solid #DDD; padding: 10px; background: #BBB; width: 300px; margin: 0 auto">Popup Contents Here</div>
    </div>
</top>
  • #top element makes hides background, you can make it transparent but still unclickable. #top元素可隐藏背景,您可以使其透明但仍不可点击。
  • #floater and #content are for vertical centering . #floater#content用于垂直居中

Modal Popup, OnClose popup event, and more! 模态弹出,OnClose弹出事件等等!

https://github.com/reduardo7/xpopup https://github.com/reduardo7/xpopup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM