Plug-in encapsulation of js carousel chart
Nowadays websites often have some picture carousel effects. There are also many various picture carousel plug-ins on the Internet. They have all kinds of effects. Here I will share a picture carousel I wrote myself. Playback effect
This Lightbox has a window container. There is a long container inside the container, which covers all the pictures horizontally. The left and right effects are achieved by changing its margin-left.
There is a black square corresponding to the number of pictures below the picture. Click the black square to jump to the corresponding picture;
The biggest feature of this plug-in is that it can be based on the number of pictures inside. Implement switching, and when sliding to the last picture, click the button on the right again, and the first picture will appear on the right, thus achieving loop playback.
I didn’t have time to debug the style of the picture and the button. You can change the css according to the effect you want
The specific code is as follows:
~function(){function AutoBanner(curEleId,ajaxURL,interval){//把之前存储获取元素的变量都作为当前实例的私有属性this.banner = document.getElementById(curEleId);this.bannerInner = utils.firstChild(this.banner);this.bannerTip = utils.children(this.banner,"ul")[0];this.bannerLink = utils.children(this.banner,'a');this.bannerLeft = this.bannerLink[0];this.bannerRight = this.bannerLink[1];this.divList = this.bannerInner.getElementsByTagName('div');this.imgList = this.bannerInner.getElementsByTagName('img');this.oLis = this.bannerTip.getElementsByTagName('li');//之前的全局变量也应该变为自己的私有属性this.jsonData = null;this.interval = interval || 3000;this.autoTimer = null;this.step = 0;this.ajaxURL = ajaxURL;//返回当前实例return this.init(); } AutoBanner.prototype = { constructor:AutoBanner,//Ajax请求数据getData:function(){var _this = this;var xhr = new XMLHttpRequest; xhr.open("get",this.ajaxURL + "?_="+Math.random(),false); xhr.onreadystatechange = function(){if(xhr.readyState ===4 && /^2\d{2}$/.test(xhr.status)){ _this.jsonData = utils.formatJSON(xhr.responseText) } } xhr.send(null) },//实现数据绑定bindData:function(){var str = "",str2 = "";if(this.jsonData){for(var i = 0,len=this.jsonData.length;i<len;i++){var curData = this.jsonData[i]; str+='<div><img src="" alt="" trueImg="'+curData['img']+'"></div>'; i===0?str2+="<li class='bg'></li>":str2+="<li></li>"} }this.bannerInner.innerHTMl = str;this.bannerTip.innerHTML = str2; },//延迟加载lazyImg:function(){var _this = this;for(var i = 0,len = this.imgList.length;i<len;i++){~function(i){var curImg = _this.imgList[i];var oImg = new Image; oImg.src = curImg.getAttribute('trueImg'); oImg.onload = function(){ curImg.src = this.src; curImg.style.display = block;//只对第一张处理if(i===0){var curDiv = curImg.parentNode; curDiv.style.zIndex = 1; myAnimate(curDiv,{opacity:1},200); } oImg = null; } }(i) } },//自动轮播autoMove:function(){if(this.step === this.jsonData.length-1){this.step = -1}this.step++;this.setBanner(); },//切换效果和焦点对齐setBanner:function(){for(var i = 0,len = this.divList.length;i<len;i++){var curDiv = this.divList[i];if(i===this.step){ utils.css(curDiv,"zIndex",1)//2、让当前的透明度从0变为1,当动画结束,我们需要让其他的div的透明度的值直接变为0myAnimate(curDiv,{opacity:1},200,function(){var curDivSib = utils.siblings(this);for(var k = 0,len = curDivSib.length;k<len;k++){ utils.css(curDivSib[k],'opacity',0) } })continue} utils.css(curDiv,"zIndex",0) }//实现焦点对其for(i = 0,len = this.oLis.length;i<len;i++){var curLi = this.oLis[i]; i === this.step?utils.addClass(curLi,"bg"):utils.removeClass(curLi,"bg"); } },//控制自动轮播mouseEvent:function(){var _this = this;this.banner.onmouseover = function(){ window.clearInterval(_this.autoTimer); _this.bannerLeft.style.display = _this.bannerRight.style.display = "block"}this.banner.onmouseout = function(){ _this.autoTimer = window.setInterval(function(){ _this.autoMove.call(_this) },_this.interval); _this.bannerLeft.style.display = _this.bannerRight.style.display = "none"} },//实现焦点切换tipEvent:function(){var _this = this;for(var i = 0,len = this.oLis.length;i<len;i++){var curLi = this.oLis[i]; curLi.index = i; curLi.onclick = function(){ _this.step = this.index; _this.setBanner(); } } },//实现左右切换leftRight:function(){var _this = this;this.bannerRight.onclick = function(){ _this.autoMove(); };this.bannerLeft.onclick = function(){if(_this.step === 0){ _this.step = _this.jsonData.length; } _this.step--; _this.setBanner(); } },//当前插件的唯一入口 命令模式:init相当于指挥室,指挥各军队协同作战init:function(){var _this = this;this.getData();this.bindData(); window.setTimeout(function(){ _this.lazyImg(); },500);this.autoTimer = window.setInterval(function(){ _this.autoMove(); },this.interval);this.mouseEvent();this.tipEvent();this.leftRight();return this; } } window.AutoBanner = AutoBanner }()//使用var banner1 = new AutoBanner('banner','json/banner.txt',1000)
The above is the detailed content of Plug-in encapsulation of js carousel chart. For more information, please follow other related articles on the PHP Chinese website!

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

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 use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
