Jquery change image on rollover with fade

a 夏天 提交于 2019-12-20 06:31:28

问题


I'm currently using the script below to change an image on rollover. There are many images that this effect needs to be applied to - each image has its own rollover image. The script works for the rollover, but not for the fade.

I've read about using CSS and jquery for the fade effect: http://jqueryfordesigners.com/image-cross-fade-transition/

But it would be a real pain having to write the CSS for over 100 different images! I was just wondering if anyone knows what I'm doing wrong and how to make the script work? :)

$(function () {
$("img.rollover").hover(function () {
    this.src = this.src.replace("_off", "_on")
}, function () {
    this.src = this.src.replace("_on", "_off")
})
});


$("a img").hover(function () {
    jQuery(this).stop().fadeTo("fast", 0.5);
}, function () {
    jQuery(this).stop().fadeTo("fast", 1);
});

回答1:


Instead of writing the CSS for over 100 different images, why not use a jQuery .each loop to add the CSS for you?

$("img.rollover").each(function() {
    this.css('background-image',this.src.replace("_off", "_on"));
});

...and then follow the single-image technique from the page you linked to.



来源:https://stackoverflow.com/questions/7954280/jquery-change-image-on-rollover-with-fade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!