jquery + fancybox问题,fancybox jquery事件没有“看到”刚刚添加到 DOM 树的 html 内容

发布于 2024-12-10 21:21:37 字数 3087 浏览 0 评论 0原文

我使用 jQuery 和 Fancybox 来以这种方式生成图像库:我只向用户显示一张中等大小的图像,他单击(然后单击预加载到隐藏

中的其他图像链接)并获得漂亮的图像库。

我需要预加载图像链接,因为它不是静态列表,它取决于用户单击的第一个图像,这样我可以防止 fancybox 显示重复的图像。

这是我的代码(有点混乱,因为我从 ruby​​ 获取图像 url):

javascript:

$(function() {

    $('#car_image').click(function(e) { // generating fancybox gallery containing only large images
    // here we should generate fancybox <div id="large_images_gallery_div"> and place this div inside of <div id="car_image">

    var gallery_images_urls = [];
    var gallery_images_tagged = '';

    <% @car.photos.each do |car_photo| %>
        var current_image_url = '<%= car_photo.image.url(:large) %>';

        if(current_image_url != $('#changeme').attr('href')) gallery_images_tagged += '<a href="' + current_image_url + '" class="fancybox" rel="my_car_gallery" />';
    <% end%>

        $('#car_image div#large_images_gallery_div').replaceWith('<div id="large_images_gallery_div" style="visibility: hidden;">' + gallery_images_tagged + '</div');

    });

  $("a.fancybox").fancybox();


});

html:

<div id="car_image">
    <%= link_to @car.photos.first.image.url(:large), :class => "fancybox", :rel => "my_car_gallery", :id => "changeme" do %>
        <%= image_tag @car.photos.first.image.url(:medium) %>
    <% end%>

    <div id="large_images_gallery_div" style="visibility: hidden;">
                <-- HERE ALL GALLERY IMAGES LINKS BEING PRELOADED -->
    </div>
</div>

问题: 我的列表已正确加载,但 fancybox 只显示第一个图像,不显示其他图像,这是由 jQuery 添加的replaceWith() 方法

可能无法“看到”DOM 树已更改,

顺便说一句,对我糟糕的英语

UPDATE

图像之前的 html 感到抱歉链接预加载:

<div id="car_image">
    <a rel="my_car_gallery" id="changeme" class="fancybox" href="/uploads/photo/image/44/large_1.jpg">
        <img src="/uploads/photo/image/44/medium_1.jpg">
    </a>

    <div style="visibility: hidden;" id="large_images_gallery_div">

    </div>
</div>

图像链接预加载后的 html:

<div id="car_image">
    <a rel="my_car_gallery" id="changeme" class="fancybox" href="/uploads/photo/image/44/large_1.jpg">
        <img src="/uploads/photo/image/44/medium_1.jpg" >
    </a>

    <div style="visibility: hidden;" id="large_images_gallery_div">
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/45/large_2.jpg"></a>
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/46/large_3.jpg"></a>
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/47/large_4.jpg"></a>
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/48/large_5.jpg"></a>
    </div>
</div>

I am using jQuery with Fancybox for generating images gallery this way: I show to user just one medium-sized image which he clicks (and after that click other images links being preloaded into hidden <div>) and gets nice images gallery.

I need to preload image links because it's not a static list, it depends on first image which user clicked and this way I can prevent fancybox to show duplicate images.

Here is my code (a bit messy, because I'm getting image urls from ruby):

javascript:

$(function() {

    $('#car_image').click(function(e) { // generating fancybox gallery containing only large images
    // here we should generate fancybox <div id="large_images_gallery_div"> and place this div inside of <div id="car_image">

    var gallery_images_urls = [];
    var gallery_images_tagged = '';

    <% @car.photos.each do |car_photo| %>
        var current_image_url = '<%= car_photo.image.url(:large) %>';

        if(current_image_url != $('#changeme').attr('href')) gallery_images_tagged += '<a href="' + current_image_url + '" class="fancybox" rel="my_car_gallery" />';
    <% end%>

        $('#car_image div#large_images_gallery_div').replaceWith('<div id="large_images_gallery_div" style="visibility: hidden;">' + gallery_images_tagged + '</div');

    });

  $("a.fancybox").fancybox();


});

html:

<div id="car_image">
    <%= link_to @car.photos.first.image.url(:large), :class => "fancybox", :rel => "my_car_gallery", :id => "changeme" do %>
        <%= image_tag @car.photos.first.image.url(:medium) %>
    <% end%>

    <div id="large_images_gallery_div" style="visibility: hidden;">
                <-- HERE ALL GALLERY IMAGES LINKS BEING PRELOADED -->
    </div>
</div>

THE ISSUE: my list being loaded correctly, but fancybox shows me just first image and does not show others, which was added by jQuery's replaceWith() method

possible it can't "see" that DOM tree has been changed

BTW, sorry for my poor english

UPDATE

html before images links preloading:

<div id="car_image">
    <a rel="my_car_gallery" id="changeme" class="fancybox" href="/uploads/photo/image/44/large_1.jpg">
        <img src="/uploads/photo/image/44/medium_1.jpg">
    </a>

    <div style="visibility: hidden;" id="large_images_gallery_div">

    </div>
</div>

html after images links preloading:

<div id="car_image">
    <a rel="my_car_gallery" id="changeme" class="fancybox" href="/uploads/photo/image/44/large_1.jpg">
        <img src="/uploads/photo/image/44/medium_1.jpg" >
    </a>

    <div style="visibility: hidden;" id="large_images_gallery_div">
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/45/large_2.jpg"></a>
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/46/large_3.jpg"></a>
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/47/large_4.jpg"></a>
        <a rel="my_car_gallery" class="fancybox" href="/uploads/photo/image/48/large_5.jpg"></a>
    </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-12-17 21:21:38

正如我在 #jquery @ freenode 上被告知的,这个问题是关于 jQuery.delegate()< /a> 所以我得到了这个方法的帮助:

$('#car_image').delegate(
    ".fancybox",
    "click",
        function (ev) {
            ev.preventDefault();
            $.fancybox(
                (function () {
                    var gallery_images_urls = [];

                    gallery_images_urls.push('<img src="' + $('a#changeme').attr('href') + '" />'); // pushing first image

                    <% @car.photos.each do |car_photo| %>
                        if($('a#changeme').attr('href') != '<%= car_photo.image.url(:large) %>') gallery_images_urls.push('<img src="<%= car_photo.image.url(:large) %>" />');
                    <% end %>

                    return gallery_images_urls;
                }).bind(this)(),
                    {
                        transitionIn : 'elastic',
                        transitionOut : 'elastic',
                        speedIn : 600,
                        speedOut : 200
                    }
            );
    }
);

每次点击#car_image都会触发delegate(),我解决了直接向$提供fancybox图像对象的问题.fancybox()无需任何额外 html 预加载的方法

As I was told on #jquery @ freenode this question is about jQuery.delegate() so I got help with this method:

$('#car_image').delegate(
    ".fancybox",
    "click",
        function (ev) {
            ev.preventDefault();
            $.fancybox(
                (function () {
                    var gallery_images_urls = [];

                    gallery_images_urls.push('<img src="' + $('a#changeme').attr('href') + '" />'); // pushing first image

                    <% @car.photos.each do |car_photo| %>
                        if($('a#changeme').attr('href') != '<%= car_photo.image.url(:large) %>') gallery_images_urls.push('<img src="<%= car_photo.image.url(:large) %>" />');
                    <% end %>

                    return gallery_images_urls;
                }).bind(this)(),
                    {
                        transitionIn : 'elastic',
                        transitionOut : 'elastic',
                        speedIn : 600,
                        speedOut : 200
                    }
            );
    }
);

which triggers delegate() for every click on #car_image, I solved to provide fancybox images objects directly to $.fancybox() method without any additional html preloading

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文