简体   繁体   English

将叠加层添加到弹出窗口并添加输入类型字段以退出表单

[英]Adding Overlay to a popup and adding input type field to exit the form

I have been working on creating a popup with some different functionality. 我一直在努力创建具有某些不同功能的弹出窗口。 It is being used as a pop for users to subscribe to my website. 它被用作弹出窗口供用户订阅我的网站。 I have added in a form connected my DB, which is all working great. 我以连接我的数据库的形式添加了表格,一切正常。 I am now trying to add some custom features. 我现在正在尝试添加一些自定义功能。

  1. I am trying to add some overlay (faded) to my background of the popup so my homepage is faded out in the background of the popup 我正在尝试向弹出窗口的背景添加一些叠加(淡入淡出),以便在弹出窗口的背景中淡出我的主页

  2. I need to add in a input type field (if possible) to close my form. 我需要添加一个输入类型字段(如果可能)以关闭表单。 * I have added in a image with a X, if there is not input type option I will use this to exit the form *我在图像中添加了X,如果没有输入类型选项,我将使用它退出表单

Here is my code: 这是我的代码:

JavaScript: JavaScript:

 <script>
 function PopUp(hideOrshow) {
   if (hideOrshow == 'hide') {
        document.getElementById('ac-wrapper').style.display = "none";       
    }
    else if(localStorage.getItem("popupWasShown") == null) {
        localStorage.setItem("popupWasShown",1);
        document.getElementById('ac-wrapper').removeAttribute('style');

    }
}

window.onload = function () {
    setTimeout(function () {
        PopUp('show');
    }, 0);
}


function hideNow(e) {
    if (e.target.id == 'ac-wrapper') document.getElementById('ac-wrapper').style.display = 'none';
}

HTML: HTML:

<div id="ac-wrapper" style='display:none' onClick="hideNow(event)">
<div id="popup">
<img alt="#" class="close-image" src="Assets/images/Deep_Close.png" />

<form name="Mail_list" action="save.php" method="post">
<br/>
<h4>Subscription</h4>
<br/>
    <div class="form-group">
      <label for="first_name">First Name: </label>
      <input type="text" name="first_name" id="first_name" size="25" placeholder="First Name"  autofocus required />
    </div>
    <div class="form-group">
      <label for="last_name">Last Name: </label>
      <input type="text" name="last_name" id="last_name" size="25" placeholder="Last Name"  required />
    </div>
     <div class="form-group">
      <label for="email">User Email: </label>
      <input type="text" name="email" id="email" size="25" placeholder="Email"  required />
    </div>
    <br/><br/>
    <input type="submit" value="Submit Form">
    <input type="reset" value="Reset Form">             
</form> 

CSS: CSS:

#ac-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1001;   
}

#popup{
position:absolute;
display:hidden;
top:300px;
left:50%;
width:500px;
height:auto;
margin-left:-250px;
background-color:white;
z-index:6;
padding:20px;
border:solid 5px #333333;
border-radius:5px;
}

#overlay-back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #000;
opacity: 0.6;
filter: alpha(opacity=60);
z-index: 5;
display: none
}

.close-image{
display: block;
float:right;
position:relative;
top:-15px;
right: -15px;
height: 20px;
}

As you can see I have added in the 'Overlay' CSS already, I just am not sure how to implement this in my popup to make the functionality work. 如您所见,我已经在“ Overlay” CSS中添加了代码,只是不确定如何在弹出窗口中实现此功能以使其正常工作。

Also, the 'close-image' CSS will be used to close the form if their is no input type available to close the form (which I haven't been able to find), How do I implement this is my JavaScript? 另外,如果没有可用的输入类型来关闭表单(我无法找到),则将使用“关闭图像” CSS来关闭表单(我找不到)。如何实现这是我的JavaScript?

  1. For the overlay, give it position: fixed and place it outside of your other elements. 对于叠加层,请为其定位:固定并放置在其他元素之外。 You want it to be 'fixed' to the body, so with 100% height and width it'll cover the full page, effectively. 您希望将其“固定”在主体上,因此高度和宽度为100%时,它将有效地覆盖整个页面。

Just build your overlay/ form to look how you want - with all elements visible, then handle the show/ hide afterwards in your JavaScript. 只需构建您的overlay /表单以查看您想要的样子-在所有元素都可见的情况下,然后再在JavaScript中处理show / hide。

  1. To close the form, just have a click handler to hide both the form and the overlay when the given element is clicked. 要关闭表单,只需单击事件处理程序即可在单击给定元素时隐藏表单和覆盖图。 See a brief example: 看一个简单的例子:

     var closeImage = document.getElementById('close-image'); var overlay = document.getElementById('overlay-back'); var formWrapper = document.getElementById('ac-form-wrapper'); // Hide the elements & do other stuff you may want.. function hide() { overlay.style.display = "none"; formWrapper.style.display = "none"; } // Call this whenever you want to show stuff. // In this case, it would go inside the setTimeout // function also. function show() { overlay.style.display = "block"; formWrapper.style.display = "block"; } // Click handler for the image element. closeImage.addEventListener("click", function() { hide(); }, false); 

It may be better to change the close-image element to be an ID instead, it's easier to target in native JavaScript, and the element probably doesn't need to be a class anyway. 相反,最好将close-image元素更改为ID,在本机JavaScript中更容易定位,并且该元素可能不一定要成为类。

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

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