gpt4 book ai didi

jquery - 将图像容器高度调整为某个值的倍数(加载、方向更改和调整大小时)

转载 作者:行者123 更新时间:2023-12-01 06:04:19 26 4
gpt4 key购买 nike

我需要改进一个 jquery 脚本,它将图像容器的高度捕捉到基线网格。我想让它在窗口大小调整期间工作。

到目前为止,我将以下内容应用于将溢出设置为隐藏的图像容器。其中一些是受到提出的问题的启发 herehere .

/* Updates height on $(window).load(function(){}); */
$(window).load(function(){

/* Adjust media container heights and image margins */
function containerHeight(){
var targetContainer = 'div.media';
$(targetContainer).each(function(){
var targetHeight = Math.round($(this).height() / 20) * 20 - 8;
if($(this).height() < targetHeight){
var newHeight = targetHeight - 20;
}else{
var newHeight = targetHeight;
}
$(this).css({
'height': newHeight
});
});
$(targetContainer).children('img').each(function(){
$(this).css({
'margin-top': Math.round(($(this).parent(targetContainer).height() - $(this).height()) / 2 )
});
});
}
containerHeight();
/* Does not update height on $(window).bind('resize', function(){}); with <body class="full"> */
$(window).bind('resize', function(){
containerHeight();
});

});

但是,我的馅饼在 http://pastie.org/2846491仅适用于固定宽度容器。

我需要在窗口调整大小期间/之后和移动方向:纵向/横向更改时,使这项工作适用于流体容器宽度/高度,同时将内容保持在基线网格上。

关于我应该如何解决这个问题有什么建议吗?请帮忙!

最佳答案

我最近遇到了这个问题,并临时设计了一个解决方案。不是最优雅/最有效的解决方案,但肯定可行:

var fontSize = 20;
var lineHeight = 28;

$(window).resize(function() {
$('img').each(function() {
var heightDif = $(this).outerHeight();
$(this).css('margin-bottom', fontSize + heightDif % lineHeight);
});
});
$('img').load(function() { $(window).resize(); });

只要调整窗口大小,代码就会触发,并计算图像高度和下一个最低基线之间的差值。然后它将差值添加到底部边距。该事件首先在 img 加载时触发(这是提高效率的机会)。

我希望这有帮助;如果您有任何可以使其变得更好的改进,我很想知道,以便我可以在我的代码中实现它们!

关于jquery - 将图像容器高度调整为某个值的倍数(加载、方向更改和调整大小时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8078906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com