gpt4 book ai didi

javascript - 查询/CSS : Full size background image

转载 作者:行者123 更新时间:2023-11-29 14:59:48 27 4
gpt4 key购买 nike

我目前正在使用一个 jquery 插件来设置我的背景图片。我遇到的问题是 CSS/JQuery 使页面不成比例。我的内容位于 #container div 中,因此我将其位置设置为绝对位置(最初是相对位置)。如何让页面内容居中对齐并保持 100% 高度的属性? EXAMPLE

这是在 jquery 插件之前- CSS 100% 高度- EXAMPLE

Jquery后台插件

<script>
(function($) {
$.fn.fullBg = function(){
var bgImg = $(this);

function resizeImg() {
var imgwidth = bgImg.width();
var imgheight = bgImg.height();

var winwidth = $(window).width();
var winheight = $(window).height();

var widthratio = winwidth / imgwidth;
var heightratio = winheight / imgheight;

var widthdiff = heightratio * imgwidth;
var heightdiff = widthratio * imgheight;

if(heightdiff>winheight) {
bgImg.css({
width: winwidth+'px',
height: heightdiff+'px'
});
} else {
bgImg.css({
width: widthdiff+'px',
height: winheight+'px'
});
}
}
resizeImg();
$(window).resize(function() {
resizeImg();
});
};
})(jQuery)
</script>

与问题相关的 CSS

#container {
position: relative;
margin: 0 auto;
width: 945px;
background: #f0f0f0;
height: auto!important;
height: 100%;
min-height: 100%;
border-right: 15px solid #000;
border-left: 15px solid #000;
}

.fullBg {
position: absolute;
top: 0;
left: 0;
overflow: hidden;
}

HTML/Inline JS调用函数

<img src="images/raven_bg.jpg" alt="" id="background" />
<div id="container">
</div>
<script>
$(window).load(function() {
$("#background").fullBg();
});///this comes right before the closing body tag
</script>

最佳答案

这使它居中 -

#container { 
margin-left: 50%;
left: -488px;
}

这有点 hacky,但它确实有效。

50% 偏移(根据宽度保持居中)

宽度的一半,加上边框 = 478.5(或 488)以使其在框架中居中。当然,with "left"之所以有效,是因为它有 position: relative;附在上面

关于javascript - 查询/CSS : Full size background image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11803328/

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