導航:首頁 > 編程語言 > js獲取圖片寬度

js獲取圖片寬度

發布時間:2021-03-05 16:01:43

『壹』 js如何獲取圖片顯示時的大小尺寸

給img添加載入事件,圖像載入完成才能獲取圖片的尺寸。
html中添加回onload
function showSize(img){
alert(img.width);
alert(img.height);
}
<div style="width:100px;height:600px"><img src="1.jpg" onload="showSize(this)"></div>
或者答js代碼中添加onload
let img=document.querySelector("div img");
img.onload = function() {
alert(img.height);
alert(img.width);
}

『貳』 想做一個JS來獲取圖片的寬度跟高度

<img id="t" src="圖片地址" />
<script>
var img = document.getElementById("t")
var temp = new Image();
temp.onload = function(){
var style = img.style,
ratio = Math.min( 1,
Math.max( 0, 100 ) / this.width || 1,
Math.max( 0, 100 ) / this.height || 1
);
//設置預覽版尺權寸
style.width = Math.round( this.width * ratio ) + "px";
style.height = Math.round( this.height * ratio ) + "px";
}
temp.src = img.src;
</script>

『叄』 我想用js獲取一張圖片的寬度和高度 請問怎麼能獲取 圖片在<img>標簽中

||<img id="_img" src="abc.jpg" >

<script type="text/javascript">
function getWH(){
var id="_img";
var w=0;
var h=0;
// 具有較好的通用性內
var real= document.getElementById(id).height ||容 document.getElementById(id).style.height || document.getElementById(id).offsetHeight;
h=parseInt(real);
real= document.getElementById(id).width || document.getElementById(id).style.width || document.getElementById(id).offsetWidth;
w=parseInt(real);
alert("w:"+w+" ; h:"+h)
}
getWH();

</script>

『肆』 怎麼用js獲取圖片寬高並寫入html代碼

<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metacharset="utf-8"/>
<title>獲取Element中圖片的屬性</title>
</head>
<body>

<imgsrc="https://gss0.bdstatic.com/7051cy792sgCpNKfpU_Y_D3/static/common/widget/search-box-new/img/logo-new-101_bba4ab1.png"/>

<!--圖片信息容器-->
<div></div>

<scripttype="text/javascript">
varimg=document.images[0];
vardiv=document.getElementsByTagName('div')[0];
//向div容器寫入圖片信息
div.innerHTML='圖片的寬度是:'+img.width+'圖片的高度是:'+img.height;
</script>
</body>
</html>

『伍』 js怎麼獲取 圖片 的尺寸 和大小 並顯示在html 里

(1)varimg=document.getElementById("img");
varwidth=img.getAttribute("width");
varheight=img.getAttribute("height");
document.getElementById("html").innerHTML("寬度:"+width,"高度:"+height);
(2)上述img為圖片的id,html為需內要現在容在某個位置的id

『陸』 js 獲取圖片的高度和寬度問題

<script>
var image = new Image();
image.src = 'http://www..com/img/logo.gif';
image.onreadystatechange=function(){
if (image.readyState=="complete") {
alert(["圖片大小是:",image.width,image.height]);
}
}
</script>
再試一下呢。。。
應該是圖內片沒有完全載入容吧

『柒』 js怎麼獲取div中圖片的寬高,例如

親,直接獲取該圖片後style.width就可以了,獲取圖片img用getElementsByTagName或者給img加個id用getElementsById獲取

『捌』 js怎麼獲取 這個div內圖片的寬度和

下面是代碼,測試通過

<div style="width:100000px;" id="pal">
<img src="images/2009021121575533977.jpg" width="486" height="348" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
</div>

<SCRIPT>
var obj=document.getElementById("pal");
var imgs=obj.getElementsByTagName("img");
var sum=0;
for(var i=0;i<imgs.length;i++)
sum+=imgs[i].width;
alert(sum);
</SCRIPT>

『玖』 JS獲取圖片寬高,急急急~

var img = new Image();
img.onload = function(){
寬度=this.width
高度=this.height
}
img.src = 圖片地址;

『拾』 js如何獲取圖片的高和寬根據我的部分代碼添加完善,謝謝!

用我的這個代碼你測試下!

<script>
var flag=false;
function DrawImage(ImgD,FitWidth,FitHeight){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= FitWidth/FitHeight){
if(image.width>FitWidth){
ImgD.width=FitWidth;
ImgD.height=(image.height*FitWidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>FitHeight){
ImgD.height=FitHeight;
ImgD.width=(image.width*FitHeight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script>
---------------------------------------------------------------
調用:
<img border=0 src=123.jpg onload="javascript:DrawImage(this,500,400);">
其中:寬=500,高=400 可以修改.

閱讀全文

與js獲取圖片寬度相關的資料

熱點內容
文件加密精靈軟體實驗報告 瀏覽:352
windowold還原win10 瀏覽:775
日本非洲大片 瀏覽:401
matlab繪圖工具 瀏覽:883
網路用語頭部什麼意思 瀏覽:258
iphone5s電話攔截軟體 瀏覽:453
國外電影大胸女人 瀏覽:155
台灣黑社會電影推薦吸毒嫖娼 瀏覽:526
文件系統中源程序是 瀏覽:538
word代碼背景 瀏覽:790
小電影網站有哪些 瀏覽:184
基於大數據的人才畫像 瀏覽:571
越南題材的中國電影 瀏覽:334
台灣紅羊公司出品的電影 瀏覽:261
網路大的未來發展趨勢 瀏覽:949
網路通信科目有哪些 瀏覽:942
有個女同藍頭發短發的電影叫什麼 瀏覽:697
appleshuffle紅色是4s耳機 瀏覽:60
日本生化女的電影 瀏覽:165
國產圖片視頻 瀏覽:479

友情鏈接