微信小程-實現(xiàn)城市列表選擇

實現(xiàn)效果預(yù)覽

1蓝厌、定位當(dāng)前城市

2驰徊、根據(jù)首字母定位到響應(yīng)的區(qū)域

3睁冬、點擊選中區(qū)域

目錄結(jié)構(gòu)



index.wxml 頁面

```<view class="searchLetter touchClass">

? <view class="thishotText" bindtap="hotCity">

? ? <view style="margin-top:0;">當(dāng)前</view>

? ? <view style="margin-top:0;">熱門</view>

? </view>

? <view wx:for="{{searchLetter}}" style="color:#F9C400;font-size:20rpx;" wx:key="index" data-letter="{{item.name}}" catchtouchend="clickLetter" >{{item.name}}</view>

</view>

<block wx:if="{{isShowLetter}}">

? <view class="showSlectedLetter">

? ? {{showLetter}}

? </view>

</block>

<scroll-view scroll-y="true" style="background-color: #fff;height:{{winHeight}}px" bindscroll="bindScroll"

scroll-into-view="{{scrollTopId}}" scroll-top="{{scrollTop}}">

? <view class="hotcity-common thisCity">當(dāng)前選擇城市</view>

? <view class="thisCityName">{{city}}</view>

? <view>

? ? <text class="hotcity hotcity-common">熱門城市</text>

? ? <view class="weui-grids">

? ? ? <block wx:for-items="{{hotcityList}}" wx:key='item'>

? ? ? ? <view class="weui-grid" data-cityCode="{{item.cityCode}}" data-city="{{item.city}}" bindtap="bindHotCity" >

? ? ? ? ? <view class="weui-grid__label">{{item.city}}</view>

? ? ? ? </view>

? ? ? </block>

? ? </view>

? </view>

? <view class="selection" wx:for="{{cityList}}" wx:key="{{item.initial}}">

? ? <view class="item_letter"? id="{{item.initial}}">

? ? <image src="/images/shopbgimg.png"></image>

? ? <text>? {{item.initial}}</text>


? ? </view>

? ? <view class="item_city"? wx:for="{{item.cityInfo}}" wx:for-item="ct" wx:key="{{ct.id}}" data-cityCode="{{ct.code}}" data-city="{{ct.city}}" bindtap="bindCity">

? ? ? {{ct.city}}

? ? </view>

? </view>

</scroll-view>

```

index.js 頁面

```var city = require('../../utils/city.js');

var app = getApp()

Page({

? data: {

? ? searchLetter: [],

? ? showLetter: "",

? ? winHeight: 0,

? ? // tHeight: 0,

? ? // bHeight: 0,

? ? cityList: [],

? ? isShowLetter: false,

? ? scrollTop: 0,//置頂高度

? ? scrollTopId: '',//置頂id

? ? city: "",

? ? hotcityList: [{ cityCode: 110000, city: '北京市' }, { cityCode: 310000, city: '上海市' }, { cityCode: 440100, city: '廣州市' }, { cityCode: 440300, city: '深圳市' }, { cityCode: 330100, city: '杭州市' }, { cityCode: 320100, city: '南京市' }, { cityCode: 420100, city: '武漢市' }, { cityCode: 410100, city: '鄭州市' }, { cityCode: 120000, city: '天津市' }, { cityCode: 610100, city: '西安市' }, { cityCode: 510100, city: '成都市' }, { cityCode: 500000, city: '重慶市' }],

? ? latitude: '',

? ? longitude: ''

? },

? onLoad: function (e) {

? ? // 生命周期函數(shù)--監(jiān)聽頁面加載

? ? var searchLetter = city.searchLetter;

? ? var cityList = city.cityList();

? ? var sysInfo = wx.getSystemInfoSync();

? ? var winHeight = sysInfo.windowHeight;

? ? var itemH = winHeight / searchLetter.length;

? ? var tempObj = [];

? ? for (var i = 0; i < searchLetter.length; i++) {

? ? ? var temp = {};

? ? ? temp.name = searchLetter[i];

? ? ? temp.tHeight = i * itemH;

? ? ? temp.bHeight = (i + 1) * itemH;

? ? ? tempObj.push(temp)

? ? }

? ? this.setData({

? ? ? winHeight: winHeight,

? ? ? itemH: itemH,

? ? ? searchLetter: tempObj,

? ? ? cityList: cityList,

? ? ? city: e.city

? ? })

? },

? onReady: function () {

? ? // 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成

? },

? onShow: function () {

? ? // 生命周期函數(shù)--監(jiān)聽頁面顯示

? ? this.getLocation()

? },

? clickLetter: function (e) {

? ? var showLetter = e.currentTarget.dataset.letter;

? ? this.setData({

? ? ? showLetter: showLetter,

? ? ? isShowLetter: true,

? ? ? scrollTopId: showLetter,

? ? })

? ? var that = this;

? ? setTimeout(function () {

? ? ? that.setData({

? ? ? ? isShowLetter: false

? ? ? })

? ? }, 1000)

? },

? // 微信獲得經(jīng)緯度

? getLocation: function () {

? ? let vm = this;

? ? var latitude = ''

? ? var longitude = ''

? ? wx.getLocation({

? ? ? type: 'wgs84',

? ? ? success: function (res) {

? ? ? ? latitude = res.latitude

? ? ? ? longitude = res.longitude

? ? ? ? var speed = res.speed

? ? ? ? var accuracy = res.accuracy;

? ? ? ? vm.setData({

? ? ? ? ? latitude: res.latitude,

? ? ? ? ? longitude: res.longitude

? ? ? ? })

? ? ? },

? ? ? fail: function (res) {

? ? ? }

? ? })

? ? //

? },

? //選擇城市

? bindCity: function (e) {

? ? this.setData({

? ? ? city: e.currentTarget.dataset.city,

? ? })

? },

? //選擇熱門城市

? bindHotCity: function (e) {

? ? this.setData({

? ? ? city: e.currentTarget.dataset.city

? ? })

? },

? //點擊熱門城市回到頂部

? hotCity: function () {

? ? this.setData({

? ? ? scrollTop: 0,

? ? })

? }

})

```

index.wxss 頁面

```.searchLetter {

? position: fixed;

? right: 0;

? width: 50rpx;

? text-align: center;

? justify-content: center;

? display: flex;

? flex-direction: column;

? color: #666;

? z-index: 1;

? padding-right: 30rpx;

? padding-left: 13rpx;

}

.searchLetter view {

? margin-top: 20rpx;

}

.touchClass {

? background-color: #fff;

? color: #fff;

? padding-top: 46rpx;

}

.showSlectedLetter {

? background-color: rgba(0, 0, 0, 0.5);

? color: #fff;

? display: flex;

? justify-content: center;

? align-items: center;

? position: fixed;

? top: 50%;

? left: 50%;

? margin: -100rpx;

? width: 200rpx;

? height: 200rpx;

? border-radius: 20rpx;

? font-size: 52rpx;

? z-index: 1;

}

.selection {

? display: flex;

? width: 100%;

? flex-direction: column;

? margin-top: 10rpx;

}

.item_letter {

? display: flex;

? height: 67rpx;

? line-height: 67rpx;

? padding-left: 69rpx;

? align-items: center;

? font-size: 20rpx;

? color: #333;

? position: relative

}

.item_letter image {

? height: 67rpx;

? width: 100%;

? position: absolute;

? top: 0;

? left: 0;

}

.item_letter text{

? position: absolute;

? top: 0;

? left: 69rpx;

}

.item_city {

? display: flex;

? background-color: #fff;

? height: 83rpx;

margin-left: 43rpx;

? align-items: center;

? border-bottom: 1rpx solid #EDEDED;

? font-size: 22rpx;

? color: #666;

}

.hotcity-common {

? font-size: 20rpx;

? color: #666;

? padding: 0 0 0 40rpx;

}

.thisCity {

? padding-top: 30rpx;

}

.thisCityName {

? display: inline-block;

? border: 1rpx solid #F9C400;

? padding: 10rpx;

? font-size: 20rpx;

? color: #F9C400;

? text-align: center;

? min-width: 92rpx;

? margin: 28rpx 0 25rpx 49rpx;

? border-radius:6rpx;


}

.thishotText {

? color: #F9C400;

? font-size: 20rpx;

? margin: 0 !important;

}

.slectCity {

? border-color:#F9C400 !important;

}

.slectCity view {

? color: #F9C400 !important;

}

.weui-grids{

? margin-left: 49rpx;

? overflow: hidden;

? padding-bottom: 18rpx;

}

.weui-grid {

? position: relative;

? float: left;

? width: 124rpx;

? box-sizing: border-box;

? border-radius: 6rpx;

? margin: 18rpx 29rpx 26rpx 0;

? height: 46rpx;

? line-height: 46rpx;

? background: #F4F4F4;

? font-size: 22rpx;

? color: #666

}

.weui-grid__label {

? display: block;

? text-align: center;

? color: #333;

? font-size: 24rpx;

? white-space: nowrap;

? text-overflow: ellipsis;

? overflow: hidden;

}

```

city.js文件里面是省市? 有需要私信镐确!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末祥诽,一起剝皮案震驚了整個濱河市黍聂,隨后出現(xiàn)的幾起案子躺苦,更是在濱河造成了極大的恐慌,老刑警劉巖产还,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匹厘,死亡現(xiàn)場離奇詭異,居然都是意外死亡脐区,警方通過查閱死者的電腦和手機愈诚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來牛隅,“玉大人炕柔,你說我怎么就攤上這事∶接叮” “怎么了匕累?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長默伍。 經(jīng)常有香客問我欢嘿,道長衰琐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任炼蹦,我火速辦了婚禮羡宙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘掐隐。我一直安慰自己狗热,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布虑省。 她就那樣靜靜地躺著匿刮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪慷妙。 梳的紋絲不亂的頭發(fā)上僻焚,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機與錄音膝擂,去河邊找鬼虑啤。 笑死,一個胖子當(dāng)著我的面吹牛架馋,可吹牛的內(nèi)容都是我干的狞山。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼叉寂,長吁一口氣:“原來是場噩夢啊……” “哼萍启!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起屏鳍,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤勘纯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后钓瞭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體驳遵,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年山涡,在試婚紗的時候發(fā)現(xiàn)自己被綠了堤结。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡鸭丛,死狀恐怖竞穷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鳞溉,我是刑警寧澤瘾带,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站穿挨,受9級特大地震影響月弛,放射性物質(zhì)發(fā)生泄漏肴盏。R本人自食惡果不足惜科盛,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一帽衙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贞绵,春花似錦厉萝、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至母蛛,卻和暖如春翩剪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背彩郊。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工前弯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人秫逝。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓恕出,卻偏偏與公主長得像,于是被迫代替她去往敵國和親违帆。 傳聞我的和親對象是個殘疾皇子浙巫,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,440評論 2 359