![](/img/trans.png)
[英]is it possible to open a popup with javascript and then detect when the user closes it?
[英]is it possible to open a popup with javascript and run once
是否有可能(以及如何执行)使其仅运行一次,一旦关闭该函数就不会继续执行
;(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
<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>
显示弹出窗口后,使用.off()
删除滚动处理程序:
if($(document).scrollTop() >= $(document).height() / 5) {
$("#xpopup").show("slow");
$(window).off('scroll');
}
请注意,这将从窗口中删除所有滚动处理程序。 如果只有一个,那不是问题。 如果还有更多内容,请存储对此滚动处理程序的引用,并将其传递给.off()
:
$(window).off('scroll', myScrollPopupHandler);
我会用单身
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
之所以这样做是个好方法,是因为您可能会用其他一些动作来代替alert
。 在这种情况下,您要做的就是用可能引起调用的任何对象替换实例(例如,引导警报)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.