Carousel animation can improve the appearance and interactive performance of the page. Next, we will learn to use the basic knowledge of front-end development such as HTML, CSS and Javascript to make a simple carousel. Introduction to carousel: In a specific module of a website, different pictures can be displayed by clicking the mouse on a computer or moving the mouse in, or sliding the finger on a mobile phone. This module is called a carousel module. (Welcome to criticize and correct me if I have done anything wrong. If you think it is helpful, please give me a star~) HTML layout part: <div id="box"> <div class="scenery pic"> <img class="show" src="imgs/s2.jpg" alt="scenery"> <img src="imgs/s3.jpg" alt="scenery"> <img src="imgs/s1.jpg" alt="scenery"> <img src="imgs/s5.jpg" alt="scenery"> <img src="imgs/s4.jpg" alt="scenery"> </div> <div class="car pic"> <img src="imgs/animal4.jpg" alt="animal"> <img src="imgs/animal3.jpg" alt="animal"> <img src="imgs/animal2.jpg" alt="animal"> <img src="imgs/animal1.jpg" alt="animal"> </div> <div class="entertainment pic"> <img src="imgs/entertainment1.jpg" alt="entertainment"> <img src="imgs/entertainment2.jpg" alt="entertainment"> <img src="imgs/entertainment3.jpg" alt="entertainment"> <img src="imgs/entertainment4.jpg" alt="entertainment"> <img src="imgs/entertainment5.jpg" alt="entertainment"> </div> <div class="leftbar"> <div class="checked">Landscape</div> <div>Famous car</div> <div>Entertainment</div> </div> <div class="bottombar"> </div> </div> CSS style part: /* For the convenience of layout, flex is mostly used in containers */ #box { position: relative; width: 460px; height: 300px; margin: 40px auto; border: 1px solid rgb(109, 98, 98); user-select: none; } /* Sidebar layout */ .leftbar { display: flex; flex-direction: column; justify-content: space-between; position: absolute; top: -1px; left: -80px; width: 80px; height: 100%; text-align: center; font-size: 20px; cursor: pointer; } .leftbar div { flex: 1; line-height: 100px; background-color: cadetblue; letter-spacing: 5px; } .leftbar div:nth-child(2) { border-top: 1px solid #fff; border-bottom: 1px solid #fff; } /* Bottom switch button style design*/ .bottombar { display: flex; justify-content: space-between; position: absolute; bottom: -1px; right: -1px; z-index: 10; width: 200px; height: 30px; cursor: pointer; } .bottombar div { flex: 1; line-height: 30px; background-color: rgb(232, 233, 235, .5); text-align: center; font-weight: 700; } .bottombar div~div { border-left: 1px solid grey; } img { position: absolute; display: block; width: 460px; height: 300px; } .show { z-index: 5; } .leftbar .checked, .bottombar .checked { background-color: rgb(241, 5, 5); } JavaScript logic implementation part: var Box = document.querySelector('#box'), pic = Box.querySelectorAll('.pic'), Idx = 0, index = 0, timer = null, ltDiv = Box.querySelector('.leftbar'), btDiv = Box.querySelector('.bottombar'), Img = Box.querySelectorAll('img'); function createBtBar(len) {//Dynamically create the bottom switch button var str = ''; for (var i = 0; i < len; i++) { str += `<div>${i + 1}</div>`; } btDiv.innerHTML = str; btDiv.children[0].classList.add('checked'); } function initialize() {//Page initial status createBtBar(pic[0].children.length); } initialize(); function clearZindex() {//Reset the positioning level of all images for (var k = 0; k < Img.length; k++) { Img[k].classList.remove('show'); } } ltDiv.addEventListener('click', (e) => {//Sidebar item switchif (e.target.tagName.toLowerCase() === 'div') { for (var j = 0; j < ltDiv.children.length; j++) { ltDiv.children[j].classList.remove('checked'); } clearZindex(); Idx = 0; index = getEleIdx(e.target); ltDiv.children[index].classList.add('checked'); pic[index].children[0].classList.add('show'); createBtBar(pic[index].children.length); } }); btDiv.addEventListener('click', (e) => {//Delegate listens to the bottom switch button if (e.target.tagName.toLowerCase() === 'div') { function changePic(callback) { btDiv.children[Idx].classList.remove('checked'); clearZindex(); callback && callback(); btDiv.children[Idx].classList.add('checked'); pic[index].children[Idx].classList.add('show'); } changePic(function () { Idx = getEleIdx(e.target); }); } }); function getEleIdx(item) {//Get DOM element index var elements = item.parentNode.children; for (var i = 0, len = elements.length; i < len; i++) { if (item === elements[i]) { return i; } } } function loopShow() {//loop automatic display clearInterval(timer); timer = setInterval(function () { pic[index].children[Idx].classList.remove('show'); btDiv.children[Idx].classList.remove('checked'); Idx += 1; if (Idx < 0 || Idx > pic[index].children.length - 1) { Idx = 0; } pic[index].children[Idx].classList.add('show'); btDiv.children[Idx].classList.add('checked'); }, 1000); } loopShow(); Box.addEventListener('mouseover', function () { clearInterval(timer); //Move the mouse into the display area to stop the carousel }); Box.addEventListener('mouseout', function () { loopShow(); //Move the mouse out of the display area to automatically rotate }); The specific display effects are as follows: (Tip: Please prepare the pictures and put them in your own folder~) The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Solution to the Chinese garbled code problem in the decompressed version of MYSQL
>>: MySQL loop inserts tens of millions of data
Prepare war package 1. Prepare the existing Sprin...
1. The Chinese garbled characters appear in MySQL...
Suddenly, I needed to build a private service for...
Preface: After the automation is written, it need...
I have summarized 3 methods to deploy multiple fr...
Table of contents Let's talk about flattening...
1. Apache 2.4.41 installation and configuration T...
The specific code is as follows: The html code is...
I have been working on a project recently - Budou...
Preface What is data type conversion? The default...
Prepare a CentOS6 installation disk (any version)...
This article records the installation and configu...
This article uses an example to describe how to c...
In many apps and websites, when we log in or regi...
When it comes to bionic design, many people will t...