我遇到了页面加载时砌体图像重叠的问题。为了使图像布局正确,我尝试添加此代码,但仍然无法正常工作并出现错误:未捕获的 TypeError: $container.masonry is not a function
<script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.1/masonry.pkgd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.imagesloaded/3.1.8/imagesloaded.pkgd.min.js"></script>
<script src="/js/jquery.js"></script>
$(document).ready(function () {
var $container = $("#container");
$container.imagesLoaded(function () {
$container.masonry();
});
});
<script>
(function ($) {
var $container = $('.masonry_wrapper'),
colWidth = function () {
var w = $container.width(),
columnNum = 1,
columnWidth = 0;
if (w > 1200) {
columnNum = 4;
} else if (w > 900) {
columnNum = 4;
} else if (w > 600) {
columnNum = 2;
} else if (w > 300) {
columnNum = 1;
}
columnWidth = Math.floor(w/columnNum);
$container.find('.item').each(function() {
var $item = $(this),
multiplier_w = $item.attr('class').match(/item-w(\d)/),
multiplier_h = $item.attr('class').match(/item-h(\d)/),
width = multiplier_w ? columnWidth*multiplier_w[1]-4 : columnWidth-4,
height = multiplier_h ? columnWidth*multiplier_h[1]*0.5-4 : columnWidth*0.5-4;
$item.css({
width: width,
height: height
});
});
return columnWidth;
}
function refreshWaypoints() {
setTimeout(function() {
}, 10200);
}
$('nav.portfolio-filter ul a').on('click', function() {
var selector = $(this).attr('data-filter');
$container.isotope({ filter: selector }, refreshWaypoints());
$('nav.portfolio-filter ul a').removeClass('active');
$(this).addClass('active');
return false;
});
function setPortfolio() {
setColumns();
$container.isotope('reLayout');
}
isotope = function () {
$container.isotope({
resizable: true,
itemSelector: '.item',
masonry: {
columnWidth: colWidth(),
gutterWidth: 0
}
});
};
isotope();
$(window).smartresize(isotope);
}(jQuery));
</script>