PrefaceUse js to achieve a year rotation selection effect. Without further ado, let’s take a look at the pictures: 1. What is the idea?
2. All codes1. html The code is as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="index.css" rel="external nofollow" type="text/css"/> <script type="text/javascript" src="echarts.min.js"></script> <script type="text/javascript" src="index.js"></script> </head> <body> <div class="container"> <div id="left" class="arrow_left" onclick="clickBefore()" style="cursor:default" unselectable="on" onselectstart="return false;"> <span><</span> </div> <div id="wrap" class="wrap"> <span onclick="selected(this)">1</span> <span onclick="selected(this)">2</span> <span onclick="selected(this)">3</span> <span onclick="selected(this)">4</span> <span onclick="selected(this)">5</span> </div> <div id="right" class="arrow_right arrow_active" onclick="clickNext()" style="cursor:default" unselectable="on" onselectstart="return false;"> <span>></span> </div> </div> <div class="content" id="content"> </div> </body> </html> 2.js The code is as follows: window.onload = function () { //First render list initList(firstIndex); }; let yearArr = [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021]; yearArr.reverse(); //Starting index let firstIndex = 0; //Selected index, the first one is selected by default let selectedIndex = 0; /** * Initialization list */ function initList(firstIndex) { //Rendering page span list let spanList = document.getElementById('wrap').getElementsByTagName('span'); for (let i = 0; i < spanList.length; i++) { let index = firstIndex + i; let span = spanList[i]; span.innerText = yearArr[index]; //Select style to add and remove if (index === selectedIndex) { span.classList.add('active'); } else { span.classList.remove('active') } } //Page content display value document.getElementById('content').innerText = 'Currently selected year:' + yearArr[selectedIndex]; } /** * Next page */ function clickNext(div) { if (firstIndex + 5 < yearArr.length) { firstIndex = firstIndex + 1; initList(firstIndex) } arrowActive(); } /* * Previous page */ function clickBefore(div) { if (firstIndex > 0) { firstIndex = firstIndex - 1; initList(firstIndex) } arrowActive(); } /** * Select */ function selected(span) { let value = span.innerText; let index = yearArr.findIndex((el) => { return el + "" === value; }) selectedIndex = index !== -1 ? index : 0; initList(firstIndex); } /** * Left and right arrows activated * firstIndex = 0: right activated, left not * firstIndex = length-5: left activated, right not * Others: all activated */ function arrowActive() { let left = document.getElementById('left') let right = document.getElementById('right') left.classList.add('arrow_active'); right.classList.add('arrow_active'); if (firstIndex === 0) { left.classList.remove('arrow_active'); } else if (firstIndex === yearArr.length - 5) { right.classList.remove('arrow_active'); } } 2.css The code is as follows: body{ margin-top: 80px; } .container { display: flex; justify-content: center; align-items: center; margin: 10px; } .wrap { height: 40px; z-index: 1; color: black; display: flex; flex: 1; background: rgba(155,226,219,0.5); border-radius: 20px; margin-left: 20px; margin-right: 20px; } .wrap span { flex: 1; text-align: center; height: 40px; line-height: 40px; border-radius: 20px; } .active{ background: #1abc9c; color:#fff; } .arrow_left { left: 10px; color: green; padding: 0px 14px; border-radius: 50%; font-size: 30px; z-index: 2; } .arrow_right { right: 10px; color: green; padding: 0px 14px; border-radius: 50%; font-size: 30px; z-index: 2; } .arrow_active{ color: blue; } .content{ margin-left: 30px; } SummarizeRecord a little bit every day and grow from a rookie to a rookie! ! ! This is the end of this article about using js natively to implement the year carousel selection effect. For more relevant js native implementation of year carousel selection content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A brief introduction to Vue filters, lifecycle functions and vue-resource
>>: How to implement vue page jump
Table of contents 1. Where is the slowness? 2. Ha...
This article has been included on GitHub https://...
Public free STUN servers When the SIP terminal us...
Unix/Linux Services systemd services Operation pr...
1. Introduction I have taken over a project of th...
When it comes to pictures, the first thing we thi...
Preface Let me share with you how I deployed a Sp...
need: The backend returns an array object, which ...
Table of contents 1. Docker configuration 2. Crea...
I recently upgraded MySQL to 5.7, and WordPress r...
1. Use .gifs rather than .jpgs. GIFs are smaller ...
One time we talked about the dice rolling game. A...
In the process of making web pages, it is inevita...
I have always used Loadrunner to do performance t...
in conclusion % of width: defines the percentage ...