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
Preface Recently, I accidentally discovered MySQL...
Illustrated CentOS 7.3 installation steps for you...
1 The select tag must be closed <select><...
vue scaffolding -> vue.cli Quickly create a la...
Nginx is developed in C language and is recommend...
As shown below: #!/usr/bin/env python3.5 import p...
Background Information I've been rereading so...
1. Introduction to LVM When we manage Linux disks...
After being tortured by the front-end cross-domai...
I haven’t updated my blog for several days. I jus...
location matching order 1. "=" prefix i...
Preface This article describes two situations I h...
1. Grammar: <meta name="name" content...
Table of contents MutationObserver API Features I...
Update: Recently, it was discovered that the serv...