简体   繁体   中英

How to use addEventListener for image properly?

I like to measure the width of an image, which was loaded by user click. I need the width to adjust the position button next to the image. Is this possible to do in Javascript?

here is my code:

document.getElementById('preIMG').addEventListener("load", posButton2IMG); 


function posButton2IMG(){
   var imgWidth = document.getElementById('preIMG').clientWidth;
   var imgWidPos = document.getElementById('picture_preview').clientWidth - imgWidth + 15;
   document.getElementById('gear_butto').style.right = (imgWidPos*0.5) + "px";
}  

edit: In fact the user can explore several images and addEventListener seems only working for the first time the side is load.

不要在click事件中调用函数,只需传递函数JavaScript将在触发click事件时调用您的函数

document.getElementById('preIMG').addEventListener("load", posButton2IMG); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM