簡體   English   中英

javascript動態內容加載后無法動態調整IMG的大小

[英]Can't dynamic resize IMG after javascript dynamic content load

我從dynamicdrive使用此javascript將內容加載到div中:

    var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
    var loadedobjects=""
    var rootdomain="http://"+window.location.hostname
    var bustcacheparameter=""

    function ajaxpage(url, containerid){
        var page_request = false
        if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest();
        else if (window.ActiveXObject){ // if IE
            try {
                page_request = new ActiveXObject("Msxml2.XMLHTTP");
                }
            catch (e){
                try{
                    page_request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                catch (e){}
            }
        }
        else
        return false
        page_request.onreadystatechange=function(){
            loadpage(page_request, containerid)
        }

        if (bustcachevar) //if bust caching of external page
        bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
        page_request.open('GET', url+bustcacheparameter, true)
        page_request.setRequestHeader('charset', 'ISO-8859-1')
        page_request.send(null)
    }

    function loadpage(page_request, containerid){
        if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
        document.getElementById(containerid).innerHTML=page_request.responseText
    }

我使用一個稱為“ displayImage('url','width')”的自寫php函數來顯示圖像,然后將其動態調整為指定的寬度。

這在靜態內容上很好用,但是該功能拒絕在動態內容上使用。

誰能在正確的方向上推動我前進? :/我不太擅長javascript ...

編輯:

抱歉,忘記添加php函數,它就這樣了:

function displayImage($img_path, $height) {
$image_info = getimagesize($img_path);  
$new_dimension = imageResize($image_info[0], $image_info[1], $height);
$returnstr = "<IMG SRC=\"{$img_path}\" " . $new_dimension . "/>\n";
return $returnstr;
}

function imageResize($width, $height, $target) {
if ($width > $height) {
$percentage = ($target / $width); 
} else {
$percentage = ($target / $height);
}
$width = round($width * $percentage);
$height = round($height * $percentage);

return "width=\"$width\" height=\"$height\"";

}

PHP是一種服務器端語言,這意味着它可以創建頁面,然后將其發送到瀏覽器。 它無法調整以后添加到頁面的圖像的大小。 您必須使用JS來執行此操作,或者在ajax頁面本身中使用該PHP函數,然后動態加載已縮小的圖像。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM