


JavaScript study notes one: jQuery writing method, image scaling and preloading_jquery
One of the JavaScript study notes: jQuery writing method, image scaling and preloading
In the past, when using JavaScript, I always wrote a few functions on the page, and basically did not consider the encapsulation and reuse of functions. Recently, there has been This project may have high requirements in this regard, so I researched encapsulation similar to jQuery.
Here, let’s try the effects of image scaling and preloading, and write similar JavaScript code.
The effect of image scaling and preloading is as follows (the preloading effect is sometimes not very obvious):
The main JS code is as follows:
(function() {
var yQuery = (function() {
var yQuery = function () {
return yQuery.fn.init();
};
yQuery.fn = yQuery.prototype = {
init: function() {
return this;
},
//Image proportional scaling and preloading method declaration, but I feel that writing it like this (return new imgResizeBox(e)) is very awkward. Please let me know.
imgResize: function(e) {
return new imgResizeBox( e);
}
};
//image image processing
var imgResizeBox = function(e) {
//image parameter
setting = {
imgId: " ", //The ID of the image container, such as .viewArea img
height: 0,
width: 0,
loading: "images/lightbox-ico-loading.gif"
};
$.extend(setting, e, setting); //Parameter replacement
var images = $(setting.imgId); //Get all images
$(images).hide(); //Hide
var loading = new Image(); //Preload images
loading.className = "loading";
loading.src = setting.loading;
$(images).after(loading);
//Preloading function
var perLoading = function($this) {
var img = new Image();
img.src = $this.src;
if (img. complete) {
computeImg.call($this);
return;
};
img.onload = function() {
computeImg.call($this);
img .onload = function() { };
};
};
//Picture scaling processing, and picture display function
var computeImg = function() {
var m = this.height - setting.height;
var n = this.width - setting.width;
if (m > n)
this.height = this.height > setting.height ? setting .height : this.height;
else
this.width = this.width > setting.width ? setting.width : this.width;
$(this).next(".loading") .remove();
$(this).show();
};
//Call the preloading function in a loop
return $(images).each(function() {
perLoading(this);
});
}
return yQuery;
})();
window.yQuery = window.$$ = yQuery();
}) ();
The calling code is as follows:
$(document).ready(function()
{
$$.imgResize({ imgId: ".viewArea img", height:160, width:270, loading: "http:// www.jb51.net/images/2012/155618/2012062710243954.gif" });
});
Finally, the simple source code is attached: jsDemo_jb51.rar

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to handle image caching and preloading in Vue? When developing Vue projects, we often need to deal with caching and preloading of images to improve website performance and user experience. This article will introduce some methods of handling image caching and preloading in Vue, and give corresponding code examples. 1. Image caching uses image lazy loading (LazyLoading) Image lazy loading is a technology that delays loading images, that is, the image is not loaded until the page scrolls to the location of the image. This reduces requests for image resources when the page is first loaded

As web applications become increasingly complex, front-end developers need to better provide functionality and user experience while ensuring page loading speeds. This involves lazy loading and preloading of Vue components, which are important means to optimize the performance of Vue applications. This article will provide an in-depth introduction to the implementation methods of lazy loading and preloading of Vue components. 1. What is lazy loading? Lazy loading means that the code of a component will only be loaded when the user needs to access it, instead of loading the code of all components at the beginning. This can reduce

UniApp's design and development skills for image processing and preloading Introduction: In mobile application development, image processing and preloading are common requirements. As a cross-platform development framework, UniApp provides convenient and fast image processing and preloading functions. This article will introduce the design and development techniques for image processing and preloading in UniApp, and give corresponding code examples. 1. Design and development of image processing Zooming pictures In UniApp, to zoom pictures, you can use <uni-ima

In web development, image preloading is a common technology that can improve the user experience. When users browse the web, images can be downloaded and loaded in advance, reducing the waiting time for image loading. In the Vue framework, we can implement image preloading through some simple methods. This article will introduce the image preloading technology in Vue, including the principle of preloading, implementation methods and usage precautions. 1. The principle of preloading First, let’s understand the principle of image preloading. The traditional image loading method is to wait until all images are downloaded before displaying them.

How to handle lazy loading and preloading of image resources in Vue technology development. With the enrichment of web page content, images have become an indispensable part of web pages. However, loading a large number of image resources may cause the webpage to load slowly and affect the user experience. In order to solve this problem, we can use lazy loading and preloading technology of image resources to optimize the user experience. 1. Lazy loading technology Lazy loading means that when the images in the web page are first loaded, only the images in the visible area are loaded. When the user scrolls the page and reaches the area where the image is located, the image is loaded again.

With the rapid development of the Internet, modern society has become inseparable from various websites. However, for website developers and operators, a high-performance website is crucial. A responsive website that loads quickly not only provides a better user experience but also improves SEO rankings. This article will introduce five key techniques to help improve website performance. First of all, compressing web content is an important part of improving website performance. Most websites contain a lot of front-end resources, such as HTML, CSS, JavaScript

PHP development cache preheating and preloading requires specific code examples. With the rapid development of the Internet and mobile applications, users have increasingly higher requirements for the response speed of websites and applications. In order to improve user experience, caching has been widely used in website and application development. In PHP development, cache preheating and preloading are important links to improve performance. This article will introduce the concepts of cache preheating and preloading in PHP development, and give specific code examples. 1. Cache warm-up Cache warm-up means before system startup or data update,

Amap is a navigation software that everyone often uses when traveling. It has a function of preloading pictures along the way. Some friends are not very familiar with this. Here is how to enable this function. After opening the Amap app on your phone, go to the "My" option in the lower right corner and tap the "Hexagon" settings icon in the upper right corner to open the settings page. 2. After coming to the settings page, there is a "Footprint Settings", click on it to enter. 3. Next, find "Preload photos along the way" in the footprint settings page. There is a switch button displayed behind it. Click the slider on it to set it to blue to turn on the function. The process of using Amap You can add photos along the way with one click.
