简体   繁体   English

嵌套在另一个函数中的Fancybox函数不起作用

[英]Fancybox function nested into another function doesn't work

I'm working on converting my gallery thumbnails into fancybox links but when I'm trying to target the image content under the .gallery-overlay , it doesn't work and the page just redirects to the photo's source. 我正在将画廊缩略图转换为.gallery-overlay链接,但是当我尝试定位.gallery-overlay下的图像内容时,它不起作用,并且页面仅重定向到照片的来源。 What am I doing wrong here? 我在这里做错了什么? JQuery and fancybox linked properly, jQuery和fancybox正确链接,

$('.gallery-overlay').click(function(){
    $(this).find('.fancybox').fancybox();
});

brings out the fancybox after 2 (!) clicks, but in a useless, incorrect way. 单击2(!)后会弹出花哨框,但这种方式是无用的,不正确的。

 $(document).ready(function() { $('.gallery-overlay').click(function() { $(this).children('img').fancybox(); }); }); 
 .gallery-overlay { position: relative; width: 100%; transition: 1s; } .gallery-overlay img { z-index: -1; position: relative; } .gallery-overlay:hover { background-image: linear-gradient(rgba(0, 0, 0, 0) 45%, rgba(0, 0, 0, .7)70%); transition: 1s; } .gallery-caption { color: white; top: 75%; text-transform: uppercase; font-weight: bold; position: absolute; width: 100%; opacity: 0; z-index: 102; transition: 1s; } .gallery-item:hover .gallery-caption { display: block; transition: 1s; opacity: 1; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js" type="text/javascript" charset="utf-8"></script> <div class="row"> <div class="col-sm-4 gallery-item"> <div class="gallery-overlay"> <a class="fancybox" rel="portfolio" href="photos/office_woman_l.jpg"> <img class="img-responsive" src="photos/thumbnail.jpeg" alt="thumbnail"> </a> </div> <h3 class="gallery-caption text-center">Item caption</h3> </div> <div class="col-sm-4 gallery-item"> <div class="gallery-overlay"> <a class="fancybox" rel="portfolio" href="photos/laptop_l.jpg"> <img class="img-responsive" src="photos/thumbnail.jpeg" alt="thumbnail"> </a> </div> <h3 class="gallery-caption text-center">Item caption</h3> </div> <div class="col-sm-4 gallery-item"> <div class="gallery-overlay"> <a class="fancybox" rel="portfolio" href="photos/glasses_woman_l.jpg"> <img class="img-responsive" src="photos/thumbnail.jpeg" alt="thumbnail"> </a> </div> <h3 class="gallery-caption text-center">Item caption</h3> </div> </div> 

Fancybox works when clicking the a element which has a class fancybox not the image, so your script must be like this: Fancybox可以在单击具有fancybox类而不是图像的类的元素时工作,因此您的脚本必须像这样:

$(document).ready(function() {
  $('.gallery-overlay a').fancybox();
});

https://jsfiddle.net/IA7medd/ds5j8vz5/ https://jsfiddle.net/IA7medd/ds5j8vz5/

You should apply the .fancybox() to tag <a> and prevent the click event on this tag. 您应该将.fancybox()应用于标签<a>并防止对此标签进行click事件。 Also, you should put the tag <h3> for inside the .gallery-overlay . 另外,您应该将标记<h3>放在.gallery-overlay When the caption appears, it covers the .gallery-overlay and the click event isn't triggered. 当标题出现时,它将覆盖.gallery-overlay并且不会触发click事件。

$(document).ready(function() {
    $('.gallery-overlay').click(function(e) {
        e.preventDefault();
        $(this).children('a').fancybox();
    });
});

https://jsfiddle.net/alexndreazevedo/u9d2pwo8/ https://jsfiddle.net/alexndreazevedo/u9d2pwo8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM