国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Web Front-end JS Tutorial How to add new functions to the Carousel plug-in with jQuery_jquery

How to add new functions to the Carousel plug-in with jQuery_jquery

May 16, 2016 pm 03:05 PM

This article is a tutorial video written by the editor to add new functions to the carousel plug-in and about the Carousel plug-in. Referring to other websites, when the mouse is placed on the lower button or clicked, the Carousel will display the li with the same subscript as the button as the first frame.

All the code is herehttps://github.com/wwervin72/jQuery-Carousel

Let’s get started. The first thing we need to do is to display these buttons. So we need to add a method to Carousel's prototype object to generate a button for switching slides.

switchSlideBtn : function(){
var slideNum = this.posterItems.size(); //獲得當(dāng)前的這個carousel對象的總共的幀數(shù)
var str = ''; 
var firstBtnLeft = (this.setting.width-(slideNum-1)*15-slideNum*15)/2; //規(guī)定第一個按鈕放的位置
for(var i = 0; i<slideNum; i++){
str += '<button class="btn"></button>'; //把每一個btn的代碼添加到str字符串中,然后一次性添加到selBtn這里面,避免多次修改DOM
}
$('#selBtn').html(str); 
for(var i = 0;i<slideNum; i++){
$('#selBtn .btn').eq(i).css('left' , firstBtnLeft+i*30); 
} 
}, 

Then we need to run this method in the Carousel constructor

this.switchSlideBtn();

Now here, our selection button has been added. What we need to do now is to add an event for each button when the mouse is placed on it.

$('#selBtn .btn').each(function(){
$(this).hover(function(){
if(self.rotateFlag){
self.switchSlide(this);
}
},function(){
});
})

Then we also need to add a method to switch slides to the prototype object of Carousel, because in the HTML code we use li and put the a and Img tags in it, so the following Li is each element of Carousel. frame.

//用切換幻燈片的按鈕切換幻燈片的方法
switchSlide : function(btn){
var self = this;
var BtnIndex = $(btn).index(); //獲得當(dāng)前是哪一個按鈕執(zhí)行事件
$('#selBtn .btn').css('background','rgba(255,255,255,.3)');
$('#selBtn .btn').eq(BtnIndex).css('background','rgba(255,255,255,1)');
var level = Math.floor(this.posterItems.size()/2),
posterItemsLength = this.posterItems.size(),
index;
$('.poster-item').filter(function(i,item){
if($(this).css('z-index') == level){ //獲得當(dāng)前顯示的第一幀的下標(biāo)
index = i;
}
});
var nextTime = BtnIndex-index; //向左旋轉(zhuǎn)nextTime次
var arr = [],zIndexArr=[];
for(var i = 0;i < posterItemsLength;i++){ 
arr.push(i); 
}
arr = arr.concat(arr); //添加一個數(shù)組,用來模擬Li的下標(biāo)
if(nextTime > 0){ //prev 左旋轉(zhuǎn),把數(shù)組的后半部分向前移動nextTime個下標(biāo)
self.rotateFlag = false; //注意這里吧self.rotateFlag這個標(biāo)識放在里面來修改了。
this.posterItems.each(function(i, item){
var posterItemIndex = arr.lastIndexOf(i); //獲得li節(jié)點在arr中對應(yīng)的下標(biāo)
var tag = $(self.posterItems[arr[posterItemIndex-nextTime]]),
width = tag.width(),
height = tag.height(),
zIndex = tag.css('zIndex'),
opacity = tag.css('opacity'),
left = tag.css('left'),
top = tag.css('top');
zIndexArr.push(zIndex);
$(item).animate({
width : width,
height : height,
opacity : opacity,
left : left,
top : top
},self.setting.speed,function(){
self.rotateFlag = true; //在每一個幀的動畫都執(zhí)行完畢之后,self.rotateFlag改為true,才能執(zhí)行下一次動畫
});
});
self.posterItems.each(function(i){
$(this).css('zIndex',zIndexArr[i]); //把這個z-index提出來單獨改變是為了讓z-index這個屬性的改變最先執(zhí)行,并且不需要動畫
});
}
if(nextTime < 0){ //next 右旋轉(zhuǎn),把數(shù)組的前半部分向后移動nextTime的絕對值個下標(biāo)
self.rotateFlag = false;
this.posterItems.each(function(i, item){
var posterItemIndex = arr.indexOf(i), //獲得li節(jié)點在arr中對應(yīng)的下標(biāo)
tag = $(self.posterItems[arr[posterItemIndex+Math.abs(nextTime)]]),
width = tag.width(),
height = tag.height(),
zIndex = tag.css('zIndex'),
opacity = tag.css('opacity'),
left = tag.css('left'),
top = tag.css('top');
zIndexArr.push(zIndex);
$(item).animate({
width : width,
height : height,
opacity : opacity,
left : left,
top : top
},self.setting.speed,function(){
self.rotateFlag = true; 
});
});
self.posterItems.each(function(i){
$(this).css('zIndex',zIndexArr[i]);
});
}
},

I mainly encountered two problems here:

1. How to obtain the subscript of each frame in Carousel after movement, and then add the attributes of the corresponding subscript to the corresponding frame.

Here I create an array with elements 0-li.length-1 based on the length of li, and concat itself again, using the elements inside to identify the subscript after each frame is moved, if it is needed by Carousel Rotate to the left, that is, the subscript of the button is greater than the subscript of the current first frame, then we need to use the second half of this array as the subscript of each frame, and move to the left (button subscript - the current first frame A frame index) position, and then the element of this position is the index of each frame after rotation. The same is true if it is rotated to the right. But you need to move the first half of the array one after another.

2. When we use the mouse to move quickly on the button, some bugs will appear. This is because the next event is triggered before the previous animation is completed.

Then here we need to use a flag to limit the execution of the event, which is the self.rotateFlag here. But after many tests, I found that the statement with the flag assigned to false cannot be placed in front of the rotating method. This will also cause problems. When we place it at the beginning of the if conditional statement in the method, Basically there is no problem.

Okay, now we have introduced the functions of Carousel extension. I won’t introduce the other parts. Interested friends can go to the address I gave above to download and have a look. At the same time, thank you very much for your support of the Script House website!

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Java vs. JavaScript: Clearing Up the Confusion Java vs. JavaScript: Clearing Up the Confusion Jun 20, 2025 am 12:27 AM

Java and JavaScript are different programming languages, each suitable for different application scenarios. Java is used for large enterprise and mobile application development, while JavaScript is mainly used for web page development.

Javascript Comments: short explanation Javascript Comments: short explanation Jun 19, 2025 am 12:40 AM

JavaScriptcommentsareessentialformaintaining,reading,andguidingcodeexecution.1)Single-linecommentsareusedforquickexplanations.2)Multi-linecommentsexplaincomplexlogicorprovidedetaileddocumentation.3)Inlinecommentsclarifyspecificpartsofcode.Bestpractic

How to work with dates and times in js? How to work with dates and times in js? Jul 01, 2025 am 01:27 AM

The following points should be noted when processing dates and time in JavaScript: 1. There are many ways to create Date objects. It is recommended to use ISO format strings to ensure compatibility; 2. Get and set time information can be obtained and set methods, and note that the month starts from 0; 3. Manually formatting dates requires strings, and third-party libraries can also be used; 4. It is recommended to use libraries that support time zones, such as Luxon. Mastering these key points can effectively avoid common mistakes.

Why should you place  tags at the bottom of the ? Why should you place tags at the bottom of the ? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript vs. Java: A Comprehensive Comparison for Developers JavaScript vs. Java: A Comprehensive Comparison for Developers Jun 20, 2025 am 12:21 AM

JavaScriptispreferredforwebdevelopment,whileJavaisbetterforlarge-scalebackendsystemsandAndroidapps.1)JavaScriptexcelsincreatinginteractivewebexperienceswithitsdynamicnatureandDOMmanipulation.2)Javaoffersstrongtypingandobject-orientedfeatures,idealfor

JavaScript: Exploring Data Types for Efficient Coding JavaScript: Exploring Data Types for Efficient Coding Jun 20, 2025 am 12:46 AM

JavaScripthassevenfundamentaldatatypes:number,string,boolean,undefined,null,object,andsymbol.1)Numbersuseadouble-precisionformat,usefulforwidevaluerangesbutbecautiouswithfloating-pointarithmetic.2)Stringsareimmutable,useefficientconcatenationmethodsf

What is event bubbling and capturing in the DOM? What is event bubbling and capturing in the DOM? Jul 02, 2025 am 01:19 AM

Event capture and bubble are two stages of event propagation in DOM. Capture is from the top layer to the target element, and bubble is from the target element to the top layer. 1. Event capture is implemented by setting the useCapture parameter of addEventListener to true; 2. Event bubble is the default behavior, useCapture is set to false or omitted; 3. Event propagation can be used to prevent event propagation; 4. Event bubbling supports event delegation to improve dynamic content processing efficiency; 5. Capture can be used to intercept events in advance, such as logging or error processing. Understanding these two phases helps to accurately control the timing and how JavaScript responds to user operations.

What's the Difference Between Java and JavaScript? What's the Difference Between Java and JavaScript? Jun 17, 2025 am 09:17 AM

Java and JavaScript are different programming languages. 1.Java is a statically typed and compiled language, suitable for enterprise applications and large systems. 2. JavaScript is a dynamic type and interpreted language, mainly used for web interaction and front-end development.

See all articles