简体   繁体   English

如何在页面加载后显示图像弹出窗口

[英]how to show an Image PopUp after Page Load

This code is used to create popups on websites.此代码用于在网站上创建弹出窗口。 After clicking on the close button once, the popup box goes away but after 3 seconds the fire comes again.单击一次关闭按钮后,弹出框消失,但 3 秒后又起火。 I only want this 1 time, not over and over again我只想要这一次,而不是一遍又一遍

            $(document).ready(function(){
            setTimeout(function(){
                $('#popUp').css('display','block'); }, 3000);
            }); 

        $('.close-popup').click(function(){
            $('#popUp').css('display','none');
        });
<div id="popUpmain">
        <form action="">
            <div id="popUp" style="display:none">
                <h1 class="popUp-heading">Sign Up</h1>
                <input type="text" class="pop-up-name" placeholder="Your Name">
                <input type="text" class="pop-up-email" placeholder="Email">
                <input type="text" class="pop-up-number" placeholder="Number">
                <select name="Select Product" id="" class="popup-select">
                    <option value="Select Product active">Select Product</option>
                    <option value="Mattress">Mattress</option>
                    <option value="Select Product active">Comforter</option>
                    <option value="Select Product active">pillow</option>
                </select> 
                <input type="submit" class="popup-btn close-popup">
                <button class="close-btn close-popup">Not Now</button>
            </div>
        </form>
    </div>

try changing your js to this:尝试将您的 js 更改为:

basically, you set the timeout function to a variable and then when you close the popup you clear the timeout基本上,您将超时函数设置为变量,然后在关闭弹出窗口时清除超时

 $(document).ready(function(){
      var popup =   setTimeout(function(){
            $('#popUp').css('display','block'); }, 3000);
        }); 

    $('.close-popup').click(function(){
        $('#popUp').css('display','none');
          clearTimeout(popup);
    });

 var cnt = 0; $(document).ready(function(){ cnt++; console.log('alert'+ cnt) var popup = setTimeout(function(){ $('#popUp').css('display','block'); }, 3000); $('.close-popup').click(function(){ $('#popUp').css('display','none'); clearTimeout(popup); }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="popUpmain"> <form action=""> <div id="popUp" style="display:none"> <h1 class="popUp-heading">Sign Up</h1> <input type="text" class="pop-up-name" placeholder="Your Name"> <input type="text" class="pop-up-email" placeholder="Email"> <input type="text" class="pop-up-number" placeholder="Number"> <select name="Select Product" id="" class="popup-select"> <option value="Select Product active">Select Product</option> <option value="Mattress">Mattress</option> <option value="Select Product active">Comforter</option> <option value="Select Product active">pillow</option> </select> <input type="submit" class="popup-btn close-popup"> <button type ='button' class="close-btn close-popup">Not Now</button> </div> </form> </div>

It's because of your 'not now' button.这是因为你的“不是现在”按钮。

<div id="popUpmain">
    <form action="">
        <div id="popUp" style="display:none">
            <h1 class="popUp-heading">Sign Up</h1>
            <input type="text" class="pop-up-name" placeholder="Your Name">
            <input type="text" class="pop-up-email" placeholder="Email">
            <input type="text" class="pop-up-number" placeholder="Number">
            <select name="Select Product" id="" class="popup-select">
                <option value="Select Product active">Select Product</option>
                <option value="Mattress">Mattress</option>
                <option value="Select Product active">Comforter</option>
                <option value="Select Product active">pillow</option>
            </select> 
            <input type="submit" class="popup-btn close-popup">
            <!--<start of problem> -->
            <button class="close-btn close-popup">Not Now</button>
            <!--<end of problem> -->
        </div>
    </form>
</div>

If you change the above:如果您更改上述内容:

<button class="close-btn close-popup">Not Now</button>

To:至:

<button type="button" class="close-btn close-popup">Not Now</button>

It will prevent the button from submitting the form, avoiding a post-back .), preventing the document reloading , re-triggering the $(document).ready(function() call.它将阻止按钮提交表单,避免回发。),阻止文档重新加载,重新触发$(document).ready(function()调用。

Button types:按钮类型:

https://www.w3schools.com/tags/att_button_type.asp https://www.w3schools.com/tags/att_button_type.asp

Your code as made at first follow the right logic!您最初制作的代码遵循正确的逻辑!

There is something that you are missing!有一些东西你错过了!

This something is that the button is within a form !这就是按钮在表单中 And by default will submit the form !并且默认会提交表单

Which means at submit, it will refresh the page !这意味着在提交时,它将刷新页面 And so the page load again !所以页面再次加载 And ready event will get executed again! ready事件将再次执行! Which will fire again!哪个会再次火起来! And it gonna be an infinit loop if you try the same thing again !如果你再次尝试同样的事情,它将会是一个无限循环 By clicking the button again!再次点击按钮!

You can prevent that using e.preventDefault() :您可以使用e.preventDefault()防止这种情况:

$('.close-popup').click(function(e){
    e.preventDefault(); // <<<< here
    $('#popUp').css('display','none');
});

https://api.jquery.com/event.preventdefault/ https://api.jquery.com/event.preventdefault/

And here a playground for your code!这里是您代码的游乐场! Even though it's straight forward!尽管它是直截了当的!

https://jsfiddle.net/mkpzrg3j/1/ https://jsfiddle.net/mkpzrg3j/1/

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

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