我的代码有点问题,但我找不到问题出在哪里。 如果我在 css 中使用带有“body”标签的背景,它会显示,但如果我对 div 做同样的事情,它会保持白色。
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/style.css">
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<div id="one">
<header>
</header>
<section>
</section>
<footer>
</footer>
</div>
</body>
</html>
CSS
#one{
background: url(../img/background.jpg);
background-repeat: repeat;
background-size: cover;
display: block;
height: 100%;
width: 100%;
}
和一个javascript(以防万一)
$(function(){
var x = 0;
setInterval(function(){
x-=1;
$('body').css('background-position', 0 + 'px '+ x +'px');
}, 200);
})
我尝试使用“body”并且它有效,但仅适用于 body :(
最佳答案
要在 #one
div 中使用 100% 高度,您还需要为 body
和 html
标签设置 100% 高度。没有这条规则,100% 宽度也可以。
因此,将以下行添加到您的 css 文件中:
html, body { height: 100%; }
关于javascript - css 背景在 div 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825495/