小程序按照字母選擇城市

wxml

<view class="container">
    <!-- search -->
    <view class="search-bar">
        <image src="/static/images/chooseAddr/search.png" class="search-img"></image>
        <input type="text" value='{{inputVal}}' bindinput='getValue' placeholder='城市名/拼音' placeholder-class="placeholder"></input>
    </view>

    <!-- 搜索結(jié)果列表 -->
    <view class='list-detail' wx:if="{{searchList.length>0}}">
        <scroll-view scroll-y="true" style="height:100%;">
            <view class='item-nav' wx:for="{{searchList}}" catchtap='getCity' data-items="{{item}}" wx:key="key">
                <view class='title'>{{item.city_name}}</view>
            </view>
        </scroll-view>
    </view>

    <!-- 城市列表 -->
    <view class='list-city' wx:if="{{searchList.length==0}}">
        <scroll-view scroll-y="true" style="height:100%;" scroll-into-view="{{scrollTopId}}" scroll-with-animation="true" enable-back-to-top="true">
            <view class='item fullname region'>
                <view style="color:#333">
                    <view style="display:inline-block;padding:26rpx 0;" catchtap="getCity" data-items="{{current_city}}">當(dāng)前城市:
                        <view class="top-now">
                            <image src="/static/images/chooseAddr/ic02.png" class="now-img"></image>{{city}}
                        </view>
                    </view>
                </view>
            </view>
            <!-- 全部 -->
            <view class='item' wx:for="{{cityList}}" wx:for-index="idx" wx:for-item="group" wx:key="key">
                <view class='py' id="{{idx}}">{{idx}}</view>
                <view class="fullname" wx:for="{{group}}" wx:key="key" data-items="{{item}}" bindtap='getCity'>{{item.city_name}}</view>
            </view>
        </scroll-view>

        <!-- 首字母 -->
        <view class='city-py' bindtouchstart="tStart" bindtouchend="tEnd" catchtouchmove="tMove">
            <view wx:for="{{_py}}" wx:key="key" bindtouchstart="getPy" bindtouchend="setPy" id="{{item}}" class="city-py-item {{showPy==item?'act-city-py':''}}">
                {{item}}<view class="showPy" hidden="{{hidden}}" wx:if="{{showPy==item}}"><view>{{showPy}}</view></view>
            </view>
        </view>
    </view>
</view>

wxss

page {
  height: 100%;
}

.show{
  position: absolute;
  background-color: #5C5B5A;
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  opacity: 0.7;
}

.container {
  /* padding-top: 100rpx; */
  box-sizing: border-box;
  height: 100%;
}

.search-bar {
  font-size: 26rpx;
  color: #999999;
  background-color: #EFEFEF;
  display: flex;
  border-radius: 45rpx;
  width: 90%;
  margin: 20rpx auto 0 auto;
  padding: 15rpx 20rpx;
  align-items: center;
}

.search-img{
  width: 28rpx;
  height: 28rpx;
  margin: 5rpx 10rpx;
}

.search-bar .clear {
  position: absolute;
  right: 30rpx;
  bottom: 25rpx;
  z-index: 2;
  height: 50rpx;
  width: 50rpx;
  opacity: 0.35;
}

.search-bar .split {
  padding: 0 30rpx;
}

.search-bar .picker-opts {
  box-sizing: border-box;
  white-space: nowrap;
  float: left;
  height: 100rpx;
  line-height: 100rpx;
}

.search-bar input {
  width: 90%;
}

.search-bar .placeholder {
  color: #999999;
}

.list-detail{
  z-index: 1000;
  position: relative;
}

.list-detail, .list-city {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: #fff;
}

.list-detail {
  border-bottom: 1rpx solid #eee;
  padding: 20rpx 20rp  0 0;
  font-size: 24rpx;
  margin-right: 50rpx;
}

.item-nav {
  border-bottom: 1rpx solid #eee;
  padding: 20rpx 15rpx;
  font-size: 24rpx;
}

.list-detail .title, .history .title {
  color: #000;
  line-height: 45rpx;
}

.list-detail .address, .history .address {
  font-size: 28rpx;
  color: #aaa;
  white-space: normal;
}

/* 城市列表 */

.list-city {
  position: relative;
}

.city-item{
  padding-bottom:30rpx
}

.py {
  box-sizing: border-box;
  font-size: 26rpx;
  color: #aaa;
  padding: 10rpx 25rpx;
}

.fullname {
  box-sizing: border-box;
  font-size: 26rpx;
  color: #555;
  padding: 20rpx;
}
.top-now{
  border: 1rpx solid #ECECEC;
  display: inline-block;
  border-radius: 40rpx;
  padding: 16rpx 30rpx;
  font-size: 25rpx;
  color: #333333;
}
.now-img{
  width: 20rpx;
  height: 24rpx;
  margin-right: 6rpx;
}
.region{
  border-bottom: 1rpx solid #eee;
}

.city-py {
  position: fixed;
  top: 29%;
  right: 10rpx;
  /* margin-top: -360rpx; */
}

.city-py-item {
  font-size: 22rpx;
  width: 32rpx;
  height: 32rpx;
  line-height: 32rpx;
  text-align: center;
  color: #333;
  margin-top: 2rpx;
  font-weight: normal;
}

.act-city-py {
  font-size: 22rpx;
  color: #fff!important;
  border-radius: 36rpx;
  background-color: #FF8700;
}
.showPy {
  position: absolute;
  /* top: 0; */
  color: #fff;
  right: 0;
  z-index: 3;
  width: 80rpx;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  margin-right: 80rpx;
  margin-top: -56rpx;
  background-color: #FF8700;
  border-radius: 80rpx 80rpx 0;
  -webkit-transform: rotate(315deg);
  -ms-transform: rotate(315deg);
  -o-transform: rotate(315deg);
  transform: rotate(315deg);
}

.showPy view{
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

.hot-city {
  border: 1rpx solid #ECECEC;
  display: inline-block;
  border-radius: 40rpx;
  padding: 16rpx 0;
  width: 150rpx;
  font-size: 25rpx;
  color: #333333;
}

.hot-item{
  padding: 40rpx 50rpx 20rpx 20rpx;
  display: grid;
  grid-template-columns: 22% 22% 22% 22%;
  text-align: center;
  grid-gap: 36rpx 20rpx;
}
.hot-down{
  /* display: inline-block; */
  float: right;
  margin-right: 45rpx;
}
.hot-down image{
  width: 20rpx;
  height: 10rpx;
}

js

data: {
        city: '',
        _py: ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"], //"hot",
        searchList: [],//搜索列表
        hidden: true,
        showPy: '',
        showPy_height: '0',
        cityList: {
                "A": [
                    {
                        city_code: "340800",
                        city_name: "安慶",
                        first_letter: "A",
                        province: "安徽省",
                    }, {
                        city_code: "340800",
                        city_name: "安慶",
                        first_letter: "A",
                        province: "安徽省",
                    }, {
                        city_code: "340800",
                        city_name: "安慶",
                        first_letter: "A",
                        province: "安徽省",
                    }, {
                        city_code: "340800",
                        city_name: "安慶",
                        first_letter: "A",
                        province: "安徽省",
                    }, {
                        city_code: "340800",
                        city_name: "安慶",
                        first_letter: "A",
                        province: "安徽省",
                    }, {
                        city_code: "340800",
                        city_name: "安慶",
                        first_letter: "A",
                        province: "安徽省",
                    }, {
                        city_code: "340800",
                        city_name: "安慶",
                        first_letter: "A",
                        province: "安徽省",
                    }],
                "B": [
                    {
                        city_code: "340800",
                        city_name: "北京",
                        first_letter: "B",
                        province: "北京",
                    }, {
                        city_code: "340800",
                        city_name: "北京",
                        first_letter: "B",
                        province: "北京",
                    }, {
                        city_code: "340800",
                        city_name: "北京",
                        first_letter: "B",
                        province: "北京",
                    }, {
                        city_code: "340800",
                        city_name: "北京",
                        first_letter: "B",
                        province: "北京",
                    }, {
                        city_code: "340800",
                        city_name: "北京",
                        first_letter: "B",
                        province: "北京",
                    }, {
                        city_code: "340800",
                        city_name: "北京",
                        first_letter: "B",
                        province: "北京",
                    }, {
                        city_code: "340800",
                        city_name: "北京",
                        first_letter: "B",
                        province: "北京",
                    }]
            }//懶得寫了嗡官,反正按照此數(shù)據(jù)格式就可以
    },
 //獲取文字信息
    getPy: function (e) {
        this.setData({
            hidden: false,
            showPy: e.target.id,
        })
    },

    setPy: function (e) {
        // console.log(e)
        this.setData({
            // hidden: true,
            scrollTopId: this.data.showPy
        })
    },

    //滑動(dòng)選擇城市
    tMove: function (e) {
        console.log(e)
        const that = this
        let sum = that.data.windowHeight * 0.29
        let _py = that.data._py
        let count = 0
        var query = wx.createSelectorQuery()
        query.select('.city-py').boundingClientRect((res) => {
            // console.log(res.height/26)
            count = res.height / 22
            let temp = Math.ceil((e.touches[0].pageY - sum) / count)
            console.log(temp)
            console.log(_py[temp - 1])

            that.setData({
                showPy_height: sum + Math.ceil((e.touches[0].pageY - sum) / count) * 32,
                showPy: _py[temp - 1]
            })
        }).exec()
    },

    //觸發(fā)全部開始選擇
    tStart: function () {
        this.setData({
            hidden: false
        })
    },

    //觸發(fā)結(jié)束選擇
    tEnd: function () {
        this.setData({
            hidden: true,
            scrollTopId: this.data.showPy
        })
    },
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末闲延,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子志膀,更是在濱河造成了極大的恐慌佑吝,老刑警劉巖坐昙,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異芋忿,居然都是意外死亡炸客,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進(jìn)店門戈钢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來痹仙,“玉大人,你說我怎么就攤上這事殉了】觯” “怎么了?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長抖所。 經(jīng)常有香客問我梨州,道長痕囱,這世上最難降的妖魔是什么田轧? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮鞍恢,結(jié)果婚禮上傻粘,老公的妹妹穿的比我還像新娘。我一直安慰自己帮掉,他們只是感情好弦悉,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蟆炊,像睡著了一般稽莉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上涩搓,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天污秆,我揣著相機(jī)與錄音,去河邊找鬼昧甘。 笑死良拼,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的充边。 我是一名探鬼主播庸推,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼浇冰!你這毒婦竟也來了贬媒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤肘习,失蹤者是張志新(化名)和其女友劉穎际乘,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體井厌,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蚓庭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仅仆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片器赞。...
    茶點(diǎn)故事閱讀 40,567評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖墓拜,靈堂內(nèi)的尸體忽然破棺而出港柜,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布夏醉,位于F島的核電站爽锥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏畔柔。R本人自食惡果不足惜氯夷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望靶擦。 院中可真熱鬧腮考,春花似錦、人聲如沸玄捕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枚粘。三九已至馅闽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間馍迄,已是汗流浹背福也。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留柬姚,地道東北人拟杉。 一個(gè)月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像量承,于是被迫代替她去往敵國和親搬设。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容