最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

小程序實現(xiàn)帶索引的城市列表

 更新時間:2022年07月17日 15:18:51   作者:Song_Tutu  
這篇文章主要為大家詳細介紹了小程序實現(xiàn)帶索引的城市列表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了小程序實現(xiàn)帶索引的城市列表的具體代碼,供大家參考,具體內容如下

效果

網(wǎng)上找的很多的效果右邊的索引不會按左邊滑動區(qū)域高亮處理所以自己寫了個

代碼實現(xiàn)

因為我的城市數(shù)據(jù)沒有而項目里先有的是省市區(qū)代碼數(shù)據(jù)所以要先處理一下數(shù)據(jù)
用來獲取首字母:

//用來獲取首字母
import py from '../../utils/strChineseFirstPY'
checkCh(ch) {
? ? ? let uni = ch.charCodeAt(0);
? ? ? //如果不在漢字處理范圍之內,返回原字符,也可以調用自己的處理函數(shù)
? ? ? if (uni > 40869 || uni < 19968) {
? ? ? ? return ch;
? ? ? } //dealWithOthers(ch);
? ? ? //檢查是否是多音字,是按多音字處理,不是就直接在strChineseFirstPY字符串中找對應的首字母
? ? ? return (py.oMultiDiff[uni] ? py.oMultiDiff[uni] : (py.strChineseFirstPY.charAt(uni - 19968)));
? ? }

利用首字母講城市數(shù)據(jù)整理:

//用來獲取首字母
/**
? ? ?* @name:?
? ? ?* @test: test font
? ? ?* @msg:?
? ? ?* @param {*} data 數(shù)組
? ? ?* @param {*} field 依據(jù)那個字段排序
? ? ?* @return {*}
? ? ?*/
? ? data_letter_sort (data, field) {
? ? ? let list = new Array();
? ? ? let letter = ''
? ? ? for (let i = 0; i < data.length; i++) {
? ? ? ? // 首字母 轉 大寫英文
? ? ? ? letter = (data[i][field]).substr(0, 1).toUpperCase();
? ? ? ? // 創(chuàng)建 字母 分組
? ? ? ? if (!(letter in list)) {
? ? ? ? ? list[letter] = new Array();
? ? ? ? }
? ? ? ? // 字母 分組 添加 數(shù)據(jù)
? ? ? ? list[letter].push(data[i]);
? ? ? }
? ? ? ?// 轉換 格式 進行 排序;
? ? ? let resault = new Array();
? ? ? for (let key in list) {
? ? ? ? resault.push({
? ? ? ? ? letter: key,
? ? ? ? ? list: list[key]
? ? ? ? });
? ? ? }
? ? ? resault.sort(function (x, y) {
? ? ? ? return x.letter.charCodeAt(0) - y.letter.charCodeAt(0);
? ? ? });
? ? ? // 轉換 數(shù)據(jù) 格式
? ? ? let json_sort = {}
? ? ? for (let i = 0; i < resault.length; i++) {
? ? ? ? json_sort[resault[i].letter] = resault[i].list;
? ? ? }
? ? ? return json_sort;
? ? },

處理過后的數(shù)據(jù)為按首字母排序的一個對象

然后難點就在右邊的索引怎么按照左邊的區(qū)域來進行高亮顯示
首先我想的是獲取每個字母距離上邊的高度然后頁面滑動時進行高度判斷

//獲取城市數(shù)據(jù)的首字母列表
for (let key in this.cityList) {
? ? ? let obj = {
? ? ? ? py: key,
? ? ? ? active: false
? ? ? }
? ? ? if (obj.py == 'A') {
? ? ? ? obj.active = true
? ? ? }
? ? ? wx.createSelectorQuery().select('#'+ key).boundingClientRect(function(rect){
? ? ? ? obj.top = rect.top ? ? // 節(jié)點的上邊界坐標
? ? ? }).exec()
? ? ? //pylist是用來渲染右邊索引列表的
? ? ? this.pyList.push(obj);
? ? }
//滑動時間
scroll(e) {
? ? ? let scrollTop = e.target.scrollTop
? ? ? this.pyList.map(item => {
? ? ? ? if ((scrollTop - item.top)>-76) {
? ? ? ? ? this.pyList.map(item => {
? ? ? ? ? //active是用來是否高亮顯示
? ? ? ? ? ? item.active = false
? ? ? ? ? })
? ? ? ? ? item.active = true
? ? ? ? ? return
? ? ? ? }
? ? ? })
? ? },

整體代碼

<!--
?* @Descripttion:?
?* @version:?
?* @Author: Songtutu
?* @Date: 2021-04-06 10:30:36
?* @LastEditors: Songtutu
?* @LastEditTime: 2021-04-08 13:40:32
-->
<template>
? <div class="main">
? ? <div class="search">
? ? ? ? <div class="search-box">
? ? ? ? ? <img :src="imglist.map_search"/>
? ? ? ? ? <input placeholder="請輸入城市名稱" @focus="focus" :focus="true" v-model="search" type="digit" -splaceholdertyle="font-size:14px;color: rgba(0, 0, 0, 0.3);" confirm-type="search" @confirm='doSearch'/>
? ? ? ? </div>
? ? ? </div>
? ? <!-- 城市列表 -->
? ? <scroll-view scroll-y="true" scroll-with-animation="true" enable-back-to-top="true" class="city-list" @scroll="scroll" :scroll-top="scrollTop" v-show="searchList.length == 0">
? ? ??
? ? ? <div class="dw-box">
? ? ? ? <div>定位城市:{{city}}</div>
? ? ? ? <img :src="imglist.map_sx" @click="sx()"/>
? ? ? </div>
? ? ? <div :id='key' class="city-group" v-for="(value, key, index) in cityList" :key="index">
? ? ? ? <div ?class="city-group-title">{{key}}</div>
? ? ? ? <div class="city-item" :id="i == value.length -1?key:''" v-for="(item, i) in value" :key="i">
? ? ? ? ? {{item.cityName}}
? ? ? ? </div>
? ? ? </div>
? ? </scroll-view>
? ? <div class="search-list" v-show="searchList.length != 0">
? ? ? <div class="city-item" v-for="(item, i) in searchList" :key="i">
? ? ? ? {{item.cityName}}
? ? ? </div>
? ? </div>
? ? <div class="py-box" v-show="searchList.length == 0">
? ? ? <div class="py-item" :class="item.active?'active':''" v-for="(item, index) in pyList" :key="index" @click="clickPy(item, index)">{{item.py}}</div>
? ? </div>
? </div>
</template>
<script>
import py from '../../utils/strChineseFirstPY'
import area from '../../utils/area1'
export default {
? data() {
? ? return {
? ? ? cityList: [],
? ? ? imglist: [],
? ? ? search:'',
? ? ? city: '',
? ? ? pyList: [],
? ? ? scrollTop: 0,
? ? ? searchList: []
? ? }
? },
? created() {
? ? let title = "選擇城市"
? ? let frontColor = "#000000"
? ? let backgroundColor = "#ffffff"
? ? wx.setNavigationBarTitle({
? ? ? title
? ? })
? ? wx.setNavigationBarColor({
? ? ? frontColor, //前景顏色值,包括按鈕、標題、狀態(tài)欄的顏色,僅支持 #ffffff 和 #000000,
? ? ? backgroundColor
? ? });
? ? const imgurl = "圖片根目錄"
? ? this.imglist = {
? ? ? map_search: `${imgurl}map_search.png`,
? ? ? map_sx: `${imgurl}map_sx.png`
? ? }
? ? area.region.map(item => {
? ? ? item.mallCityList.map(i => {
? ? ? ? this.cityList.push(i)
? ? ? })
? ? })
? ? this.cityList.map(item => {
? ? ? item.py = this.checkCh(item.cityName.charAt(0))
? ? })
? ? this.cityList = this.data_letter_sort(this.cityList, 'py')
? ? console.log(this.cityList)
? },
? mounted() {
? ? for (let key in this.cityList) {
? ? ? let obj = {
? ? ? ? py: key,
? ? ? ? active: false
? ? ? }
? ? ? if (obj.py == 'A') {
? ? ? ? obj.active = true
? ? ? }
? ? ? wx.createSelectorQuery().select('#'+ key).boundingClientRect(function(rect){
? ? ? ? obj.top = rect.top ? ? // 節(jié)點的上邊界坐標
? ? ? }).exec()
? ? ? this.pyList.push(obj);
? ? }
? },
? methods: {
? ? focus() {
? ? ? if (!this.search) {
? ? ? ? this.searchList = []
? ? ? }
? ? },
? ? doSearch() {
? ? ? this.searchList = []
? ? ? for(let key in this.cityList) {
? ? ? ? this.cityList[key].map(item => {
? ? ? ? ? if (item.cityName.indexOf(this.search) != -1) {
? ? ? ? ? ? this.searchList.push(item)
? ? ? ? ? }
? ? ? ? })
? ? ? }
? ? },
? ? clickPy(item) {
? ? ? this.scrollTop = item.top - 44
? ? },
? ? scroll(e) {
? ? ? let scrollTop = e.target.scrollTop
? ? ? this.pyList.map(item => {
? ? ? ? if ((scrollTop - item.top)>-76) {
? ? ? ? ? this.pyList.map(item => {
? ? ? ? ? ? item.active = false
? ? ? ? ? })
? ? ? ? ? item.active = true
? ? ? ? ? return
? ? ? ? }
? ? ? })
? ? },
? ? sx() {
? ? ? this.getLocation()
? ? },
? ? /**
? ? ?* 定位授權
? ? ?*/
? ? getLocation() {
? ? ? const that = this;
? ? ? //調用自帶位置獲取
? ? ? wx.getSetting({
? ? ? ? success(res) {
? ? ? ? ? if (!res.authSetting["scope.userLocation"]) {
? ? ? ? ? ? wx.authorize({
? ? ? ? ? ? ? scope: "scope.userLocation",
? ? ? ? ? ? ? success() {
? ? ? ? ? ? ? ? that.localDetail();
? ? ? ? ? ? ? },
? ? ? ? ? ? ? fail() {
? ? ? ? ? ? ? ? wx.showModal({
? ? ? ? ? ? ? ? ? title: "提示",
? ? ? ? ? ? ? ? ? content: "獲取當前位置需要授權哦~",
? ? ? ? ? ? ? ? ? success(res) {
? ? ? ? ? ? ? ? ? ? if (res.confirm) {
? ? ? ? ? ? ? ? ? ? ? wx.openSetting({
? ? ? ? ? ? ? ? ? ? ? ? success: res => {
? ? ? ? ? ? ? ? ? ? ? ? ? if (res.authSetting["scope.userLocation"]) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? that.localDetail();
? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? } else if (res.cancel) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? } else {
? ? ? ? ? ? that.localDetail();
? ? ? ? ? }
? ? ? ? }
? ? ? });
? ? },
? ? localDetail() {
? ? ? wx.getLocation({
? ? ? ? type: "gcj02", //返回可以用于wx.openLocation的經緯度
? ? ? ? success: function(res) {
? ? ? ? ? console.log(res)
? ? ? ? }
? ? ? });
? ? },
? ? /**
? ? ?* @name:?
? ? ?* @test: test font
? ? ?* @msg:?
? ? ?* @param {*} data 數(shù)組
? ? ?* @param {*} field 依據(jù)那個字段排序
? ? ?* @return {*}
? ? ?*/
? ? data_letter_sort (data, field) {
? ? ? let list = new Array();
? ? ? let letter = ''
? ? ? for (let i = 0; i < data.length; i++) {
? ? ? ? // 首字母 轉 大寫英文
? ? ? ? letter = (data[i][field]).substr(0, 1).toUpperCase();
? ? ? ? // 創(chuàng)建 字母 分組
? ? ? ? if (!(letter in list)) {
? ? ? ? ? list[letter] = new Array();
? ? ? ? }
? ? ? ? // 字母 分組 添加 數(shù)據(jù)
? ? ? ? list[letter].push(data[i]);
? ? ? }
? ? ? ?// 轉換 格式 進行 排序;
? ? ? let resault = new Array();
? ? ? for (let key in list) {
? ? ? ? resault.push({
? ? ? ? ? letter: key,
? ? ? ? ? list: list[key]
? ? ? ? });
? ? ? }
? ? ? resault.sort(function (x, y) {
? ? ? ? return x.letter.charCodeAt(0) - y.letter.charCodeAt(0);
? ? ? });
? ? ? // 轉換 數(shù)據(jù) 格式
? ? ? let json_sort = {}
? ? ? for (let i = 0; i < resault.length; i++) {
? ? ? ? json_sort[resault[i].letter] = resault[i].list;
? ? ? }
? ? ? return json_sort;
? ? },
? ? checkCh(ch) {
? ? ? let uni = ch.charCodeAt(0);
? ? ? //如果不在漢字處理范圍之內,返回原字符,也可以調用自己的處理函數(shù)
? ? ? if (uni > 40869 || uni < 19968) {
? ? ? ? return ch;
? ? ? } //dealWithOthers(ch);
? ? ? //檢查是否是多音字,是按多音字處理,不是就直接在strChineseFirstPY字符串中找對應的首字母
? ? ? return (py.oMultiDiff[uni] ? py.oMultiDiff[uni] : (py.strChineseFirstPY.charAt(uni - 19968)));
? ? }
? }
}
</script>
<style scoped>
.search-list {
? margin-top: 68rpx;
? background: white;
? width: 100%;
? height: auto;
}
.active {
? width: 16px;
? height: 16px;
? background: #005F28;
? line-height: 16px;
? border-radius: 50%;
? color: white;
? text-align: center;
}
.py-box {
? width: 16px;
? text-align: center;
? height: auto;
? position: fixed;
? top: 142px;
? font-weight: 400;
? color: rgba(0, 0, 0, 0.4);
? font-size: 14px;
? right:9px;
}
.search {
? position: fixed;
? top: 0;
? left: 0;
? width: 100%;
? z-index: 99;
? background: white;
? padding-bottom: 8px;
}
.city-item {
? margin: 0 32px 0px 16px;
? height: 40px;
? line-height: 40px;
? font-size: 14px;
? font-weight: 400;
? color: rgba(0, 0, 0, 0.9);
? border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}
.city-list {
? background: white;
? height: 100vh;
? position: relative;
}
.city-list .city-group-title {
? position: sticky;
? top: 33px;
? width: 100%;
? font-size: 14px;
? font-weight: 400;
? color: rgba(0, 0, 0, 0.4);
? padding: 0px 16px;
? height: 32px;
? line-height: 32px;
? background: #F5F5F5;
? box-sizing: border-box;
}
.main{
? min-height: 100vh;
? background: #F5F5F5;;
? padding-top: 6px;
}
.search-box {
? margin-top: 6px;
? margin: 0 auto;
? width: 343px;
? display: flex;
? height: auto;
? padding: 6px 8px;
? background: rgba(0, 0, 0, 0.05);
? border-radius: 4px;
}
.search-box img {
? width: 16px;
? height: 16px;
? margin-top: 2px;
? margin-right: 4px;
}
.search-box input {
? height: 20px;
? font-size: 14px;
? font-weight: 400;
? color: rgba(0, 0, 0, 0.9);
? line-height: 20px;
}
.dw-box {
? margin-top: 32px;
? height: 44px;
? display: flex;
? line-height: 44px;
? padding: 0 16px;
? font-size: 14px;
? font-weight: 400;
? color: #005F28;

}
.dw-box div {
? flex: 1;
? text-align: left;
}
.dw-box img {
? margin-top: 14px;
? width: 16px;
? height: 16px;
}
</style>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • javascript 定時器工作原理分析

    javascript 定時器工作原理分析

    說到 javascript 中的定時器,我們肯定會想到 setTimeout() 和 setInterval() 這兩個函數(shù)。本文將從 事件循環(huán)(Event Loop) 的角度來分析兩者的工作原理和區(qū)別
    2016-12-12
  • 更靠譜的H5橫豎屏檢測方法(js代碼)

    更靠譜的H5橫豎屏檢測方法(js代碼)

    這篇文章主要為大家詳細介紹了更靠譜的橫豎屏檢測方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • js實現(xiàn)的常用的左側導航效果

    js實現(xiàn)的常用的左側導航效果

    使用js簡單實現(xiàn)下常用的左側導航效果為提高導航性能而生,各位朋友可以參考應用,希望對大家有所幫助
    2013-10-10
  • JavaScript圖片上傳并預覽的完整實例

    JavaScript圖片上傳并預覽的完整實例

    上傳是個老生常談的話題了,多數(shù)情況下各位想必用的是uplodify,webUploader之類的插件,這篇文章主要給大家介紹了關于JavaScript圖片上傳并預覽的相關資料,需要的朋友可以參考下
    2022-01-01
  • 基于ES6作用域和解構賦值詳解

    基于ES6作用域和解構賦值詳解

    下面小編就為大家?guī)硪黄贓S6作用域和解構賦值詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 拖動層

    拖動層

    [紅色]拖動層...
    2006-10-10
  • 簡單談談offsetleft、offsetTop和offsetParent

    簡單談談offsetleft、offsetTop和offsetParent

    這篇文章主要給大家介紹了offsetleft、offsetTop和offsetParent的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 使用Vite從零搭建前端項目的詳細過程

    使用Vite從零搭建前端項目的詳細過程

    這篇文章主要介紹了使用Vite從零搭建前端項目的詳細過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • JS/jQuery實現(xiàn)獲取時間的方法及常用類完整示例

    JS/jQuery實現(xiàn)獲取時間的方法及常用類完整示例

    這篇文章主要介紹了JS/jQuery實現(xiàn)獲取時間的方法及常用類,結合完整實例形式分析了javascript針對日期時間的獲取、轉換、計算與檢測相關操作技巧,需要的朋友可以參考下
    2019-03-03
  • 微信小程序實現(xiàn)發(fā)送驗證碼按鈕效果

    微信小程序實現(xiàn)發(fā)送驗證碼按鈕效果

    這篇文章主要為大家詳細介紹了微信小程序實現(xiàn)發(fā)送驗證碼按鈕效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論

柳州市| 霍林郭勒市| 榆中县| 花莲县| 安乡县| 库车县| 湛江市| 广宁县| 济南市| 阿克苏市| 惠来县| 吕梁市| 江津市| 清镇市| 玛沁县| 城固县| 浠水县| 顺义区| 乐平市| 固始县| 龙胜| 加查县| 太仓市| 鄂伦春自治旗| 清流县| 洛川县| 晋宁县| 旬邑县| 宣威市| 嘉定区| 文水县| 色达县| 新密市| 邓州市| 黔东| 邛崃市| 炉霍县| 禹州市| 金昌市| 伊春市| 梁河县|