Javascript Jquery/CSS:全尺寸背景图像
我目前正在使用jquery插件来设置我的背景图像。我遇到的问题是CSS/JQuery使页面成比例。我的内容位于Javascript Jquery/CSS:全尺寸背景图像,javascript,jquery,css,Javascript,Jquery,Css,我目前正在使用jquery插件来设置我的背景图像。我遇到的问题是CSS/JQuery使页面成比例。我的内容位于#containerdiv中,因此我将其位置设置为绝对(最初是相对的)。如何使页面内容居中对齐并保持100%高度的属性 这是在jquery插件-CSS 100%高度- Jquery后台插件 <script> (function($) { $.fn.fullBg = function(){ var bgImg = $(this); func
#container
div中,因此我将其位置设置为绝对(最初是相对的)。如何使页面内容居中对齐并保持100%高度的属性
这是在jquery插件-CSS 100%高度-
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>
<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>
HTML/Inline JS调用函数
<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>
<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>
$(窗口)。加载(函数(){
$(“#背景”).fullBg();
});///这正好在结束正文标记之前
这将使它居中-
#container {
margin-left: 50%;
left: -488px;
}
这有点老套,但很管用
50%移位(根据宽度保持其居中)
宽度的一半,加上边框=478.5(或488),使其在框架中居中。当然,“左”只起作用,因为它有位置:相对;附加到它您有浏览器限制吗?有什么理由不能在CSS中使用背景大小吗?