I have...
html, body {
background-size: contain;
background-repeat: no-repeat;
}
As my CSS and in my HTML I have
<html>
<head>
<title>Wut</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="page">
</div>
<script type="text/javascript">
var imgCount = 3;
var dir = 'img/';
var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
var images = new Array();
images[1] = "1.png",
images[2] = "2.png",
images[3] = "3.png",
document.body.style.background = "url(" + dir + images[randomCount] + ")";
</script>
</body>
For some reason the background which is randomly selected by the JS scrolls and i don't want/need it to. Also as a side note: I managed to make it stop scrolling at one point but when i added background-color, half of the background image was covered up and you couldn't see the background image itself.
Thanks for the help
The reason why it happens is because background is a composite property which includes background-color, background-image, background-repeat, background-position, background-attachment. It means that when you set background alone without specifying background-repeat you simply overwrite previously defined rule, it just falls back to default value which is repeat. To fix it you should explicitly provide it:
document.body.style.background = "url(" + dir + images[randomCount] + ") no-repeat";
or set background-image insted:
document.body.style.backgroundImage = "url(" + dir + images[randomCount] + ")";
Add this line
document.body.style.backgroundRepeat="no-repeat";
or another way existing line replace this one
document.body.style.background = "url(" + dir + images[randomCount] + ") no-repeat";
<html>
<head>
<title>Wut</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="page">
</div>
<script type="text/javascript">
var imgCount = 3;
var dir = 'img/';
var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
var images = new Array();
images[1] = "1.png",
images[2] = "2.png",
images[3] = "3.png",
document.body.style.background = "url(" + dir + images[randomCount] + ")";
document.body.style.backgroundRepeat="no-repeat";
</script>
</body>
CSS no need to define
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With