[英]is it possible to open a popup with javascript and run once
Is it possible (and how?) to make it only run, once, that the function does not continue to be executed once closed 是否有可能(以及如何执行)使其仅运行一次,一旦关闭该函数就不会继续执行
;(function($){
$(window).scroll(function(){
if($(document).scrollTop()>=$(document).height()/5)
$("#xpopup").show("slow");else $("xpopup").hide("slow");
});
})(jQuery);
function closePopup(){
jQuery('#xpopup').hide('slow');
}
HTML 的HTML
<div id="xpopup" style="display: none;">
<a style="position:absolute;top:14px;right:10px;color:#999;font-size:20px;font-weight:bold;" href="javascript:void(0);" onclick="return closePopup();">
<img src="close.png" width="18" height="18"/>
</a>
popup content
</div>
After displaying the popup, remove the scroll handler with .off()
: 显示弹出窗口后,使用
.off()
删除滚动处理程序:
if($(document).scrollTop() >= $(document).height() / 5) {
$("#xpopup").show("slow");
$(window).off('scroll');
}
Note that this will remove all scroll handlers from the window. 请注意,这将从窗口中删除所有滚动处理程序。 If you only have one, that's not a problem.
如果只有一个,那不是问题。 If you have more, store a reference to this scroll handler and pass it to
.off()
: 如果还有更多内容,请存储对此滚动处理程序的引用,并将其传递给
.off()
:
$(window).off('scroll', myScrollPopupHandler);
I would use singleton 我会用单身
function singleton(){
let instance;
let createInstance = function(){
return 'I exist'; //replace with actual object (e.g. your instance that would cause the alert)
}
return {
createAlert : function(){
if(!instance){
instance = createInstance();
alert(instance);
}
}
}
}();
singleton.createAlert(); //this would go into your desired event
The reason why this might be a good way to do it is that you would likely replace alert
with some other action. 之所以这样做是个好方法,是因为您可能会用其他一些动作来代替
alert
。 In that case all you need to do is to replace instance with whatever object that may cause the invokation (eg bootstrap alert). 在这种情况下,您要做的就是用可能引起调用的任何对象替换实例(例如,引导警报)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.