JS background image varies with browser size and keeps the picture intact

Source: Internet
Author: User

<HTML><Head><style>Body{Overflow:Hidden;  }#pic{margin:0 Auto;width:100%;Height:100%;padding:0;Max-width:100%;position:relative;   }#img{position:Absolute; Left:0px;Top:0px;}</style></Head><Body>    <DivID= "Pic">        <imgsrc=""ID= "img" />    </Div>     <Scripttype= "Text/javascript">window.onresize=function() {resize ();    }; functionResize () {//get the browser's width, including the appropriate Internet Explorer        if(window.innerwidth)varWinwidth=window.innerwidth; Else if((document.body)&&(document.body.clientWidth))varWinwidth=Document.body.clientWidth; if(window.innerheight)varWinheight=Window.innerheight; Else if((document.body)&&(document.body.clientHeight))varWinheight=Document.body.clientHeight; varbg_rate=parsefloat (1920x1080/990);//the aspect ratio of the picture        varbr_rate=parsefloat (Winwidth/winheight);//Browser aspect ratio      //Depending on the aspect ratio, the width of the image is fixed so that the picture fills the screen without distortion, and the extra parts are hidden .        if(Br_rate<bg_rate) {document.getElementById ('img'). Style.height=Winheight+ 'px'document.getElementById ('img'). Style.width= "'; }Else{document.getElementById ('img'). Style.width=Winwidth+ 'px'; document.getElementById ('img'). Style.height= "'; }    }</Script></Body></HTML>

JS background image varies with browser size and keeps the picture intact

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.