鍍金池/ 問答/HTML5  HTML/ 在ie8中使用 jq的load()無反應

在ie8中使用 jq的load()無反應

$("<img/>").attr("src", path).load(function(){
    console.log('點擊888888');
    alert('!!!!!!!!!!!!!!!!!!!!!!!!!!')
    var windowW = $(window).width();        //獲取當前窗口寬度
    var windowH = $(window).height();       //獲取當前窗口高度
    var realWidth = this.width;     //獲取圖片真實寬度
    var realHeight = this.height;       //獲取圖片真實高度
    var imgWidth, imgHeight;
    var scale = 0.8;        //縮放尺寸,當圖片真實寬度和高度大于窗口寬度和高度時進行縮放

    if(realHeight > windowH * scale) {  //判斷圖片高度
        console.log('點擊999999');
        imgHeight = windowH * scale;  //如大于窗口高度,圖片高度進行縮放
        imgWidth = imgHeight / realHeight * realWidth;  //等比例縮放寬度
        if(imgWidth > windowW * scale) {    //如寬度扔大于窗口寬度
            imgWidth = windowW * scale;   //再對寬度進行縮放
        }
    } else if(realWidth > windowW * scale) {    //如圖片高度合適,判斷圖片寬度
        imgWidth = windowW * scale;       //如大于窗口寬度,圖片寬度進行縮放
        imgHeight = imgWidth / realWidth * realHeight;  //等比例縮放高度
    } else {    //如果圖片真實高度和寬度都符合要求,高寬不變
        imgWidth = realWidth;
        imgHeight = realHeight;
    }
    $(bigimg).css("width",imgWidth);    //以最終的寬度對圖片縮放
    var w = (windowW-imgWidth)/2;   //計算圖片與窗口左邊距
    var h = (windowH-imgHeight)/2;  //計算圖片與窗口上邊距
    $(innerdiv).css({"top":h, "left":w});   //設置#innerdiv的top和left屬性
    $(outerdiv).fadeIn("fast"); //淡入顯示#outerdiv及.pimg
});
回答
編輯回答
離魂曲

可以使用load()方法,但是path再ie8中不能寫為動態(tài)

if(/(MSIE 8.0)|(MSIE 7.0)/ig.test(ua)){
    $("<img/>").attr("src",pathShow);    // pathShow為圖片路徑,load()方法在ie8及以下不能為動態(tài)地址
    $("<img/>").css("width");
    if( $("<img/>") ){
        $('#innerdiv').css({
            'height':'100%',
            'width':'100%',
            'background':'#000000',
            'opacity':'0.3'
        });
        $(outerdiv).fadeIn("fast");     //淡入顯示#outerdiv及.pimg
    }
}

即可

2018年5月19日 11:24