Javascript 无需单击即可在页面加载时启动 FancyBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8434044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start FancyBox on page load without clicking
提问by donguyzo
I want to display a modal window using fancybox at webpage load. This window will display a web page that will allow to choose the desired language. The page displayed in the popup window will be a web page (index_popup.php
) located in the same folder as the home page (index.php
).
我想在网页加载时使用fancybox 显示一个模态窗口。此窗口将显示允许选择所需语言的网页。弹出窗口中显示的页面将是与主页 ( index_popup.php
) 位于同一文件夹中的网页 ( index.php
)。
回答by Reni Raj N R
window.jQuery(document).ready(function() {
$.fancybox.open('#popup_box');
});
回答by ptim
For fancybox 2: the documentation offers the following example: http://jsfiddle.net/STgGM/, to which I've added a document ready:
对于fancybox 2:文档提供了以下示例: http://jsfiddle.net/STgGM/,我已经添加了一个准备好的文档:
jQuery(document).ready(function($) {
$.fancybox.open([
{
href : 'http://fancyapps.com/fancybox/demo/1_b.jpg',
title : '1st title'
},
{
href : 'http://fancyapps.com/fancybox/demo/2_b.jpg',
title : '2nd title'
}
], {
padding : 0
});
});
回答by Renon Stewart
See Fancybox popup once time for session
<script type="text/javascript" src="/js/jquery/jquery.cookie.js"></script>
<script type="text/javascript">
(function($) {
function openFancybox() {
// launches fancybox after half second when called
setTimeout(function () {
$.fancybox.open(
[
{
href : 'http://fancyapps.com/fancybox/demo/1_b.jpg',
}
]
);
}, 1500);
};
var visited = $.cookie('visited'); // create the cookie
if (visited == 'yes') {
return false; // second page load, cookie is active so do nothing
} else {
openFancybox(); // first page load, launch fancybox
};
// assign cookie's value and expiration time
$.cookie('visited', 'yes', {
expires: 1 // the number of days the cookie will be effective
});
})(jQuery);
</script>
回答by parisssss
回答by Ambie
I tried the first solution:
我尝试了第一个解决方案:
window.jQuery(document).ready(function() {
$.fancybox.open('#popup_box');
});
But I wasn't able to get it to load correctly, so I added a variable, then passed that in. It works exactly as I needed it to. Has the slider now, instead of loading each image separately.
但是我无法正确加载它,所以我添加了一个变量,然后将其传入。它完全按照我的需要工作。现在有滑块,而不是单独加载每个图像。
This was my change:
这是我的改变:
window.jQuery(document).ready(function() {
var box = $('#popup_box');
$.fancybox.open(box);
});
回答by king14nyr
Although FancyBox doesn't support a way to auto launch, there have been a few workarounds that seem pretty successful. One notable method I've used is in the link below to a similar question. This should accomplish the onPageLoad:
尽管 FancyBox 不支持自动启动的方式,但有一些解决方法似乎非常成功。我使用过的一个值得注意的方法是在下面的类似问题的链接中。这应该完成 onPageLoad:
回答by Alin Razvan
<head>
<script type="text/javascript">
function autoClick() {
document.getElementById('onload').click();
}
</script>
</head>
<body onLoad="autoClick();">
<a class="fancybox-media" id="onload" href="https://www.youtube.com/watch?v=MWydLB0nFew"></a>