【发布时间】:2018-08-09 10:28:59
【问题描述】:
我有两个版本的图像 - 灰色图像和同一图像的彩色版本。在正常状态下,图像是灰色的。悬停或单击时,图像变为彩色版本。
我知道这可以通过toggleClass/addClass/etc 或更改attr(src) 来完成。我已经设法让这个与悬停一起工作,但我无法让点击元素工作。
这些是我正在尝试使用的图像和页面 - http://www.expatlifeinsurance.com/contact-health-insurance/
这是我现在的代码:
<div id="smokers">
<img id="nosmoke" src="http://www.expatlifeinsurance.com/images/smoke_no_grey.png">
<img id="smoke" src="http://www.expatlifeinsurance.com/images/smoke_grey.png">
</div>
$("#nosmoke")
.mouseover(function() {
this.src = this.src.replace('http://www.expatlifeinsurance.com/images/smoke_no_grey.png', 'http://www.expatlifeinsurance.com/images/smoke_no_green.png');
})
.mouseout(function() {
this.src = this.src.replace('http://www.expatlifeinsurance.com/images/smoke_no_green.png', 'http://www.expatlifeinsurance.com/images/smoke_no_grey.png');
})
.click(function() {
$(this).toggleClass("off");
if ($(this).hasClass("off")) this.src = this.src.replace(/(.*)-on(.*)/, "$1-off$2");
else this.src = this.src.replace(/(.*)-off(.*)/, "$1-on$2");
}
);
【问题讨论】:
-
请将您的代码添加到问题中。理想情况下,在一个正常工作的 sn-p 中,这样我们就可以看到问题。
-
另请注意,您可以在纯 CSS 中实现这一点:codepen.io/jahid-webdev/pen/JRPvBj,但请注意,如果您需要 IE 支持,您需要修改它以使用 IE specific filter for greyscale
-
您可以在悬停和单击时使用 jquery 更改它,看看这篇文章。 stackoverflow.com/questions/23959132/…
-
@MohamedElgarnaoui 我已经尝试过这段代码,但它对我不起作用。请参阅上面我将编辑问题。
-
请添加您的代码
标签: jquery