We recommend using the Javascript image shrinking function (onDOMLoaded )......

Source: Internet
Author: User

070520: Norman gave Dean Edwards and other cross-browser solutions for onDOMLoaded events ......
In fact, onDOMLoaded is a "nonexistent" event when the DOM is loaded (the image has not been loaded ......
Different browsers have different support levels ......
So we need to perform Hack ......
Fortunately, Dean and some other friends have repeatedly studied and provided solutions ......
Thank you ......
The improvement of the thumbnail itself is to merge two different modes of the previous version (fast and smooth )......
The so-called fast mode uses the while loop ......
Fast but prone to false positives ......
The so-called gentle mode uses the setInterval function to solve the above problems ......
Unfortunately, setInterval has the shortest cycle limit ......
So the speed is slow ......
The threshold for switching from fast to smooth mode is set to 500 ......
That is, when the image size of a page reaches 500, the smooth mode is enabled to scale down the image ......
Everyone can make changes as needed ......
For details, see the second line of the resizeImgs function ......

070516: Thanks to Norman for his suggestions on improving Code performance ......
Change all elements in the traversal to an image array ......
As for the function proposed by Norman to scale down images before reading images ......
Not implemented ......
This is because Firefox does not support onreadystatechange events (only onload ?)......
Second, in the test, we found that in the interactive state, the image size needs to be refreshed before it can be executed ......
That is to say, you have to wait until the page is loaded (or once) before you can operate ......
Forgive me for not learning too much ......
If you have any solutions, please leave a message to the professor ......

How it works ......
Is to traverse the Image array ......
If the resize attribute is defined, scale down the image ......
When the value of resize is a number (and smaller than the original image width), it is reduced to this digital width ......
When resize is another value, it is reduced to the preset width ......
As for why to clear the height attribute ......
Answer: to maintain the ratio of width to height ......
Above ......

Because the code is too long ......
Do not post ......
For usage instructions, see Demo ......
Http://www.jb51.net/test/resizeIMGs_1.2/index.html
Local download

Related Article

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.