Monday, March 22, 2010

[js] swap image

/*
    img jQuery object of img tag element
    new img suffix
    1/0 suffix or not
*/
function imgSwap(img, suffix, state) {
    //  ext/. -3 -1
    var img_src = img.attr("src");
    var suffix_pos = img_src.length -4 -suffix.length;
    var img_src_suffix = img_src.substr(suffix_pos, suffix.length);
    var img_src_ext = img_src.substr(img_src.length-4);

    if (state == 1 && img_src_suffix != suffix)
    {
        //prefix + suffix + ext
        img.attr("src", img_src.substr(0, img_src.length-4) + suffix + img_src_ext );
    }
    else if (state ==0 && img_src_suffix == suffix)
    {
        //prefix + ext
        img.attr("src", img_src.substr(0, suffix_pos) + img_src_ext );
    }
}
function a_blur() {
    jQuery("a").focus( function() { this.blur(); return false; } );
}


使用,1是over,0是mouse out
    jQuery(".swap").hover(
        function(){ imgSwap(jQuery(this), "-over", 1) },
        function(){ imgSwap(jQuery(this), "-over", 0) }
    );

No comments: