可以使用 JavaScript 在翻转时更改图像的来源吗?
我有一个关于使用 JavaScript 的翻转效果的问题。
我读过,当使用 JS 处理 onmouseover
时,可以在 CSS 偏移的帮助下使用图像精灵(侧面有另一个图像的图像)来实现基本的翻转。还阅读了有关使用 JS (className) 更改类本身以实现上述效果的可能性。
我的问题是,我可以使用JavaScript修改图像src本身,以实现翻转效果吗?
也许是这样的代码 -
document.getElementByID("button1").onmouseover = rolloverFunction;
function rolloverFunction(){
this.src = "button1-image2.png";
}
我输入了类似的内容来查看图像的 src 是否可以在翻转时修改,但它不起作用。你能让我知道我哪里出错了吗?
I have a question on rollover effects using JavaScript.
I have read that image sprites (image with another image on its side) can be used with help of CSS offsetting to achieve basic rollover when onmouseover
is handled using JS. Have also read about the possibility of changing classes itself using JS (className) to achieve the above effect.
My question is, can I modify the image src itself using JavaScript, to achieve the rollover effect?
A code like this, maybe -
document.getElementByID("button1").onmouseover = rolloverFunction;
function rolloverFunction(){
this.src = "button1-image2.png";
}
I typed something like this to see if the src of the image can be modified upon rollover, but it is not working. Could you please let me know where I am going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
mouseover
和mouseout
事件。当鼠标悬停在图像中时触发mouseover
,当离开图像时触发mouseout
。使用普通的 JS 会产生:或者使用通用函数来避免重复 URL:
You need the
mouseover
andmouseout
events.mouseover
is triggered when hovering in the image,mouseout
on leaving the image. Using plain ol' JS would yield:Or using a common function to avoid duplicating the URL: