0

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

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>
4

1 回答 1

0

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

所以你会做这样的事情:

document.body.className = 'state1';

在你的 CSS

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

等等...

于 2013-02-04T04:57:03.537 回答