【发布时间】:2019-09-06 23:51:46
【问题描述】:
在一个项目中,我为图像列表做了一个砌体效果。
对于 css 和 javascript,我使用了 this tutorial。
我只想将第一个元素的大小增加一倍:
我目前有这 2 个具有经典砖石效果的文件。
JS
jQuery(function ($) {
var resizeMasonryItem = function ( item, selector_to_get_height = ".post-entry-content", isfirst = false ){
/* Get the grid object, its row-gap, and the size of its implicit rows */
var $grid = $('.masonry-container'),
rowGap = parseInt( $grid.css('grid-row-gap' ) ),
rowHeight = parseInt( $grid.css('grid-auto-rows') );
/*
* Spanning for any brick = S
* Grid's row-gap = G
* Size of grid's implicitly create row-track = R
* Height of item content = H
* Net height of the item = H1 = H + G
* Net height of the implicit row-track = T = G + R
* S = H1 / T
*/
var rowSpan
//if( !isfirst ) {
rowSpan = Math.ceil((item.find(selector_to_get_height)[0].getBoundingClientRect().height + rowGap) / (rowHeight + rowGap));
/*}else{
rowSpan = Math.ceil((item.find(selector_to_get_height)[0].getBoundingClientRect().height * 2 + rowGap) / (rowHeight * 2 + rowGap));
}*/
/* Set the spanning as calculated above (S) */
item.css( "grid-row-end", "span " + rowSpan );
item.css( "height", item.find('.brick-inner').height() );
};
var resizeAllMasonryItems = function (selector_to_get_height) {
var allItems = $('.h4a-brick');
/*
* Loop through the above list and execute the spanning function to
* each list-item (i.e. each masonry item)
*/
allItems.each(function (index) {
var isfirst = ( index === 0 );
resizeMasonryItem( $(this), selector_to_get_height, isfirst );
$(this).css("opacity", 1);
});
};
$(window).on("load", function() {
/* Resize all the grid items on the load and resize events */
$(window).bind("load resize", function(e) {
resizeAllMasonryItems( ".brick-inner" );
});
resizeAllMasonryItems(".brick-inner" );
});
});
HTML
<section class="masonry-container">
<article class="brick">
<div class="brick-inner">
<div class="post-thumbnail-wrap">
<div class="post-thumbnail">
<a class="post-thumbnail-rollover" href="..." aria-label="Post image"><img src="..." class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" width="851" height="1200">
</a>
</div><!-- End .post-thumbnail -->
</div><!-- End .post-thumbnail-wrap -->
</div><!-- End .brick-inner -->
</article>
</section>
有人有这个想法吗?
【问题讨论】:
-
你可以在你的项目中使用任何库吗?如果是这样,我建议使用这个:packery.metafizzy.co/draggable.html