当前位置:首页 > VUE

vue实现城市索引列表

2026-02-21 07:30:50VUE

实现城市索引列表的步骤

数据准备

城市数据通常以数组形式存储,每个城市对象包含名称和拼音首字母。例如:

const cities = [
  { name: '北京', pinyin: 'bj' },
  { name: '上海', pinyin: 'sh' },
  { name: '广州', pinyin: 'gz' },
  // ...
]

组件结构

创建Vue组件时,需要包含以下部分:

vue实现城市索引列表

  • 城市列表展示区域
  • 右侧字母索引栏
  • 顶部当前选中字母提示
<template>
  <div class="city-list">
    <div v-if="currentLetter" class="current-letter">{{ currentLetter }}</div>
    <div class="list-wrapper">
      <div v-for="(group, letter) in groupedCities" :key="letter">
        <div class="letter-title">{{ letter }}</div>
        <div 
          v-for="city in group" 
          :key="city.name"
          class="city-item"
          @click="selectCity(city)"
        >
          {{ city.name }}
        </div>
      </div>
    </div>
    <div class="index-bar">
      <div 
        v-for="letter in letters" 
        :key="letter"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd"
      >
        {{ letter }}
      </div>
    </div>
  </div>
</template>

数据处理

使用computed属性对城市数据进行分组:

computed: {
  groupedCities() {
    return this.cities.reduce((acc, city) => {
      const letter = city.pinyin[0].toUpperCase()
      if (!acc[letter]) acc[letter] = []
      acc[letter].push(city)
      return acc
    }, {})
  },
  letters() {
    return Object.keys(this.groupedCities).sort()
  }
}

触摸交互

实现右侧字母索引的触摸交互功能:

vue实现城市索引列表

methods: {
  handleTouchStart(e) {
    this.touchStartY = e.touches[0].clientY
    this.scrollToLetter(this.getLetterFromTouch(e))
  },
  handleTouchMove(e) {
    this.scrollToLetter(this.getLetterFromTouch(e))
  },
  getLetterFromTouch(e) {
    const index = Math.floor((e.touches[0].clientY - this.touchStartY) / 20)
    return this.letters[Math.max(0, Math.min(index, this.letters.length - 1))]
  },
  scrollToLetter(letter) {
    this.currentLetter = letter
    const element = document.querySelector(`[data-letter="${letter}"]`)
    if (element) element.scrollIntoView()
  }
}

样式设计

关键CSS样式示例:

.city-list {
  position: relative;
  height: 100vh;
}

.list-wrapper {
  height: calc(100% - 50px);
  overflow-y: auto;
}

.index-bar {
  position: fixed;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.current-letter {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  background-color: rgba(0,0,0,0.5);
  color: white;
}

性能优化

对于大量城市数据,可以考虑:

  • 使用虚拟滚动技术
  • 添加防抖处理触摸事件
  • 对字母索引进行节流处理

完整实现需要考虑浏览器兼容性和移动端触摸体验,可以通过添加更多交互反馈(如震动效果)来提升用户体验。

标签: 索引城市
分享给朋友:

相关文章

vue实现城市索引列表

vue实现城市索引列表

Vue 实现城市索引列表 数据结构准备 城市数据通常以数组形式存储,每个城市对象包含名称和拼音首字母。示例数据结构如下: const cities = [ { name: '北京', piny…

vue实现城市定位功能

vue实现城市定位功能

使用Geolocation API获取用户位置 在Vue中实现城市定位功能,可以通过浏览器提供的Geolocation API获取用户的经纬度坐标。需要在mounted生命周期钩子中调用相关方法。…

react如何获取数组的索引值

react如何获取数组的索引值

获取数组索引值的常见方法 在React中,处理数组并获取其索引值可以通过多种方式实现,具体取决于使用场景(如渲染列表、事件处理等)。以下是几种典型方法: 使用map方法时的索引参数 在渲染数组时…

php实现索引

php实现索引

PHP 实现索引的方法 使用数组索引 PHP 的数组可以自动或手动设置索引,索引可以是整数或字符串。 $array = array(10, 20, 30); echo $array[0]; //…

js索引实现

js索引实现

索引的基本概念 索引是一种数据结构,用于快速查找数据。在 JavaScript 中,索引通常用于优化数组或对象的查找操作,减少时间复杂度。 数组索引的实现 数组的索引可以通过直接访问下标实现,时间复…

jquery 索引

jquery 索引

jQuery 索引的基本概念 jQuery 索引通常指通过索引位置获取或操作 DOM 元素。jQuery 提供了多种方法来实现基于索引的操作,例如 eq()、:eq() 选择器或直接通过数组下标访问。…