https://github.com/weii2008/beiwanglu/tree/main
// 窗口大小变化事件监听器
$(window).on('resize', function () {
// 延迟触发 Packery 的重新布局,以确保页面布局稳定
clearTimeout(window.packeryResizeTimeout);
window.packeryResizeTimeout = setTimeout(function () {
$grid.packery();
}, 100);
// 改变图片尺寸
// 获取窗口宽度
var windowWidth = $(window).width();
// 设置图片尺寸和占位区域比例
$(".article_content").find("img.aligncenter").each(function () {
var _width = parseInt($(this).attr('width')) || 0; // 获取图片的宽度属性
var _height = parseInt($(this).attr('height')) || 0; // 获取图片的高度属性
if (_width > 0 && _height > 0) {
var aspectRatio = _height / _width; // 计算宽高比
if (windowWidth > 2000 || windowWidth < 1000) {
// 窗口宽度过大或过小时,设置宽度为 985,并调整高度
$(this).css({
width: 985,
height: 985 * aspectRatio,
});
} else {
// 其他情况下设置宽度为 790,并调整高度
$(this).css({
width: 790,
height: 790 * aspectRatio,
});
}
} else {
// 如果缺少宽高属性,设置默认占位比例或其他处理逻辑
$(this).css({
width: 790, // 默认宽度
height: 790 * (3 / 4), // 默认 4:3 比例
});
}
});
});
// 初始化窗口大小
$(window).trigger('resize');