jquery - 当元素的高度改变时砌体自动调整

标签 jquery html css

我正在使用砌体来布置元素,如果描述太长,它会显示阅读更多链接。这将以 Accordion 样式打开其余文本。这是一个例子:http://www.ninesixty.co.nz/cubadupa18/?post_type=marcato_artist

我怎样才能强制砌体重新调整以适应这种情况?

这是mnasonry代码

        jQuery(window).load(function() {

  // MASSONRY Without jquery
  var container = document.querySelector('#masonry-content');
  var msnry = new Masonry( container, {
    itemSelector: '.grid_4',
    columnWidth: '.grid_4',                
  });  

    });

阅读更多代码: //DOM 就绪 $(函数() {

        var $el, $ps, $up, totalHeight;

        $(".sidebar-box .button").click(function() {

            // IE 7 doesn't even get this far. I didn't feel like dicking with it.

            totalHeight = 0

            $el = $(this);
            $p  = $el.parent();
            $up = $p.parent();
            $ps = $up.find("p:not('.read-more')");

            // measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)
            $ps.each(function() {
                totalHeight += $(this).outerHeight();
                // FAIL totalHeight += $(this).css("margin-bottom");
            });

            $up
                .css({
                    // Set height to prevent instant jumpdown when max height is removed
                    "height": $up.height(),
                    "max-height": 9999
                })
                .animate({
                    "height": totalHeight
                });

            // fade out read-more
            $p.fadeOut();

            // prevent jump-down
            return false;

        });

    });

我是 jQuery 和 Javascript 的新手,所以请尝试任何易于理解的建议。

最佳答案

只需再次调用 masonry 函数来刷新它。

jQuery('#masonry-content').masonry();

如果你想要 或者 fadeOut 回调,你可以把它放在动画回调中。

        .animate({
                "height": totalHeight
            },500,function(){
             //Callback - after the animation is over
            jQuery('#masonry-content').masonry();
        });

关于jquery - 当元素的高度改变时砌体自动调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501085/

相关文章:

javascript - 在 ember.js 路由中按属性过滤多个模型

javascript - 滚动宽度总是返回未定义

javascript - jQuery:获取链接的 html 代码

javascript - 为什么 jQuery 使用原始 DOM 元素类,即使我用 jQuery 更改了它们?

javascript - 播放音频文件返回 "Uncaught (in promise)"但在控制台中有效

android - 如何设置单选按钮的高度和宽度?

javascript - Angular-vs-repeat - 使用 col-md 类时无法滚动到末尾

html - Bootstrap 响应式页脚图像对齐

javascript - 如何向 div 中的不同元素添加不同的类?

JavaScript:显示未知数量的图 block 的逻辑