Java 脚本鼠标悬停
我正在实现一个鼠标悬停,它会更改 div onMouseDown 和 onMouseUp 的背景,我还尝试预加载图像。
这就是我到目前为止所拥有的;
if(document.images) {
buttonDown = new Image();
buttonDown.src = "buttonDown.png";
}
function down(affect) {
affect.style.backgroundColor="#333333";
affect.style.color="#ffffff";
affect.style.background = buttonDown;
return true;
}
div 使用 onMouseDown="down(this);"
这是行不通的。唯一不起作用的部分是 --affect.style.background = buttonDown; 我省略了脚本标签,但它们都在那里并且按其应有的方式工作。
我的问题是如何将背景属性分配给预加载的图像,而不是仅使用字符串按名称分配图像。
I am implementing a mouseover, which changes the background of a div onMouseDown, and onMouseUp, I am also trying to preload the images.
This is what I have so far;
if(document.images) {
buttonDown = new Image();
buttonDown.src = "buttonDown.png";
}
function down(affect) {
affect.style.backgroundColor="#333333";
affect.style.color="#ffffff";
affect.style.background = buttonDown;
return true;
}
the div uses onMouseDown="down(this);"
This doesn't work. The only part that doesn't work is -- affect.style.background = buttonDown;
I left out the script tags, but they are all there and work as they should.
My question is how do I assign the background property to a preloaded image verses just using a string to assign the image by name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为您访问了错误的样式属性;如果您要使用
backgroundColor
,不妨使用更具体的backgroundImage
。其次,它需要一个字符串,而不是一个
Image
对象。试试这个:
尽管如此,我还是会研究图像 Sprites 和 HTML 类 (CSS) =)
First, I think you are accessing the wrong style attribute; If you are going to use
backgroundColor
, may as well go with the more specificbackgroundImage
.Second, it requires a string, not an
Image
Object.Try this:
All that said, I would look into image Sprites and HTML classes (CSS) =)
我做了更多研究,这就是我的发现。您可以使用设置为 style="display:none" 的 div 预加载图像,并在该 div 中包含图像。
只要下次引用该图像时,使用相同的路径它就会被预加载。
I did some more research and this is what I found. You can preload the images by using a div which is set to style="display:none" and within that div include the images.
As long as the next time you refer to the image, you use the same path it will be preloaded.