Mobile Web Development Picture Adaptive two common situation solutions

Source: Internet
Author: User
<span id="Label3"></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">When doing the Web WAP page with the mobile client, it is found that there are two special situations in the demand of the picture display, one is for the atlas, this kind of article only need to swipe left and right to browse, the best experience is to let the picture zoom display in the screen valid range, The user experience is greatly reduced by preventing the picture from being too large causing the user to swipe their finger to move the picture to see this laborious thing. The second is the picture and text of the article, the maximum width of the image does not exceed the screen width, height can auto. Both of these situations are common in projects. In addition, some people say to do a picture cutting tool, the picture size ratio is set to a uniform size, but even so, in the face of various sizes of mobile device screen, is not applicable to a unified solution can be solved. And if there is too much demand, how many different sizes of images will be stored on the server? The display is less realistic.</p></p> <p><p><br><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">The following is the Atlas type, demand side requirements picture aspect are kept in the phone visual field of view, JS code listed Below:</span></p></p> <pre class="brush:css;toolbar: true; auto-links: false;">$ (function () {var imglist =document.getelementsbytagname ("img");//android 4.0+ The window.screen.width is not supported in the high-level version, and the Android 2.3.3 system supports/*var _width = window.screen.width;var _height  = window.screen.height - 20;var _width = document.body.clientwidth;var _ Height = document.body.clientheight - 20;*/var _width, _height;dodraw (); Window.onresize = function () {dodraw ();} Function dodraw () {_width = window.innerwidth;_height = window.innerheight -  20;for ( var i = 0, len = imglist.length; i < len;  i++) {DrawImage (imglist[i],_width,_height);}} Function drawimage (imgd,_width,_height) { var image=new image (); image.src=ImgD.src;  image.onload = function () {if (image.width>30 && image.height>30) {   if (image.width/image.height>= _width/_height) { if (iMage.width>_width) {imgd.width=_width; imgd.height= (image.height*_width)/image.width; }else{  Imgd.width=image.width; imgd.height=image.height; } }else{ if (image.height>_height) { Imgd.height=_height; imgd.width= (image.width*_height)/image.height; }else{ imgd.width= image.width; imgd.height=image.height; } }}}}   });</pre> <p><p> <span style="color:rgb (wuyi, wuyi, wuyi); font-family:arial; font-size:14px; line-height:26px; Background-color:rgb (255, 255, 255); "> > Note: The test found that the Android 4.0+ system does not support the Window.screen.width attribute, and in many cases the screen pixels returned during the first load are Incorrect. My Android 2.3.3 system was tested and supported by this Property. It is said that this is an android bug, you can set the delay time by settimeout to solve this problem. however, This method, i how to test is not feasible. So let's just find another smart One. Found that window.innerwidth can take this responsibility, no compatibility problems found, Ok. </span> <br> <br> <br> <br> <span style="color:rgb (wuyi, wuyi); font-family:arial; font-size:14px; line-height:26px; backg Round-color:rgb (255, 255, 255); "> Below is the second case, an illustrated article Type. At this time only the width of the picture and the width of the phone to adapt to the requirements, the height of the restrictions, relatively easy. </span> <br> <span style="color:rgb (wuyi, wuyi); font-family:arial; font-size:14px; line-height:26px; background-color: RGB (255, 255, 255); "> Transform the above JavaScript code, as follows: </span> </p></p> <pre class="brush:css;toolbar: true; auto-links: false;"><pre class="brush:css;toolbar: true; auto-links: false;">$ (function () {var imglist =document.getelementsbytagname ("img")////android 4.0+ and other versions do not support window.screen.width, Android 2.3.3 system support Var _width;dodraw (); window.onresize = function () {//capture screen window changes, always ensure that the picture is displayed reasonably according to the screen width Dodraw ();} function Dodraw () {_width = window.innerwidth;for (var i = 0, len = imglist.length; i < len; i++) {DrawImage (imglist[i],_ width);}} function DrawImage (imgd,_width) {var image=new image (); image.src=imgd.src; image.onload = function () {//limit, Display processing if (image.width>30 && image.height>30) {if (image.width>_width) {imgd.width=_width) only for pictures with a width of greater than 30 ; imgd.height= (image.height*_width)/image.width; }else{imgd.width=image.width; imgd.height=image.height; } }}}   })</pre></pre><p><p>Mobile Web Development Picture Adaptive two common situation solutions</p></p></span>

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.