微信小程序?qū)崿F(xiàn)全國(guó)機(jī)場(chǎng)索引列表
本文為大家分享了微信小程序?qū)崿F(xiàn)MUI索引列表的具體代碼,供大家參考,具體內(nèi)容如下
效果展示圖
實(shí)現(xiàn)的原理
- ‘當(dāng)前選擇機(jī)場(chǎng)'和右側(cè)的導(dǎo)航欄采用的是固定定位;
- 左側(cè)的展示窗口的滾動(dòng)采用的是scroll-view組件;
- 選擇中的字母提示是自己WXSS樣式制作。
WXML
<view class="right-nav"> <view bindtap="getCurrentCode" class="{{chooseIndex == index ? '.city-list-active' : ''}}" wx:for="{{cityList}}" style="height:{{codeHeight}}px" data-code="{{item.code}}"> {{item.code}} </view> </view> <view class="city-layer {{isShowLayer ? '' : 'layer-hide'}}"> {[code]} </view> <view class="current-choose-city">當(dāng)前選擇機(jī)場(chǎng):{{chooseCity}}</view> <scroll-view class="city-scroll" scroll-y="true" style="height:{{cityHeight}}px" bindscroll="scroll" scroll-top="{{scrollTop}}"> <view class="city-box" wx:for="{{cityList}}" wx:key="{{item.code}}"> <view class="city-code">{{item.code}}</view> <view class="city-list" wx:for="{{item.cityList}}" wx:for-item="city" bindtap="getChooseCity" data-city="{{city}}"> {{city}} </view> </view> </scroll-view>
WXSS
.current-choose-city{ position: fixed; width: 100%; height: 50px; line-height: 50px; padding: 0 10px; top: 0; left: 0; background-color: #fff; z-index: 10; } .right-nav{ width: 30px; color: #888; text-align: center; position: fixed; bottom: 0; right: 0; background-color: rgb(200, 200, 200); z-index: 9; } .city-scroll{padding-top: 50px;} .city-code{ background-color: #f7f7f7; } .city-list,.city-code{ height: 39px; line-height: 40px; padding: 0 30px 0 10px; overflow: hidden; border-bottom: 1px solid #c8c7cc; } .city-list-active{color:#007aff;} /*提示點(diǎn)擊的字母 */ .city-layer{ width: 70px; height: 70px; line-height: 70px; text-align: center; border-radius: 50%; color: #fff; background-color: rgba(0, 0, 0, .7); position: fixed; top: calc(50% - 35px); left:calc(50% - 35px); z-index: 11; } .layer-hide{display: none;}
JS
var city_list = require('./city.js'); Page({ data: { cityList: city_list.city, chooseCity: '您還未選擇機(jī)場(chǎng)!', isShowLayer: false, chooseIndex: 0, len: [], code: null, codeHeight: null, cityHeight:null, scrollTop: 0 }, onLoad (options) { var windowHeight = wx.getSystemInfoSync().windowHeight; var arr = []; this.data.cityList.forEach(current => arr.push(current.cityList.length + 1)); this.setData({ codeHeight: (windowHeight - 50) / this.data.cityList.length, cityHeight: windowHeight - 50, len: arr }); }, getCurrentCode(e){ var index = 0, sum = 0,self = this; for (var i = 0; i < this.data.cityList.length;i++){ if (this.data.cityList[i].code === e.target.dataset.code){ index = i break; } } for (var j = 0; j < index; j++) { sum += this.data.len[j]; } this.setData({ code: e.target.dataset.code, scrollTop: sum * 40, chooseIndex: index, isShowLayer: true }); setTimeout(() => { self.setData({ isShowLayer: false }) },500); }, getChooseCity(e){ this.setData({ chooseCity: e.target.dataset.city }); } })
總結(jié):
在onLoad函數(shù)中設(shè)置左側(cè)的展示高度和右側(cè)導(dǎo)航每一個(gè)字母所在盒子的高度;
getCurrentCode函數(shù)是獲取點(diǎn)擊字母的index,然后進(jìn)行提示以及500ms后關(guān)閉提示;
getChooseCity函數(shù)是獲取選擇的機(jī)場(chǎng),對(duì)chooseCity進(jìn)行賦值。
代碼簡(jiǎn)化:
var index = 0; for (var i = 0; i < this.data.cityList.length;i++){ if (this.data.cityList[i].code === e.target.dataset.code){ index = i break; } }
簡(jiǎn)化為:
添加data-index="{{index}}",減少循環(huán)的消耗:
<view bindtap="getCurrentCode" class="{{chooseIndex == index ? '.city-list-active' : ''}}" wx:for="{{cityList}}" style="height:{{codeHeight}}px" data-code="{{item.code}}" data-index="{{index}}">
var index = e.target.dataset.index;
DEMO下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS判斷頁(yè)面是否出現(xiàn)滾動(dòng)條的方法
這篇文章主要介紹了JS判斷頁(yè)面是否出現(xiàn)滾動(dòng)條的方法,涉及javascript針對(duì)頁(yè)面元素的讀取與判定實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07基于pako.js實(shí)現(xiàn)gzip的壓縮和解壓功能示例
這篇文章主要介紹了基于pako.js實(shí)現(xiàn)gzip的壓縮和解壓功能,結(jié)合具體實(shí)例形式分析了pako.js實(shí)現(xiàn)字符串壓縮與解壓縮的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06詳解JS 比較兩個(gè)Json對(duì)象的值是否相等的實(shí)例
這篇文章主要介紹了JS 比較兩個(gè)Json對(duì)象的值是否相等的實(shí)例,有需要的朋友可以參考一下2013-11-11短視頻(douyin)去水印工具的實(shí)現(xiàn)代碼
這篇文章主要介紹了市面上短視頻(douyin)"去水印"的工具原來(lái)是這樣實(shí)現(xiàn)的,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03JS 打印功能代碼可實(shí)現(xiàn)打印預(yù)覽、打印設(shè)置等
一個(gè)不錯(cuò)的JS 打印功能代碼,包括打印預(yù)覽、打印設(shè)置等,里面整合了很多知識(shí),是一個(gè)不錯(cuò)的學(xué)習(xí)案例2014-10-10傾力總結(jié)40條常見(jiàn)的移動(dòng)端Web頁(yè)面問(wèn)題解決方案
移動(dòng)端Web需要照顧觸摸操作的體驗(yàn),以及更多的屏幕旋轉(zhuǎn)與尺寸適配等問(wèn)題,非?,嵥?在這里為大家傾力總結(jié)40條常見(jiàn)的移動(dòng)端Web頁(yè)面問(wèn)題解決方案,歡迎收看收藏!2016-05-05javascript關(guān)于復(fù)選框的實(shí)用腳本代碼
javascript關(guān)于復(fù)選框的實(shí)用腳本代碼...2007-08-08