


Two JavaScript solutions for image adaptation in mobile Web_javascript skills
This article mainly talks about the solutions to two common situations: adaptive center display of images on the Web according to the size of the mobile phone screen, and adaptive image adaptation. Let’s get started
When working on a Web wap page that works with mobile clients, I found that there are two particularly important situations where articles require image display. One is for atlases. This kind of article only needs to be browsed by swiping left and right. The best experience is to have the images zoom and display. Within the effective range of the screen, it prevents the user from having to slide their finger to move the image to view the image because it is too large, which greatly reduces the user experience. The second is articles with mixed graphics and text. The maximum width of the picture does not exceed the width of the screen, and the height can be auto. Both situations are common in projects. In addition, some people talk about making a picture cutting tool and setting the picture size ratio to a unified size, but even so, facing mobile device screens of various sizes, it is impossible to apply a unified solution. And if there are too many demands, how many images of different sizes must be stored on the server? The display is not very realistic.
The following is the type of album. The demander requires that the height and width of the images should be kept within the visual field of view of the mobile phone. The js code is listed below:
<script type="text/javascript"> $(function(){ var imglist =document.getElementsByTagName("img"); //安卓4.0+等高版本不支持window.screen.width,安卓2.3.3系统支持 /* 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; } } } } } }) </script>
Note: During the test, it was found that the Android 4.0 system does not support the window.screen.width property well. In many cases, the screen pixels returned when loading for the first time are incorrect. My Android 2.3.3 system passed the test and supports this attribute. It is said that this is a bug in the Android system, and this problem can be solved by setting the delay time through setTimeout. However, this method does not work no matter how I test it. So I might as well find another solution. I found that window.innerWidth can take on this important task, and no compatibility issues were found, ok.
The following is the second case, an article type with pictures and texts. At this time, only the width of the picture and the width of the mobile phone are required, and there is no limit on the height, which is relatively easy.
Transform the above javascript code as follows:
<script type="text/javascript"> $(function(){ var imglist =document.getElementsByTagName("img"); //安卓4.0+等高版本不支持window.screen.width,安卓2.3.3系统支持 var _width; doDraw(); window.onresize = function(){ //捕捉屏幕窗口变化,始终保证图片根据屏幕宽度合理显示 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(){ //限制,只对宽高都大于30的图片做显示处理 if(image.width>30 && image.height>30){ if(image.width>_width){ ImgD.width=_width; ImgD.height=(image.height*_width)/image.width; }else{ ImgD.width=image.width; ImgD.height=image.height; } } } } }) </script>
Note: The resize function in the code captures changes in the screen window and always ensures that the image is displayed reasonably according to the screen width. Of course, the premise is that like my project, the article is directly in rich text format, and the parent tag of the image has set the centering attribute of text-align:center. If the content of your article directly calls a third party, then you can add the corresponding processing statement to the above javascript code.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
