


Sample code sharing for implementing rotating carousel chart using JavaScript
This article mainly introduces the implementation of rotating carousels in JavaScript in detail, which has certain reference value. Interested friends can refer to it
I have been learning JavaScript recently, and then saw the rotation In the case of the carousel chart, I tried to use js to make a simple carousel chart. Because the dynamic effect could not be displayed, I put a screenshot:
This The effect is like this: there are 7 pictures in total, and they will automatically slide to the left. Then the left and right arrows can also control the left and right sliding of the carousel. At the same time, if the mouse stops on the picture, the carousel will stop. Automatic sliding, when the mouse is moved away, the carousel will continue to the left.
First of all, I have encapsulated two functions here (because I haven’t learned jQuery yet, so I used the method of encapsulating functions to implement it). The first function is the $ function, which can be called to get the elements in html. The code is as follows:
`function $(select){ if (typeof select != 'string') { console.log('传入的参数有误'); return null; } var firstChar = select.charAt(0); switch(firstChar){ case '#': return document.getElementById(select.substr(1)); break; case '.': if (document.getElementsByClassName){ return document.getElementsByClassName(select.substr(1)); } else { var result = []; var allElemnts = document.getElementsByTagName('*'); console.log(allElemnts); for (var i = 0; i < allElemnts.length; i++){ var e = allElemnts[i]; var className = e.className; var classArr = className.split(' '); for (var j = 0; j < classArr.length; j++){ var c = classArr[j]; if (c == select.substr(1)) { result.push(e); break; } } } return result; } break; default : return document.getElementsByTagName(select); } }`
The second function is an animation function that uses json to dynamically change multiple styles to achieve an animation effect. The code is as follows: `
function animate(element, json, fun) { clearInterval(element.timer); function getStyle(element, styleName){ if(element.currentStyle){ //ie浏览器下 直接通过currentstyle来获取 //return element.currentStyle.heigh; return element.currentStyle[styleName]; }else{ var computedStyle = window.getComputedStyle(element,null); return computedStyle[styleName]; } } var isStop = false; element.timer = setInterval(function () { isStop = true; for (var key in json){ var target = json[key]; var current; if (key == 'opacity') { //当动画的类型为透明度时 获取的值应该是浮点类型 current = parseFloat(getStyle(element, key)) || 1; } else { //其他情况 用整数类型 current = parseInt(getStyle(element, key)) || 0; } var step = (target - current) / 10; if (key != 'opacity') { step = step > 0 ? Math.ceil(step) : Math.floor(step); } current += step; if (key == 'opacity') { if (Math.abs(target - current) > 0.01) { isStop = false; } else { current = target; } element.style.opacity = current + ''; } else { if (Math.abs(target-current) > Math.abs(step)) { isStop = false; } else { current = target; } if (key == 'zIndex'){ element.style.zIndex = Math.round(current); } else { element.style[key] = current + 'px'; } } } if (isStop) { clearInterval(element.timer); console.log('动画完成'); if (typeof fun == 'function') { fun(); } } }, 30); }`
The next step is to write the html part. Because there are only a few pictures, the html part is very simple:
<body> <p class="box"> <p class="content"> <ul> <li><a href="#"><img src="/static/imghwm/default1.png" data-src="./images/1.jpg" class="lazy" alt="Sample code sharing for implementing rotating carousel chart using JavaScript" ></a></li> <li><a href="#"><img src="/static/imghwm/default1.png" data-src="./images/2.jpg" class="lazy" alt="Sample code sharing for implementing rotating carousel chart using JavaScript" ></a></li> <li><a href="#"><img src="/static/imghwm/default1.png" data-src="./images/3.jpg" class="lazy" alt="Sample code sharing for implementing rotating carousel chart using JavaScript" ></a></li> <li><a href="#"><img class="current lazy" src="/static/imghwm/default1.png" data-src="./images/4.jpg" alt="Sample code sharing for implementing rotating carousel chart using JavaScript" ></a></li> <li><a href="#"><img src="/static/imghwm/default1.png" data-src="./images/5.jpg" class="lazy" alt="Sample code sharing for implementing rotating carousel chart using JavaScript" ></a></li> <li><a href="#"><img src="/static/imghwm/default1.png" data-src="./images/6.jpg" class="lazy" alt="Sample code sharing for implementing rotating carousel chart using JavaScript" ></a></li> <li><a href="#"><img src="/static/imghwm/default1.png" data-src="./images/7.jpg" class="lazy" alt="Sample code sharing for implementing rotating carousel chart using JavaScript" ></a></li> </ul> </p> <p class="control"> <a href="javascript:;" id="prev"></a> <a href="javascript:;" id="next"></a> </p> </p> </body>
There is not much description about the css style part.
The following is the JS part, the code is also very simple, just clarify it
window.onload = function(){ //定位,并给图片设置大小透明度 var json = [{ width: 630, top: 23, left: 0, zIndex: 2, opacity: 0 },{ width: 630, top: 23, left: 0, zIndex: 3, opacity: 0 },{ width: 630, top: 23, left: 0, zIndex: 4, opacity: 0.6 },{ width: 730, top: 0, left: 125, zIndex: 5, opacity: 1 },{ width: 630, top: 23, left: 350, zIndex: 4, opacity: 0.6 },{ width: 630, top: 23, left: 350, zIndex: 3, opacity: 0 },{ width: 630, top: 23, left: 350, zIndex: 2, opacity: 0 }];
function refreshImageLocatin(index){ //默认情况下 第i张图对应第i个位置 //index=1时 第i个图对应i-1个位置 //也就是第i个图对应i-index的位置 var liArr = $('li'); for(var i = 0; i < liArr.length; i++){ var li = liArr[i]; var locationIndex = i - index; console.log('i='+i); console.log('index='+index); console.log('locationIndex='+locationIndex); if(locationIndex < 0){ locationIndex += 7; } var locationData = json[locationIndex]; animate(li, locationData, null); } } refreshImageLocatin(0); var index = 0; $('#next').onclick = function(){ index++; if(index == 7){ index = 0; } refreshImageLocatin(index); } $('#prev').onclick = function(){ index--; if(index < 0){ index = 6; } refreshImageLocatin(index); } var nextImage = $('#next').onclick; var contentBox = $('.content')[0]; //自动播放 var timer = setInterval(nextImage, 3000); //当鼠标移动到图片上,停止播放 contentBox.onmouseover = function(){ clearInterval(timer); } contentBox.onmouseout = function(){ timer = setInterval(nextImage ,3000) } }
The above is the detailed content of Sample code sharing for implementing rotating carousel chart using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

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.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools