我想要一个按钮来打开带有特定图片的 fancybox rel-group。我不太确定这是否有效,但我会试一试。我的问题是,如果用户单击链接,我想执行 jQuery。这是我的代码
var js = "jQuery.fancybox.pos("+currentImage+");";
// create a new function from the "js" string
var newclick = new Function(js);
jQuery("a.enlarge").click(newclick);
代码基于this answer .我希望
jQuery.fancybox.pos(2)
有效。但目前我无法使用点击功能。我做错了什么?
编辑:
我当前的设置:
<script type="text/javascript">
var currentImage = 0;
function getCurrImage(carousel, state){
currentImage = carousel.first-1;
jQuery("a.enlarge").click(function() {
alert(currentImage);
jQuery.fancybox.pos(currentImage);
});
}
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery('#mycarousel').jcarousel({
scroll:1,
'itemLoadCallback': getCurrImage
});
jQuery('a[rel=example_group]').fancybox({
'transitionIn': 'none',
'transitionOut': 'none'
});
});
</script>
jQuery.fancybox.pos(currentImage);
不起作用。 currentImage
似乎是正确的,但如果按钮被点击 4 次,警告会弹出 4 次。这意味着:
点击1:currentImage=0
点击 2: currentImage=1, currentImage=1
点击 3: currentImage=2, currentImage=2, currentImage=2
点击4:currentImage=3,currentImage=3,currentImage=3,currentImage=3
更新:
新的尝试:
<script type="text/javascript">
var currentImage = 0;
function openfancy(){
jQuery.fancybox.pos(currentImage);
}
function getCurrImage(carousel, state){
currentImage = carousel.first-1;
jQuery("a.enlarge").unbind("click", openfancy);
jQuery("a.enlarge").bind("click", openfancy);
}
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery('#mycarousel').jcarousel({
scroll:1,
'itemLoadCallback': getCurrImage
});
jQuery('a[rel=example_group]').fancybox({
'transitionIn': 'none',
'transitionOut': 'none'
});
});
</script>
jQuery.fancybox.pos(currentImage);
仍然不起作用。有机会吗?也许是因为我在该页面上有多个花式框?我也用 jQuery('.fancybox').fancybox.pos(currentImage);
尝试过,但我得到的错误消息是它不是一个函数。
最佳答案
pos
函数从未起作用。所以我想到了删除答案来使用点击功能。如果用户点击我的链接,它应该模拟点击图库中的图片。
<script type="text/javascript">
var currentImage = 0;
function getCurrImage(carousel, state){
currentImage = carousel.first-1;
}
/*jQuery.noConflict();*/
jQuery(document).ready(function() {
jQuery('#fancycarousel').jcarousel({
scroll:1,
'itemLoadCallback': getCurrImage
});
jQuery('a[rel=example_group]').fancybox({
'transitionIn': 'none',
'transitionOut': 'none'
});
jQuery('#enlarge').click(function(){
jQuery(".jcarousel-item").eq(currentImage).children("a").click();
});
});
</script>
我需要几个小时来解决这个问题!
关于javascript - 链接应该在点击功能的帮助下打开 fancybox ($.fancybox.pos),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8272719/