javascript 獲取圖片尺寸及放大圖片
更新時(shí)間:2013年09月04日 08:44:06 作者:
獲取圖片尺寸(不設(shè)置寬高)及放大圖片:利用了IE的私有屬性防止圖片放大失真嚴(yán)重!感興趣的朋友可以參考下
1)獲取圖片尺寸
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)" width="200" height="300"/>
<script>
function getWH(t){
//DOM屬性
console.log("width="+t.width);//200
console.log("height="+t.height);//300
//操作樣式
console.log("styleWidth="+t.style.width);//空
console.log("styleHeight="+t.style.height);//空
}
2)獲取圖片尺寸(不設(shè)置寬高)
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)"/>
<script>
function getWH(t){
//DOM屬性
console.log("width="+t.width);//200
console.log("height="+t.height);//300
//操作樣式
console.log("styleWidth="+t.style.width);//空
console.log("styleHeight="+t.style.height);//空
}
我們只要不在style中顯式地設(shè)置它,寬高永遠(yuǎn)為空!
3)放大圖片:
這里我們利用了IE的私有屬性防止圖片放大失真嚴(yán)重!
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)" width="200" height="300"/>
<script>
function getWH(t){
t.width *= 2;
t.height *= 2;
//每點(diǎn)擊一次,寬高放大一倍
}
</script>
4)在FF與谷歌中,我們還可以用naturalWidth與naturalHeight取得圖片的原大??!
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)" width="200" height="300"/>
<script>
function getWH(t){
console.log("width="+t.naturalWidth);
console.log("height="+t.naturalHeight);
t.width = t.naturalWidth * 2;
t.height = t.naturalHeight * 2;
}
</script>
naturalWidth和naturalHeight只是只讀屬性,不能用來(lái)設(shè)置圖片的大小,不能持續(xù)放大。
復(fù)制代碼 代碼如下:
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)" width="200" height="300"/>
<script>
function getWH(t){
//DOM屬性
console.log("width="+t.width);//200
console.log("height="+t.height);//300
//操作樣式
console.log("styleWidth="+t.style.width);//空
console.log("styleHeight="+t.style.height);//空
}
2)獲取圖片尺寸(不設(shè)置寬高)
復(fù)制代碼 代碼如下:
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)"/>
<script>
function getWH(t){
//DOM屬性
console.log("width="+t.width);//200
console.log("height="+t.height);//300
//操作樣式
console.log("styleWidth="+t.style.width);//空
console.log("styleHeight="+t.style.height);//空
}
我們只要不在style中顯式地設(shè)置它,寬高永遠(yuǎn)為空!
3)放大圖片:
這里我們利用了IE的私有屬性防止圖片放大失真嚴(yán)重!
復(fù)制代碼 代碼如下:
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)" width="200" height="300"/>
<script>
function getWH(t){
t.width *= 2;
t.height *= 2;
//每點(diǎn)擊一次,寬高放大一倍
}
</script>
4)在FF與谷歌中,我們還可以用naturalWidth與naturalHeight取得圖片的原大??!
復(fù)制代碼 代碼如下:
<img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(實(shí)際大小200*300)" onclick="getWH(this)" width="200" height="300"/>
<script>
function getWH(t){
console.log("width="+t.naturalWidth);
console.log("height="+t.naturalHeight);
t.width = t.naturalWidth * 2;
t.height = t.naturalHeight * 2;
}
</script>
naturalWidth和naturalHeight只是只讀屬性,不能用來(lái)設(shè)置圖片的大小,不能持續(xù)放大。

相關(guān)文章
實(shí)例代碼詳解javascript實(shí)現(xiàn)窗口抖動(dòng)及qq窗口抖動(dòng)
這篇文章主要介紹了實(shí)例代碼詳解javascript實(shí)現(xiàn)窗口抖動(dòng)及qq窗口抖動(dòng)的相關(guān)資料,需要的朋友可以參考下2016-01-01JS根據(jù)瀏覽器窗口大小實(shí)時(shí)動(dòng)態(tài)改變網(wǎng)頁(yè)文字大小的方法
這篇文章主要介紹了JS根據(jù)瀏覽器窗口大小實(shí)時(shí)動(dòng)態(tài)改變網(wǎng)頁(yè)文字大小的方法,涉及JavaScript針對(duì)頁(yè)面寬高的動(dòng)態(tài)獲取與元素樣式動(dòng)態(tài)運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2016-02-02js局部刷新頁(yè)面時(shí)間具體實(shí)現(xiàn)
這篇文章介紹了js局部刷新頁(yè)面時(shí)間具體實(shí)現(xiàn),需要的朋友可以參考一下2013-07-07javascript 實(shí)現(xiàn)字符串反轉(zhuǎn)的三種方法
這篇文章主要介紹了javascript 實(shí)現(xiàn)字符串反轉(zhuǎn)的三種方法,有需要的朋友可以參考一下2013-11-11layui問(wèn)題之渲染數(shù)據(jù)表格時(shí),僅出現(xiàn)10條數(shù)據(jù)的解決方法
今天小編就為大家分享一篇layui問(wèn)題之渲染數(shù)據(jù)表格時(shí),僅出現(xiàn)10條數(shù)據(jù)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09利用js編寫(xiě)網(wǎng)頁(yè)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了利用js編寫(xiě)網(wǎng)頁(yè)進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10