使用 JavaScript 翻转图像时更改页面背景
我目前正在使用 javascript 来实现此目的,但如果您有更好的解决方案,我会洗耳恭听。所以我有我的包装器及其背景,其中是我的所有内容,包括我想要在悬停淡入淡出时更改包装器/页面背景的按钮。
我的CSS看起来像这样
#wrapper {position: absolute; min-width:550px; width:100%; height:100%; background: url(img/bg.jpg) no-repeat center top;}
,然后是我的html
<div id="wrapper">
<div id="content">
<div class="logo">
<img src="img/logo1.png" />
</div>
<div class="button">
<img src="img/dj.png" onMouseOver="changeBgImage('img/bg2.jpg','wrapper')" onMouseOver="changeBgImage('img/bg2.jpg','wrapper')"/>
</div>
</div>
</div>
,然后是我的javascript,
<script type="text/javascript">
function changeBgImage (image, id) {
var element = document.getElementById(id);
element.style.backgroundImage = "url("+image+")";
}
</script>
所以当它运行时,它会改变背景,然后在鼠标移开时将其更改回来,但是我希望它淡入和淡出。我该怎么做呢?
编辑:尝试了下面的两种解决方案,似乎都不起作用或适用
I am currently using javascript for this but if you have a better solution I am all ears. So I have my wrapper and its background and within that is all my content including a button that I want to when hovering fade to change the background of the wrapper/page.
my css looks like this
#wrapper {position: absolute; min-width:550px; width:100%; height:100%; background: url(img/bg.jpg) no-repeat center top;}
and then my html
<div id="wrapper">
<div id="content">
<div class="logo">
<img src="img/logo1.png" />
</div>
<div class="button">
<img src="img/dj.png" onMouseOver="changeBgImage('img/bg2.jpg','wrapper')" onMouseOver="changeBgImage('img/bg2.jpg','wrapper')"/>
</div>
</div>
</div>
and then my javascript
<script type="text/javascript">
function changeBgImage (image, id) {
var element = document.getElementById(id);
element.style.backgroundImage = "url("+image+")";
}
</script>
So as it sits it works, it changes the background and then changes it back on mouseout, however I want it to fade in and out. How would I go about doing this??
EDIT: attempted both solutions below, neither seem to work or apply
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个很好、干净的方法。
http://jsfiddle.net/Diodeus/gYyBL/
Here is a nice, clean way to do it.
http://jsfiddle.net/Diodeus/gYyBL/
我喜欢你的解决方案。 查看此内容以获取淡入淡出效果的选项你正在努力实现。
祝你好运!
编辑: https://stackoverflow.com/a/1055432/752527 似乎也是一个不错的干净解决方案。
I like your solution. Check this out for an option for the fading effect you are trying to accomplish.
Good luck!
Edit: https://stackoverflow.com/a/1055432/752527 also seems like a nice clean solution.