มีวิธีที่เชื่อถือได้และเป็นอิสระจากกรอบในการกำหนดมิติทางกายภาพของการ<img src='xyz.jpg'>
ปรับขนาดในฝั่งไคลเอ็นต์หรือไม่?
มีวิธีที่เชื่อถือได้และเป็นอิสระจากกรอบในการกำหนดมิติทางกายภาพของการ<img src='xyz.jpg'>
ปรับขนาดในฝั่งไคลเอ็นต์หรือไม่?
คำตอบ:
คุณมี 2 ตัวเลือก:
ตัวเลือกที่ 1:
ถอดwidth
และheight
คุณลักษณะและอ่านoffsetWidth
และoffsetHeight
ทางเลือกที่ 2:
สร้างImage
วัตถุJavaScript ตั้งค่าsrc
และอ่านwidth
และheight
(คุณไม่จำเป็นต้องเพิ่มลงในหน้าเพื่อดำเนินการนี้)
function getImgSize(imgSrc) {
var newImg = new Image();
newImg.onload = function() {
var height = newImg.height;
var width = newImg.width;
alert ('The image size is '+width+'*'+height);
}
newImg.src = imgSrc; // this must be done AFTER setting onload
}
แก้ไขโดย Pekka : ตามที่ตกลงไว้ในความคิดเห็นฉันเปลี่ยนฟังก์ชันให้ทำงานในเหตุการณ์ ononload´ ของรูปภาพ ไม่งั้นมีภาพใหญ่height
และwidth
จะไม่คืนอะไรเลยเพราะยังไม่ได้โหลดภาพ
newImg.onload
ฟังก์ชั่นเพื่อให้แน่ใจว่าภาพถูกโหลดเมื่อฉันตั้งค่าความกว้าง / ความสูง คุณตกลงให้ฉันแก้ไขคำตอบของคุณตามนั้นหรือไม่
if(newImg.complete || newImg.readyState === 4) newImg.onload();
ในตอนท้ายของฟังก์ชันของคุณมันจะแก้ไขปัญหาบน Chrome / OSX ที่ทำให้ onload ไม่ทำงานเมื่อโหลดรูปภาพจากแคช
รูปภาพ (อย่างน้อยบน Firefox) มีnaturalWidth
คุณสมบัติa / height เพื่อให้คุณสามารถใช้img.naturalWidth
เพื่อรับความกว้างดั้งเดิมได้
var img = document.getElementsByTagName("img")[0];
img.onload=function(){
console.log("Width",img.naturalWidth);
console.log("Height",img.naturalHeight);
}
คุณสามารถโหลดรูปภาพไว้ในวัตถุรูปภาพจาวาสคริปต์จากนั้นตรวจสอบคุณสมบัติความกว้างและความสูงของวัตถุนั้น
/* Function to return the DOM object's in crossbrowser style */
function widthCrossBrowser(element) {
/* element - DOM element */
/* For FireFox & IE */
if( element.width != undefined && element.width != '' && element.width != 0){
this.width = element.width;
}
/* For FireFox & IE */
else if(element.clientWidth != undefined && element.clientWidth != '' && element.clientWidth != 0){
this.width = element.clientWidth;
}
/* For Chrome * FireFox */
else if(element.naturalWidth != undefined && element.naturalWidth != '' && element.naturalWidth != 0){
this.width = element.naturalWidth;
}
/* For FireFox & IE */
else if(element.offsetWidth != undefined && element.offsetWidth != '' && element.offsetWidth != 0){
this.width = element.offsetWidth;
}
/*
console.info(' widthWidth width:', element.width);
console.info(' clntWidth clientWidth:', element.clientWidth);
console.info(' natWidth naturalWidth:', element.naturalWidth);
console.info(' offstWidth offsetWidth:',element.offsetWidth);
console.info(' parseInt(this.width):',parseInt(this.width));
*/
return parseInt(this.width);
}
var elementWidth = widthCrossBrowser(element);
element
การเลือก jQuery? แล้วความสูงล่ะ?
เพียงแค่เปลี่ยนตัวเลือกที่สองของ Gabriel เล็กน้อยเพื่อให้ใช้งานได้ง่ายขึ้น:
function getImgSize(imgSrc, callback) {
var newImg = new Image();
newImg.onload = function () {
if (callback != undefined)
callback({width: newImg.width, height: newImg.height})
}
newImg.src = imgSrc;
}
Html:
<img id="_temp_circlePic" src="http://localhost/myimage.png"
style="width: 100%; height:100%">
โทรตัวอย่าง:
getImgSize($("#_temp_circlePic").attr("src"), function (imgSize) {
// do what you want with the image's size.
var ratio = imgSize.height / $("#_temp_circlePic").height();
});
img.onload = function () {console.log(img.height, img.width)}