简体   繁体   English

添加关闭按钮以自动弹出窗口

[英]Adding close button to auto pop up window

I'm trying to add an auto pop up box to the home page of a site. 我正在尝试将自动弹出框添加到网站的主页。 I have used the code from a previous answer on this site and as is follows below. 我已经使用了该站点上一个答案中的代码,如下所示。 The pop up works just fine but can you also add a close button/link to it? 弹出窗口可以正常工作,但是您还可以添加一个关闭按钮/链接吗? Thanks in advance for the help. 先谢谢您的帮助。

Javascript: Javascript:

<script type="text/javascript">
function show_popup()
{
  document.getElementById('popup').style.display = 'block'; 
}

window.onload = show_popup;
</script>

The CSS: CSS:

#popup
{
position:absolute;
z-index:99;
display:block;
top:200px;
left:50%;
width:567px;
height:420px; 
margin-left:-250px; 
}

and the call: 并致电:

<div id="popup">pop up window</div>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>‌    
<div id="popup">
<div id='popup-close'>Close</div>
pop up window
</div>

And your jQuery, 还有你的jQuery

$(document).ready(function()
{
    $('#popup').show('fast'); // This will show the Pop Up in the Page Load
    $('#popup-close').click(function(e) // You are clicking the close button
    {
      $('#popup').hide('fast'); // Now the pop up is hided.
    });
});

Add another div into the popup that when clicked activates this document.getElementById('popup').style.display = 'none'; popup中添加另一个div,单击该div即可激活此document.getElementById('popup').style.display = 'none'; . You can relatively position the div to the top, right simple enough. 您可以relatively简单地将div relatively定位到top, right

Here's a simple way using a 2nd positioned absolute div within your popup div. 这是在弹出式div中使用第二个绝对div的简单方法。

Lot's of ways to improve on it and add more stuff. 有很多改进和添加更多内容的方法。

 function show_popup() { document.getElementById('popup').style.display = 'block'; } window.onload = show_popup; $('.popup-closer').click(function() { $('#popup').hide(); }); 
 #popup { position: absolute; z-index: 99; display: block; top: 50%; left: 50%; /* if you want to center it do this.. */ transform: translate(-50%, -50%); /* ..and this */ width:150px; height:225px; color: white; background-color: black; } .popup-closer { position:absolute; top: 5px; right: 5px; display: flex; justify-content: center; align-items: center; background-color: red; border-radius: 20px; padding: 5px; cursor: pointer; } .popup-closer:hover { background-color: white; } html, body { margin: 0; padding: 0; } 
 <link href="https://netdna.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="popup"> pop up window <div class="popup-closer"><i class="fa fa-close"></i></div> </div> 

If you want it only shown once per user per session or day look into session and local storage. 如果您希望每个会话每天仅向每个用户显示一次,请查看会话和本地存储。 The popup can first check their session storage if they've been served the notice already this session/this day/etc and if so prevent them being shown a 2nd+ time. 弹出窗口可以首先检查其会话存储空间,如果在此会话/这一天/这一天等已经向他们发送了通知,并且如果这样做的话,则阻止他们再次显示。 Same deal if you have the code sitewide and you don't want it popping up every page load. 如果您在整个网站上都拥有代码,并且不希望每次加载页面时都弹出该代码,则可以进行同样的处理。

Also, may want to make #popup position: fixed; 另外,可能要设置#popup position: fixed; so when user scrolls page the popup stays with them until they acknowledge and close it. 因此,当用户滚动页面时,弹出窗口将一直伴随着它们,直到他们确认并关闭它为止。

fiddle 小提琴

https://jsfiddle.net/Hastig/au3xm1oy/ https://jsfiddle.net/Hastig/au3xm1oy/

and additional fiddle for more ideas 和更多的小提琴以获取更多想法

https://jsfiddle.net/Hastig/au3xm1oy/2/ https://jsfiddle.net/Hastig/au3xm1oy/2/

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

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