


Teach you step by step to write a fade-in and fade-out picture carousel plug-in with annotations (2)_javascript skills
Continuing from the previous article, now comes the second part.
Before we start, let’s talk about the optimization problem mentioned above about writing all functions in a closure. As mentioned before, because we only need to call init during initialization, we can only write init into the closure, and other functional functions can be called as the prototype inheritance method of init. So the previous code can actually be rewritten like this:
var Hongru= {};
function H$(id){return document.getElementById(id)}
function H$$(c,p){return p.getElementsByTagName(c)}
Hongru.fader = function (){
function init(options){ //options parameters: id (required): picture list parent tag id; auto (optional): automatic running time; index (optional): starting running picture Serial number
var wp = H$(options.id), // Get the parent element of the picture list
ul = H$$('ul',wp)[0], // Get
li = this .li = H$$('li',ul);
this.a = options.auto?options.auto:2; //Automatic running interval
this.index = options.position?options.position :0; //The picture serial number to start running (starting from 0)
this.l = li.length;
this.cur = this.z = 0; //The currently displayed picture serial number&&z-index variable
this.pos(this.index); //Transformation function
}
init.prototype = {
auto:function(){
this.li.a = setInterval(new Function ('Hongru.fader.move(1)'),this.a*1000);
},
move:function(i){//There are two options for parameter i, 1 and -1,1 It means running to the next one, -1 means running to the previous one
var n = this.cur i;
var m = i==1?n==this.l?0:n:nthis.pos(m); //Convert to the previous or next card Zhang
},
pos:function(i){
clearInterval(this.li.a);
this.z;
this.li[i].style.zIndex = this .z; //Increase the z-index of the next picture by one each time
this.cur = i; //Bind the correct serial number of the currently displayed picture
this.auto(); //Automatically run
}
}
return {init:init}
}();
But this is actually problematic. I don’t know if you will find it if it is rewritten like this. , you can no longer call 'Hongru.fader.move()' in the auto function. This is undefined. Then some people will say that since it is the prototype inheritance of init, then call 'Hongru.fader.init.move() )'Isn't that right? In fact, that's not right. I have discussed this issue in my previous article http://www.cnblogs.com/hongru/archive/2010/10/09/1846636.html; init cannot access it before it is instantiated. prototype, so we need to pay attention to two issues when doing this.
One is to use the new keyword to instantiate init during initialization.
The other is that when calling its prototype method inside the code, it must also be called through the object we instantiated. For example, when we initialize the above code, it should be like this
var newFader = new Hongru.fader.init({ //This new is very important
id:'fader'
});
If we want to call the init method in the code, we need to call it through our new instantiation object newFader, such as the auto function above If you want to call the init's move method, just call 'newFader.move()' directly. This will work.
But there is a small problem, that is, the instantiated variable name must be consistent with the name called in the code. So if I change the name of my initialization object, such as using newFader1, then I have to change the source code. This is definitely not possible, so a little trick is to pass one more parameter in init, make the variable name consistent with the parameter when initializing, and then call it through the parameter in the source code. In this way, the problem is successfully solved.
(ps: The reason why new Function is used in the code is also because it can break the scope chain, which is also one of the conditions to ensure that we can structure our code in this way.)
In summary: the previous code should be like this Optimization:
var Hongru={};
function H$(id){return document.getElementById(id)}
function H$$(c,p){return p.getElementsByTagName(c) }
Hongru.fader = function(){
function init(anthor,options){this.anthor=anthor; this.init(options);}
init.prototype = {
init: function(options){ //options parameters: id (required): picture list parent tag id; auto (optional): automatic running time; index (optional): starting picture serial number
var wp = H$(options.id), // Get the parent element of the picture list
ul = H$$('ul',wp)[0], // Get
li = this.li = H$$( 'li',ul);
this.a = options.auto?options.auto:2; //Automatic running interval
this.index = options.position?options.position:0; //Start running The picture serial number (starting from 0)
this.l = li.length;
this.cur = this.z = 0; //The currently displayed picture serial number&&z-index variable
this.pos( this.index); //Transformation function
},
auto:function(){
this.li.a = setInterval(new Function(this.anthor '.move(1)'),this .a*1000);
},
move:function(i){//There are two options for parameter i, 1 and -1, 1 means running to the next one, and -1 means running to the previous one. Zhang
var n = this.cur i;
var m = i==1?n==this.l?0:n:nthis.pos(m); //Convert to the previous or next picture
},
pos:function(i ){
clearInterval(this.li.a);
this.z;
this.li[i].style.zIndex = this.z; //Let the next picture z- each time index plus one
this.cur = i; //Bind the correct serial number of the currently displayed image
this.auto(); //Automatically run
}
}
return {init: init}
}();
It should be initialized like this:
var fader = new Hongru.fader.init('fader',{ //Make sure the first parameter is consistent with the variable name
id:'fader'
}) ;
Okay, the code optimization plan ends here. The following is the implementation of the second part of the effect: Fade in and fade out effect
In fact, with the good code structure and logic above, it is relatively easy to add the fade in and fade out effect. The idea is very simple. Make the picture transparent before changing, and then pass Timer to gradually increase transparency. But there are several boundary judgments that are more important. At the same time, when changing transparency in IE and non-IE, you should pay attention to using different css attributes.
The changes to the core code are the following two paragraphs. One is to add the transparency gradient function fade(), and the other is to make the image transparent first in pos() --> and then start executing fade()
Add a code segment in pos():
if(this .li[i].o>=100){ //Set the image transparency to transparent before fading in
this.li[i].o = 0;
this.li[i].style .opacity = 0;
this.li[i].style.filter = 'alpha(opacity=0)';
}
this.li[i].f = setInterval(new Function(this .anthor '.fade(' i ')'),20);
Then add a function fade()
fade:function(i){
if(this.li[i].o>=100){
clearInterval (this.li[i].f); //If the transparency change is completed, clear the timer
if(!this.li.a){ //Make sure all timers are cleared before starting automatic operation. Otherwise, if the controller is clicked too fast, the timer will start the next change before it has time to clear, and the function will be messed up
this.auto();
}
}
else{ //Transparency changes
this.li[i].o =5;
this.li[i].style.opacity = this.li[i].o/100;
this.li[ i].style.filter = 'alpha(opacity=' this.li[i].o ')';
}
}
Okay, it's that simple. But another thing to remember is that you must remember to clear the last timer at the beginning of the pos() call! !
Post the entire source code below:
var Hongru={};
function H$(id){return document.getElementById(id)}
function H$$(c,p){return p.getElementsByTagName(c) }
Hongru.fader = function(){
function init(anthor,options){this.anthor=anthor; this.init(options);}
init.prototype = {
init: function(options){ //options parameters: id (required): picture list parent tag id; auto (optional): automatic running time; index (optional): starting picture serial number
var wp = H$(options.id), // Get the parent element of the picture list
ul = H$$('ul',wp)[0], // Get
li = this.li = H$$( 'li',ul);
this.a = options.auto?options.auto:2; //Automatic running interval
this.index = options.position?options.position:0; //Start running Picture serial number (starting from 0)
this.l = li.length;
this.cur = this.z = 0; //Currently displayed picture serial number&&z-index variable
/* == Add fade-in and fade-out function ==*/
for(var i=0;i
this.li[i].style.opacity = this.li[i].o/100; //For non-IE, just use opacity
this.li[i].style.filter = 'alpha(opacity=' this.li[i].o ')'; //IE filter
}
this.pos(this.index); //Transformation function
},
auto:function(){
this.li.a = setInterval(new Function(this.anthor '.move(1)'),this.a*1000);
},
move :function(i){//There are two options for parameter i, 1 and -1, 1 means running to the next picture, -1 means running to the previous picture
var n = this.cur i;
var m = i==1?n==this.l?0:n:nthis.pos(m); //Transform to the previous or next picture
},
pos:function(i){
clearInterval(this.li.a); / /Clear the automatic change timer
clearInterval(this.li[i].f); //Clear the fade effect timer
this.z;
this.li[i].style.zIndex = this.z; //Increase the z-index of the next picture by one each time
this.cur = i; //Bind the correct serial number of the currently displayed picture
this.li.a = false; // Make a mark, which will be used below, indicating that the clear timer has been completed
//this.auto(); //Automatically run
if(this.li[i].o>=100){ // Before the image fades in, set the image transparency to transparent
this.li[i].o = 0;
this.li[i].style.opacity = 0;
this.li[i] .style.filter = 'alpha(opacity=0)';
}
this.li[i].f = setInterval(new Function(this.anthor '.fade(' i ')'),20 );
},
fade:function(i){
if(this.li[i].o>=100){
clearInterval(this.li[i].f); //If the transparency change is completed, clear the timer
if(!this.li.a){ //Make sure all timers are cleared before starting automatic operation. Otherwise, if the controller is clicked too fast, the timer will start the next change before it has time to clear, and the function will be messed up
this.auto();
}
}
else{
this.li[i].o =5;
this.li[i].style.opacity = this.li[i].o/100;
this.li[i].style .filter = 'alpha(opacity=' this.li[i].o ')';
}
}
}
return {init:init}
}();
Everyone should pay attention to the notes I wrote. Some places are more critical.
Let’s take a look at the running effect:
If you need to introduce external Js, you need to refresh to execute ]
Some people may have noticed that the fade in and fade out here is just a title. In fact, it only has a fade in effect, but it doesn’t matter. The effect is basically the same as a fade out, and even if you want to fade out, you only need to change two sentences. Haha
This part ends here, the next part will add the controller.

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

With the popularity of mobile devices, web design needs to take into account factors such as device resolution and screen size of different terminals to achieve a good user experience. When implementing responsive design of a website, it is often necessary to use the image carousel effect to display the content of multiple images in a limited visual window, and at the same time, it can also enhance the visual effect of the website. This article will introduce how to use CSS to achieve a responsive image automatic carousel effect, and provide code examples and analysis. Implementation ideas The implementation of responsive image carousel can be implemented through CSS flex layout. exist

How to use PHP to implement image carousel and slideshow functions In modern web design, image carousel and slideshow functions have become very popular. These features can add some dynamics and appeal to web pages and improve user experience. This article will introduce how to use PHP to implement image carousel and slideshow functions to help readers master this technology. Creating the Infrastructure in HTML First, create the infrastructure in the HTML file. Let's say our image carousel has a container and several image elements. The HTML code is as follows

How to use PHP to develop a simple image carousel function. The image carousel function is very common in web design, and can present users with better visual effects and improve user experience. This article will introduce how to use PHP to develop a simple image carousel function and give specific code examples. First, we need to prepare some image resources as carousel images. Place these images in a folder and name it "slider", making sure the folder path is correct. Next, we need to write a PHP script to get these graphs

How to implement image carousel switching effect and add fade-in and fade-out animation with JavaScript? Image carousel is one of the common effects in web design. By switching images to display different content, it gives users a better visual experience. In this article, I will introduce how to use JavaScript to achieve a carousel switching effect of images and add a fade-in and fade-out animation effect. Below is a specific code example. First, we need to create a container containing the carousel in the HTML page and add it

How to use HTML, CSS and jQuery to create a dynamic image carousel. In website design and development, image carousel is a frequently used function for displaying multiple images or advertising banners. Through the combination of HTML, CSS and jQuery, we can achieve a dynamic image carousel effect, adding vitality and appeal to the website. This article will introduce how to use HTML, CSS and jQuery to create a simple dynamic image carousel, and provide specific code examples. Step 1: Set up HTML junction

How to implement image carousel function through WordPress plug-in In today’s website design, image carousel function has become a common requirement. It can make the website more attractive and can display multiple pictures to achieve better publicity effect. In WordPress, we can implement the image carousel function by installing plug-ins. This article will introduce a common plug-in and provide code samples for reference. 1. Plug-in introduction In the WordPress plug-in library, there are many image carousel plug-ins to choose from, one of which is often

How to use Vue and ElementPlus to implement image carousels and slideshows. In web design, image carousels and slideshows are common functional requirements. These functions can be easily implemented using Vue and ElementPlus framework. This article will introduce how to use Vue and ElementPlus to create a simple and beautiful picture carousel and slideshow component. First, we need to install Vue and ElementPlus. Execute the following commands on the command line:

How to implement image carousel function in JavaScript? Picture carousel is one of the commonly used functions in web design. It can display multiple pictures and automatically switch at a certain time interval to increase the user's visual experience. It is not complicated to implement the image carousel function in JavaScript. This article will explain the implementation method with specific code examples. First, we need to create a container in HTML to display images and buttons to control the carousel. A basic carousel container can be created using the following code: <
