欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用js原生實(shí)現(xiàn)年份輪播選擇效果實(shí)例

 更新時(shí)間:2021年01月12日 11:24:54   作者:Hui-1018  
這篇文章主要給大家介紹了關(guān)于如何使用js原生實(shí)現(xiàn)年份輪播選擇效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

用js實(shí)現(xiàn)一個(gè)年份輪換選擇效果。廢話不多說(shuō),看圖:

一、思路是什么?

  • 布局: 左右箭頭使用實(shí)體字符 < 和 > 年份5個(gè)span。使用用flex布局橫向排列。
  • js邏輯:(注:年份數(shù)據(jù)為number數(shù)組)
    • a> . 默認(rèn)展示年份數(shù)據(jù)前5個(gè)。
    • b>. firstIndex記錄要顯示的5個(gè)年份的起始索引。點(diǎn)擊右側(cè)箭頭+1,直到firstIndex+5 剛好等于年份數(shù)組長(zhǎng)度,不在遞增。點(diǎn)擊左側(cè)箭頭-1,直到firstIndex為0,不在遞減。初始值為0。
    • c>.selectedIndex記錄當(dāng)前點(diǎn)擊選中的年份索引。默認(rèn)顯示第一個(gè)即2021。初始值0。
    • d>.firstIndex值發(fā)生變化,獲取firstIndex,firstIndex+1,firstIndex+2…firstIndex+4對(duì)應(yīng)的年份,渲染到頁(yè)面。并且這5個(gè)索引中某一個(gè)和selectedIndex相等,說(shuō)明選中的年份,剛好在當(dāng)前頁(yè)面顯示的年份當(dāng)中。所以,與之相等的index對(duì)應(yīng)的span添加選中樣式,其他4個(gè)span移除選中樣式。
  • css:左右箭頭邏輯,默認(rèn)全部添加可點(diǎn)擊樣式:firstIndex=0,移除左可點(diǎn)擊樣式,firstIndex+5=年份數(shù)組長(zhǎng)度,移除右可點(diǎn)擊樣式。

二、全部代碼

1. html

代碼如下:

<!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>&lt;</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>&gt;</span>
 </div>

</div>

<div class="content" id="content">

</div>
</body>
</html>

2.js

代碼如下:

window.onload = function () {
 //首次渲染列表
 initList(firstIndex);
};

let yearArr = [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021];
yearArr.reverse();

//起始索引
let firstIndex = 0;
//選中索引,默認(rèn)選中第一個(gè)
let selectedIndex = 0;


/**
 * 初始化列表
 */
function initList(firstIndex) {

 //渲染頁(yè)面span列表
 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];

 //選中樣式添加和移除
 if (index === selectedIndex) {
  span.classList.add('active');
 } else {
  span.classList.remove('active')
 }
 }
 //頁(yè)面內(nèi)容顯示值
 document.getElementById('content').innerText = '當(dāng)前選中年份:' + yearArr[selectedIndex];
}

/**
 * 下一頁(yè)
 */
function clickNext(div) {
 if (firstIndex + 5 < yearArr.length) {
 firstIndex = firstIndex + 1;
 initList(firstIndex)
 }
 arrowActive();
}

/*
* 上一頁(yè)
 */
function clickBefore(div) {
 if (firstIndex > 0) {
 firstIndex = firstIndex - 1;
 initList(firstIndex)
 }
 arrowActive();
}

/**
 * 選中
 */
function selected(span) {
 let value = span.innerText;
 let index = yearArr.findIndex((el) => {
 return el + "" === value;
 })
 selectedIndex = index !== -1 ? index : 0;
 initList(firstIndex);
}

/**
 * 左右箭頭激活
 * firstIndex = 0: 右激活 左不
 * firstIndex = length-5:左激活 右不
 * 其他:全激活
 */
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

代碼如下:

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;
}

總結(jié)

每天記錄一點(diǎn),從小小菜鳥(niǎo)變小菜鳥(niǎo)?。?!

到此這篇關(guān)于使用js原生實(shí)現(xiàn)年份輪播選擇效果的文章就介紹到這了,更多相關(guān)js原生實(shí)現(xiàn)年份輪播選擇內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript字符串插入、刪除、替換函數(shù)使用示例

    JavaScript字符串插入、刪除、替換函數(shù)使用示例

    本文為大家介紹下JavaScript字符串的插入、刪除、替換函數(shù)的在實(shí)際中的應(yīng)用,想要學(xué)習(xí)的朋友可以參考下哈,希望對(duì)初學(xué)者有所幫助
    2013-07-07
  • JavaScript使用html2canvas實(shí)現(xiàn)截取HTML并生成圖片

    JavaScript使用html2canvas實(shí)現(xiàn)截取HTML并生成圖片

    在前端開(kāi)發(fā)中,有時(shí)我們需要將網(wǎng)頁(yè)的一部分或整個(gè)頁(yè)面截取并保存為圖片,這在生成報(bào)告、分享內(nèi)容或保存用戶界面狀態(tài)等場(chǎng)景中非常有用,本文將介紹如何使用 JavaScript 庫(kù) html2canvas 來(lái)實(shí)現(xiàn)這一功能,并提供一個(gè)完整的示例,需要的朋友可以參考下
    2024-10-10
  • 瀏覽器檢測(cè)JS代碼(兼容目前各大主流瀏覽器)

    瀏覽器檢測(cè)JS代碼(兼容目前各大主流瀏覽器)

    這篇文章主要介紹了瀏覽器檢測(cè)JS代碼,兼容目前各大主流瀏覽器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Javascript執(zhí)行上下文順序的深入講解

    Javascript執(zhí)行上下文順序的深入講解

    這篇文章主要給大家介紹了關(guān)于Javascript執(zhí)行上下文順序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • javascript中this的四種用法

    javascript中this的四種用法

    在javascript當(dāng)中每一個(gè)function都是一個(gè)對(duì)象,所以在這個(gè)里var temp=this 指的是function當(dāng)前的對(duì)象。this是Javascript語(yǔ)言的一個(gè)關(guān)鍵字。它代表函數(shù)運(yùn)行時(shí),自動(dòng)生成的一個(gè)內(nèi)部對(duì)象,只能在函數(shù)內(nèi)部使用。
    2015-05-05
  • 跟我學(xué)習(xí)javascript的定時(shí)器

    跟我學(xué)習(xí)javascript的定時(shí)器

    跟我學(xué)習(xí)javascript的定時(shí)器,告訴大家具體的使用方法,并向大家提出了一個(gè)消息要求,制作一個(gè)定時(shí)器,有沒(méi)有朋友感興趣,挑戰(zhàn)一下
    2015-11-11
  • electron實(shí)現(xiàn)qq快捷登錄的方法示例

    electron實(shí)現(xiàn)qq快捷登錄的方法示例

    這篇文章主要介紹了electron實(shí)現(xiàn)qq快捷登錄的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Layui 解決表格異步調(diào)用后臺(tái)分頁(yè)的問(wèn)題

    Layui 解決表格異步調(diào)用后臺(tái)分頁(yè)的問(wèn)題

    今天小編就為大家分享一篇Layui 解決表格異步調(diào)用后臺(tái)分頁(yè)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • 微信小程序第三方框架對(duì)比 之 wepy / mpvue / taro

    微信小程序第三方框架對(duì)比 之 wepy / mpvue / taro

    這篇文章主要介紹了小程序第三方框架對(duì)比 ( wepy / mpvue / taro ) 分析,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-04-04
  • 詳解JavaScript中的Object.is()與

    詳解JavaScript中的Object.is()與"==="運(yùn)算符總結(jié)

    這篇文章主要介紹了詳解JavaScript中的Object.is()與"==="運(yùn)算符總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評(píng)論