Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

jquery - 使用jQuery更改图片来源(Change the image source on rollover using jQuery)

I have a few images and their rollover images.

(我有一些图像及其翻转图像。)

Using jQuery, I want to show/hide the rollover image when the onmousemove/onmouseout event happen.

(我想使用jQuery在onmousemove / onmouseout事件发生时显示/隐藏过渡图像。)

All my image names follow the same pattern, like this:

(我所有的图像名称都遵循相同的模式,如下所示:)

Original Image: Image.gif

(原始图片: Image.gif)

Rollover Image: Imageover.gif

(展期图片: Imageover.gif)

I want to insert and remove the "over" portion of image source in the onmouseover and onmouseout event, respectively.

(我想分别在onmouseover和onmouseout事件中插入和删除图像源的“结束”部分。)

How can I do it using jQuery?

(我该如何使用jQuery?)

  ask by Sachin Gaur translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To set up on ready:

(要准备就绪,请执行以下操作:)

$(function() {
    $("img")
        .mouseover(function() { 
            var src = $(this).attr("src").match(/[^.]+/) + "over.gif";
            $(this).attr("src", src);
        })
        .mouseout(function() {
            var src = $(this).attr("src").replace("over.gif", ".gif");
            $(this).attr("src", src);
        });
});

For those that use url image sources:

(对于使用url图片源的用户:)

$(function() {
        $("img")
            .mouseover(function() {
               var src = $(this).attr("src");
               var regex = /_normal.svg/gi;
               src = this.src.replace(regex,'_rollover.svg');
               $(this).attr("src", src);

            })
            .mouseout(function() {
               var src = $(this).attr("src");
               var regex = /_rollover.svg/gi;
               src = this.src.replace(regex,'_normal.svg');
               $(this).attr("src", src);

            });
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...