简体   繁体   中英

How to change image height and width with javascript

<div id="Poppen" style="float: right">
     <script language="JavaScript">
            function random_imglink(){
               var myimages=new Array()
               //specify random images below. You can have as many as you wish
               myimages[1]="Kleren/Kleren1.jpg"
               myimages[2]="Kleren/Kleren2.jpg"
               myimages[3]="Kleren/Kleren3.jpg"
               myimages[4]="Kleren/Kleren4.jpg"
               myimages[5]="Kleren/Kleren5.jpg"

               //specify corresponding links below
               var ry=Math.floor(Math.random()*myimages.length)
               if (ry==0)
                 ry=1
                 document.write('<img src="'+myimages[ry]+'" border=0>')
            }
            random_imglink()
           //-->
    </script>
</div>

</body>
</html>

This is a javascript code to change my images when the page is refreshed But those images are too large and I can't find how to make them smaller?

You could use height and width attributes

height="xx" width="xx"

add those attr at

document.write('<img src=myimages[ry] width=xx height=xx> border=0>')

for more info check the reference

In your img tag you can add the attr height and width. You can change the xxx into 50px or whatever height and width.

document.write('<img src="'+myimages[ry]+'" border=0 style="height:xxx;width:xxx;">')

Javascript:

document.getElementsByTagName("img").style.height="50px";

Or simple css.

#Poppen img {width: 100px; height:100px;}

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