如果哈希值在 url 中可用,我试图让 fancybox 触发,问题是我使用的不同方法,其中一些方法取自 stackoverflow,在 firefox 的错误控制台中返回“c 不可用”。我正在尝试的网站可以在这里找到 http://rojava.se/om/#infor 。非常感谢所有帮助
我尝试过的方法: How to create a direct link to any fancybox box
<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
if(window.location.hash) {
$(thisHash).fancybox({
padding: 0
// more API options
}).trigger('click');
}
$('.fancylink').fancybox({
padding: 0
// more API options
});
}); // ready
</script>
第二种方法:Fancybox doesn't trigger on $(elem).click(), but does on "real" click?
jQuery('a[href="http://hallarna.se/wp-content/gallery/2013/spoksonatenloggaliten.jpg"]').trigger('mousedown').trigger('click');
最佳答案
你必须做两件事:
1)。更改您的触发代码:
当前您正在使用此触发器:
var thisHash = window.location.hash;
if(window.location.hash) {
$(thisHash).trigger('mousedown').trigger('click');
}
但是你对自己说你尝试过这篇文章的解决方案 How to create a direct link to any fancybox box因此你的代码必须如下所示:
var thisHash = window.location.hash;
if(window.location.hash) {
$(thisHash).fancybox().trigger('click');
}
因为选择器#infor
未绑定(bind)到 fancybox (v1.3.4),因此您需要 bind
在触发 click
之前执行此操作.
2)。在 fancybox 初始化之后调用触发代码。
还是不行?这是一个吊装问题。您的触发代码当前位于 <head>
文档的部分,但 fancybox.js 文件正在页面底部被调用,因此即使按照上面的建议更改代码也不会触发,因为 fancybox 尚未初始化。
将代码放置在 fancybox 调用和初始化之后以及结束之前 </body>
标签如:
<script type='text/javascript' src='http://rojava.se/wp-content/plugins/easy-fancybox/fancybox/jquery.fancybox-1.3.5.pack.js?ver=1.5.5'></script>
<script type='text/javascript' src='http://rojava.se/wp-content/plugins/easy-fancybox/jquery.easing.pack.js?ver=1.3'></script>
<script type="text/javascript">
jQuery(document).on('ready post-load', easy_fancybox_handler );
jQuery.noConflict();
(function($) {
$(function() {
$('#footbuttoncontainer ').click(function() {
container = $('#footcontentcontainer');
if(container.height() > 20){
$('#footcontentcontainer').animate({height:"0"}, 1000);
} else {
$('#footcontentcontainer').animate({height:"26vmin"}, 1000);
}
});
var thisHash = window.location.hash;
if(window.location.hash) {
$(thisHash).fancybox().trigger('click');
}
});
})(jQuery);
</script>
</body>
注意我在 fancybox 初始化之后移动了完整的代码块。
现在看看它与hash
一起工作包含在 URL 中:
关于javascript - Fancybox 触发点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18675294/