


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.

随着移动设备的普及,网页设计需要考虑到不同终端的设备分辨率和屏幕尺寸等因素,以实现良好的用户体验。在实现网站的响应式设计时,常常需要使用到图片轮播效果,以展示多张图片在有限的可视窗口中的内容,同时也能够增强网站的视觉效果。本文将介绍如何使用CSS实现响应式图片自动轮播效果,并提供代码示例和解析。实现思路响应式图片轮播的实现可以通过CSS的flex布局实现。在

如何使用PHP实现图片轮播和幻灯片功能在现代网页设计中,图片轮播和幻灯片功能已经变得非常流行。这些功能可以给网页增添一些动态和吸引力,提升用户体验。本文将介绍如何使用PHP实现图片轮播和幻灯片功能,帮助读者掌握这一技术。在HTML中创建基础结构首先,在HTML文件中创建基础结构。假设我们的图片轮播有一个容器以及几个图片元素。HTML代码如下

如何通过WordPress插件实现图片轮播功能在如今的网站设计中,图片轮播功能已经成为一个常见的需求。它可以让网站更具吸引力,并且能够展示多张图片,达到更好的宣传效果。在WordPress中,我们可以通过安装插件来实现图片轮播功能,本文将介绍一种常见的插件,并提供代码示例供参考。一、插件介绍在WordPress插件库中,有许多图片轮播插件可供选择,其中一款常

如何使用HTML、CSS和jQuery制作一个动态的图片轮播在网站设计和开发中,图片轮播是一个经常使用的功能,用于展示多张图片或广告横幅。通过HTML、CSS和jQuery的结合,我们可以实现一个动态的图片轮播效果,为网站增加活力和吸引力。本文将介绍如何使用HTML、CSS和jQuery制作一个简单的动态图片轮播,并提供具体的代码示例。第一步:设置HTML结

如何利用PHP开发一个简单的图片轮播功能图片轮播功能在网页设计中十分常见,能够给用户呈现出更好的视觉效果,提升用户体验。本文将介绍如何使用PHP开发一个简单的图片轮播功能,并给出具体的代码示例。首先,我们需要准备一些图片资源作为轮播的图片。将这些图片放在一个文件夹内,并命名为"slider",确保文件夹路径正确。接下来,我们需要编写一个PHP脚本来获取这些图

JavaScript如何实现图片的轮播切换效果并加入淡入淡出动画?图片轮播是网页设计中常见的效果之一,通过切换图片来展示不同的内容,给用户带来更好的视觉体验。在这篇文章中,我将介绍如何使用JavaScript来实现图片的轮播切换效果,并加入淡入淡出的动画效果。下面是具体的代码示例。首先,我们需要在HTML页面中创建一个包含轮播图的容器,并在其中添加

如何利用Vue和ElementPlus实现图片轮播和幻灯片展示在网页设计中,图片轮播和幻灯片展示是常见的功能需求。而使用Vue和ElementPlus框架可以很轻松地实现这些功能。本文将介绍如何使用Vue和ElementPlus来创建一个简单而美观的图片轮播和幻灯片展示组件。首先,我们需要先安装Vue和ElementPlus。在命令行中执行以下命令:

CSS实现淡入淡出图片效果的技巧和方法在网页设计中,图片的展示是非常重要的一部分。为了提升用户体验,我们经常会使用一些动态效果来增加页面的吸引力。其中,淡入淡出效果是一种常见且优雅的动画效果,可以让页面显得流畅和有活力。本文将介绍使用CSS实现淡入淡出图片效果的技巧和方法,并提供具体的代码示例供参考。一、使用CSS的opacity属性实现淡入淡出效果CSS的


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

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.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
