当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现全国机场索引列表

微信小程序实现全国机场索引列表

2018年02月08日  | 移动技术网IT编程  | 我要评论
本文为大家分享了微信小程序实现mui索引列表的具体代码,供大家参考,具体内容如下 效果展示图 实现的原理 ‘当前选择机场'和右侧的导航栏采用的是固定定位;

本文为大家分享了微信小程序实现mui索引列表的具体代码,供大家参考,具体内容如下

效果展示图

这里写图片描述

实现的原理

  • ‘当前选择机场'和右侧的导航栏采用的是固定定位;
  • 左侧的展示窗口的滚动采用的是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">当前选择机场:{{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;}

/*提示点击的字母 */
.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: '您还未选择机场!',
 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 });
 }
})

总结:

在onload函数中设置左侧的展示高度和右侧导航每一个字母所在盒子的高度;
getcurrentcode函数是获取点击字母的index,然后进行提示以及500ms后关闭提示;
getchoosecity函数是获取选择的机场,对choosecity进行赋值。

代码简化:

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}}",减少循环的消耗:
<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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网