Load network unknown size picture, auto-zoom and horizontal, vertical center display

Source: Internet
Author: User

 Personal Essays
The code implements the following functions:
1, loading the network unknown size of the picture, the user can freely enter the size of the display area, the picture will automatically adapt to the wide height.
2, horizontal, vertical center image of unknown size

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Horizontally and vertically centers an unknown size picture</title>6 <style>7 #imageBox{8 width:250px;9 Height:250px;Ten margin:30px Auto; One Border:1px solid Green; A Display:Table; /**key point**/ - text-align:Center; - } the - . Spacespan{ - Height:100%; - Display:Table-cell; /**key point**/ + vertical-align:Middle; /**key point**/ - } + A #testImage{ at vertical-align:Middle; - } - - </style> - <Script> - functionzoomimage (ImageId, Imageboxwidth, imageboxheight) { in varImage=document.getElementById (imageId); - //calculates the aspect ratio of the picture container to varImageboxwidthheightscale=Imageboxwidth/Imageboxheight; + //calculate the aspect ratio of the actual picture - varImagewidthheightscale=Image.width/Image.height; the varWidthscale=Image.width/Imageboxwidth; * varHeightscale=Image.height/Imageboxheight; $ varMaxscale=Math.max (Widthscale, heightscale);Panax Notoginseng if(Maxscale< 1) { //picture width is higher than box hour not picture operation - return NULL; the } Else { //the width of the picture or the height of the box large hours + if(Imageboxwidthheightscale>Imagewidthheightscale) { //The box is more "flat" relative to the picture, at which point the height of the picture is controlled A Image.height*=Heightscale> 1 ? 1 /Heightscale:heightscale; the } Else { //The picture is more "flat" relative to the box, which controls the width of the picture. + Image.width*=Widthscale> 1 ? 1 /Widthscale:widthscale; - } $ } $ } - </Script> - </Head> the <Body> - <DivID= "Imagebox">Wuyi <spanclass= "Spacespan"> the <ImageID= "Testimage" - src= "Http://daily.clzg.cn/res/1/20160311/75231457630062963.jpg" Wu onload= "Zoomimage (' Testimage ', 200,200)"/> - </span> About </Div> $ </Body> - </HTML>

Actual results such as:



Load network unknown size picture, auto-zoom and horizontal, vertical center display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.