Compatible JS image upload preview code

Source: Internet
Author: User

The ability to preview images before uploading them can improve the user experience of your site and make it clear that the images you want to upload are right. This code does not have JS plug-in, pure JavaScript combined with HTML to achieve, is a very good example, relatively complete:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">#preview {width:100px;height:100px;border:1px solid #000; Overflow:hidden;} #imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=image);}</style><script type= "Text/javascript" >functionpreviewimage (file) {varMAXWIDTH = 100; varMaxHeight = 100; vardiv = document.getElementById (' preview '); if(File.files && File.files[0]) {div.innerhtml= '  '; varimg = document.getElementById (' Imghead '); Img.onload=function(){      varRect =Clacimgzoomparam (MAXWIDTH, MaxHeight, Img.offsetwidth, img.offsetheight); Img.width=Rect.width; Img.height=Rect.height; Img.style.marginLeft= rect.left+ ' px '; Img.style.marginTop= rect.top+ ' px '; }    varReader =NewFileReader (); Reader.onload=function(EVT) {IMG.SRC =Evt.target.result;} Reader.readasdataurl (file.files[0]); }  Else  {    varSfilter= ' Filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (sizingmethod=scale,src= "';    File.select (); varsrc =Document.selection.createRange (). text; Div.innerhtml= '  '; varimg = document.getElementById (' Imghead '); Img.filters.item (' DXImageTransform.Microsoft.AlphaImageLoader '). src =src; varRect =Clacimgzoomparam (MAXWIDTH, MaxHeight, Img.offsetwidth, img.offsetheight); Status= (' rect: ' +rect.top+ ', ' +rect.left+ ', ' +rect.width+ ', ' +rect.height); Div.innerhtml= "<div id=divhead style= ' width:" +rect.width+ "Px;height:" +rect.height+ "Px;margin-top:" +rect.top+ "PX; Margin-left: "+rect.left+" PX; " +sfilter+src+ "\" ></div> "; }}functionClacimgzoomparam (MaxWidth, maxheight, width, height) {varparam = {top:0, left:0, Width:width, height:height}; if(Width>maxwidth | | height>maxheight) {Ratewidth= width/MaxWidth; Rateheight= height/MaxHeight; if(Ratewidth >rateheight) {Param.width=MaxWidth; Param.height= Math.Round (Height/ratewidth); }Else{param.width= Math.Round (Width/rateheight); Param.height=MaxHeight; }} param.left= Math.Round ((maxwidth-param.width)/2); Param.top= Math.Round ((maxheight-param.height)/2); returnparam;}</script>

Compatible JS image upload preview code

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.