Home Web Front-end JS Tutorial JavaScript study notes one: jQuery writing method, image scaling and preloading_jquery

JavaScript study notes one: jQuery writing method, image scaling and preloading_jquery

May 16, 2016 pm 05:52 PM
Preloading

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:

Copy code The 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:
Copy code The 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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to handle image caching and preloading in Vue? How to handle image caching and preloading in Vue? Aug 25, 2023 pm 04:21 PM

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

How does Vue implement lazy loading and preloading of components? How does Vue implement lazy loading and preloading of components? Jun 27, 2023 pm 03:24 PM

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 design and development skills for image processing and preloading UniApp design and development skills for image processing and preloading Jul 04, 2023 pm 05:45 PM

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

How to use Vue to implement image preloading? How to use Vue to implement image preloading? Jun 25, 2023 am 11:01 AM

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 How to handle lazy loading and preloading of image resources in Vue technology development Oct 09, 2023 am 09:45 AM

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.

Five key tips for optimizing website performance Five key tips for optimizing website performance Feb 02, 2024 pm 09:13 PM

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 warm-up and pre-loading PHP development cache warm-up and pre-loading Nov 07, 2023 pm 03:30 PM

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,

How to preload pictures along the way in Amap How to preload pictures along the way in Amap Mar 01, 2024 pm 12:58 PM

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.

See all articles