具有 jQuery 无限滚动和 jQuery 砌体的 Drupal.attachBehaviours

发布于 2025-01-04 16:49:35 字数 2390 浏览 0 评论 0原文

我在这里有点绝望。我已经阅读了在 Drupal.behaviours 上找到的所有内容,但显然这还不够。我尝试使用无限滚动插件运行砌体网格,以将新图像附加到砌体。到目前为止效果很好。我想要在我的网站上实现的下一件事是悬停效果(显示图像信息)和后来的 fancybox 以更大尺寸显示图像。

(function ($) {
    Drupal.behaviors.views_fluidgrid = {
        attach: function (context) {
            $('.views-fluidgrid-wrapper:not(.views-fluidgrid-processed)', context).addClass('views-fluidgrid-processed').each(function () {
                // hide items while loading
                var $this = $(this).css({opacity: 0}),      
                id = $(this).attr('id'),
                settings = Drupal.settings.viewsFluidGrid[id];

                $this.imagesLoaded(function() {
                    // show items after .imagesLoaded()
                    $this.animate({opacity: 1});
                    $this.masonry({
                        //the masonry settings
                    });
                });

                //implement the function of jquery.infinitescroll.min.js
                $this.infinitescroll({
                    //the infinitescroll settings
                },

                //show new items and attach behaviours in callback
                function(newElems) {
                    var newItems = $(newElems).css({opacity: 0});
                    $(newItems).imagesLoaded(function() {
                        $(newItems).animate({opacity: 1});
                        $this.masonry('appended', newItems);
                        Drupal.attachBehaviours(newItems);
                    });
                });

            });
        }
    };
})(jQuery);

现在我读到,如果我希望悬停事件也发生在新添加的内容上,我需要重新附加 Drupal.behaviours。

(function ($) {
    Drupal.behaviors.imgOverlay = {
        attach: function (context) {
            var timeout;
            $('.img_gallery').hover(function() {
                $this = $(this);
                timeout = setTimeout(change_opacity, 500);
                }, reset_opacity);

            function change_opacity() {
                //set opacity to show the desired elements
            }

            function reset_opacity() {
                clearTimeout(timeout);
                //reset opacity to 0 on desired elements
            }
        }
    };
})(jQuery)

我现在在哪里编写 Drupal.attachBehaviours() 才能使其实际工作?或者还有其他错误,我只是没有看到自动取款机?我希望我写的问题是可以理解的,也许它也可以帮助其他人,因为我经历过在 drupal 7 中没有真正的“官方”运行版本的这个组合。

I am a little desperate here. I have been reading everything I was able to find on Drupal.behaviours but obviously its still not enough. I try running a masonry grid with the infinitescroll plugin to attach the new images to the masonry. This works fine so far. The next thing I wanted to implement to my website is a hover effect (which shows information on the images) and later fancybox to show the images in a huger size.

(function ($) {
    Drupal.behaviors.views_fluidgrid = {
        attach: function (context) {
            $('.views-fluidgrid-wrapper:not(.views-fluidgrid-processed)', context).addClass('views-fluidgrid-processed').each(function () {
                // hide items while loading
                var $this = $(this).css({opacity: 0}),      
                id = $(this).attr('id'),
                settings = Drupal.settings.viewsFluidGrid[id];

                $this.imagesLoaded(function() {
                    // show items after .imagesLoaded()
                    $this.animate({opacity: 1});
                    $this.masonry({
                        //the masonry settings
                    });
                });

                //implement the function of jquery.infinitescroll.min.js
                $this.infinitescroll({
                    //the infinitescroll settings
                },

                //show new items and attach behaviours in callback
                function(newElems) {
                    var newItems = $(newElems).css({opacity: 0});
                    $(newItems).imagesLoaded(function() {
                        $(newItems).animate({opacity: 1});
                        $this.masonry('appended', newItems);
                        Drupal.attachBehaviours(newItems);
                    });
                });

            });
        }
    };
})(jQuery);

Now I read that I need to Reattach the Drupal.behaviours if I want the hover event to also take place on the newly added content.

(function ($) {
    Drupal.behaviors.imgOverlay = {
        attach: function (context) {
            var timeout;
            $('.img_gallery').hover(function() {
                $this = $(this);
                timeout = setTimeout(change_opacity, 500);
                }, reset_opacity);

            function change_opacity() {
                //set opacity to show the desired elements
            }

            function reset_opacity() {
                clearTimeout(timeout);
                //reset opacity to 0 on desired elements
            }
        }
    };
})(jQuery)

Where do I now write the Drupal.attachBehaviours() to make it work actually? Or is there some other error I just dont see atm? I hope I wrote the question so that its understandable and maybe it also helps somebody else, since I experienced that there is no real "official" running Version of this combination in drupal 7.

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

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

发布评论

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

评论(1

烂柯人 2025-01-11 16:49:35

好的,解决方案实际上非常简单。当正确编写它时,它也会运行。它当然不是 Drupal.attachBehaviours() 而是 Drupal.attachBehaviors() 。所以这个组合现在起作用了,我终于松了一口气:)。

Ok, the solution is actually pretty simple. When writing it correctly than it also runs. its of course not Drupal.attachBehaviours() but Drupal.attachBehaviors() . So this combination now works and I am finally relieved :).

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