在背景颜色功能下添加背景图像

时间:2013-02-04 04:50:10

标签: javascript html css image background

我在交互式宠物上运行了三个功能(更改背景,调光灯,鼠标跟随)。我正在尝试将图像添加到身体的背景中,但每次尝试它都会被推到顶部并且脏/清洁槽功能不起作用。有人能告诉我如何更改下面第一个功能的颜色部分切换图像而不是颜色代码?我认为这可以解决眼前的问题。 :/

CSS代码:

//change background color: dirty/clean tank
setTimeout(function dirty() { //sets background color to murky green
document.body.style.backgroundColor = ****"#CCCC99";** //would like this to be an image**
var btn = document.body.appendChild(document.createElement('button'));
btn.appendChild(document.createTextNode("Clean the tank!"));
    document.body.style.marginTop = "-1000px";

btn.onclick = function clean() {
    btn.parentNode.removeChild(btn);
    document.body.style.backgroundColor = **"#CCFFFF";//would like this to be an image**
    setTimeout(dirty,27000); //how long it takes to make the tank dirty
};
},500); //first loading of clear blue timeout


//dim the lights
 var dimmed = 0;
function toggleLights()
{ 
var dimmer = document.getElementById("dimmer");
if(dimmed == 0) dimmed = 1;
else dimmed = 0;

if(dimmed == 1)
{
    dimmer.style.opacity = 0.8;
    dimmer.style.visibility = "visible";
}

if(dimmed == 0)
{
    dimmer.style.opacity = 0.0;
    dimmer.style.visibility = "hidden";
}
}


//fish moves on mouse hover
$(document).ready(function() {  
$("#swim").mousemove(function (event) {
var fish = $("#fish1");
var position = fish.position();
var mousey = event.pageX;


if (position.left > mousey) {
    $("#fish1").html("<img src='images/happy.png' />");
} else {
    $("#fish1").html("<img src='images/happyback.png'/>");
}

$("#fish1").stop().animate({
    left: event.pageX,
    top: event.pageY
}, 300);
});
}); 

HTML代码:

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>


<link href="style.css" rel="stylesheet" type="text/css" />  

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"
</script>
<script type="text/javascript" src="pet.js"></script>  
<script type="text/javascript" src="core.js"></script> 

 </HEAD>



<div id="table">
<div id="dimmer" onClick="toggleLights()"></div>
<a href="javascript:toggleLights();"><img src="images/table-lamp.png" height="380px" 
alt="table lamp"></a>
</div>


<div id="swim">
<div id="fish1"><img src="images/happy.png"/></div>
</div> 

</BODY>
</HTML>

1 个答案:

答案 0 :(得分:0)

在HTML中添加一个起始<body>标记,并使用javascript而不是样式属性更改元素类。这样,您可以通过CSS文件更好地控制样式。

所以你会做类似的事情:

document.body.className = 'state1';

在你的CSS中

.state1{
   background-image: url(the/image.jpg);
}

依旧......