微信小程序如何實(shí)現(xiàn)全國機(jī)場索引列表

這篇文章主要介紹微信小程序如何實(shí)現(xiàn)全國機(jī)場索引列表,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站建設(shè)、新?lián)峋W(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、新?lián)峋W(wǎng)絡(luò)營銷、新?lián)崞髽I(yè)策劃、新?lián)崞放乒P(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供新?lián)峤ㄕ?/a>搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:bm7419.com

效果展示圖

微信小程序如何實(shí)現(xiàn)全國機(jī)場索引列表

實(shí)現(xiàn)的原理

WXML

<view class="right-nav">
 <view bindtap="getCurrentCode" class="{{chooseIndex == index ? '.city-list-active' : ''}}" wx:for="{{cityList}}"  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ī)場:{{chooseCity}}</view>
<scroll-view class="city-scroll" scroll-y="true"  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ī)場!',
 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ī)場,對(duì)chooseCity進(jì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;
 }
}

簡化為:

添加data-index="{{index}}",減少循環(huán)的消耗:
<view bindtap="getCurrentCode" class="{{chooseIndex == index ? '.city-list-active' : ''}}" wx:for="{{cityList}}"  data-code="{{item.code}}" data-index="{{index}}">
var index = e.target.dataset.index;

以上是“微信小程序如何實(shí)現(xiàn)全國機(jī)場索引列表”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞名稱:微信小程序如何實(shí)現(xiàn)全國機(jī)場索引列表
本文路徑:http://bm7419.com/article38/pcsgpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化網(wǎng)站排名、響應(yīng)式網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、建站公司、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營