Javascript 将功能元素IMG更改为DIV背景
我有静态HTML结构和我的背景图像是全屏每页。在标记后添加img标记:Javascript 将功能元素IMG更改为DIV背景,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有静态HTML结构和我的背景图像是全屏每页。在标记后添加img标记: <img src="img/about.jpg" id="static_bg" alt=""> 最后,我的JS代码: $(window).load(function() { var theWindow = $(window), $bg = $("#static_bg"), aspectRatio = $bg.width() /
<img src="img/about.jpg" id="static_bg" alt="">
最后,我的JS代码:
$(window).load(function() {
var theWindow = $(window),
$bg = $("#static_bg"),
aspectRatio = $bg.width() / $bg.height();
function resizeBg() {
if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
$bg
.removeClass()
.addClass('staticbgheight');
} else {
$bg
.removeClass()
.addClass('staticbgwidth');
}
}
theWindow.resize(resizeBg).trigger("resize");
});
$(窗口).load(函数(){
var theWindow=$(窗口),
$bg=$(“#静态#bg”),
aspectRatio=$bg.width()/$bg.height();
函数resizeBg(){
如果((theWindow.width()/theWindow.height())
是的,很好用。但我想,删除我的IMG并添加一个DIV背景。当我改变这一点时,不要工作
我怎样才能修好它?谢谢。尝试在CSS中使用
背景属性
#static_bg { background:url('img/about.jpg') ;
position: fixed; top: 0; left: 0; z-index: -2;}
<div id="static_bg"></div>
和Div
类似
#static_bg { background:url('img/about.jpg') ;
position: fixed; top: 0; left: 0; z-index: -2;}
<div id="static_bg"></div>
您可以使用background size:cover
并在body
上应用background
,如果上面的不起作用
请参见
.你的部门{
背景:url('img/about.jpg')不重复;
背景尺寸:封面;
}
为图像设置一个ID,如下所示:
<img id="background-img" src="img/about.jpg" id="static_bg" alt="">
$("#background-img").css("display","none");
然后通过css删除图像,如下所示:
<img id="background-img" src="img/about.jpg" id="static_bg" alt="">
$("#background-img").css("display","none");
然后为您想要的元素设置背景。在这种情况下,$(“#static_bg”)
你期望什么样的div背景html?