一,  兄弟组件间联动

1.  点击城市字母,左侧对应显示

给遍历的 字母 添加一个点击事件:

Alphabet.vue

@click="handleLetterClick"
    handleLetterClick (e) {
//获取对应的字母
this.$emit('change', e.target.innerHTML)
}

在 父组件City.vue 中,监听

<city-alphabet :cities="cities" @change="handleLetterChange"></city-alphabet>
    handleLetterChange (letter) {
this.letter = letter
}

然后转发给子CityList组件:

    <city-list :letter="letter"></city-list>

CityList组件,监听:

添加 ref属性      

<div class="area" v-for="(city,key) in cities" :key="key" :ref="key">
<div class="title border-topbottom">{{key}}</div>
<div class="item-list">
<div class="item border-bottom" v-for="c in city" :key="c.id">{{c.name}}</div>
</div>
</div>
 props: ['letter'],
watch: {
letter () {
if (this.letter) {
const element = this.$refs[this.letter][0]
// better-scrool方法,滚动区自动滚动到元素上
this.scroll.scrollToElement(element)
}
}

2.  拖动城市字母表,左侧城市对应滚动

给Alphabet.vue 字母列表绑定事件:

<ul class="list">
<li class="item" v-for="item in letters" :key="item"
@click="handleLetterClick"
@touchstart="handleTouchStart"
@touchmove="handleTouchMove"
@touchend="handleTouchEnd"
:ref = 'item'
>{{item}}
</li>
</ul>

事件说明:

touchstart : 触摸开始(手指放在触摸屏上)

touchmove : 拖动(手指在触摸屏上移动)

touchend : 触摸结束(手指从触摸屏上移开)

当前第几个字母   = (触摸处浏览器页面的垂直坐标  -  A 字母距离搜索栏底部的距离) / 每个字母的高度

  methods: {
handleTouchStart () {
//滑动开始
this.touchStatus = true
},
handleTouchMove (e) {
if (this.touchStatus) {
// A 字母距离搜索栏底部的距离
const startY = this.$refs['A'][0].offsetTop
      // 79 为:顶部搜索栏 的高度
const touchY = e.touches[0].clientY - 79
const index = Math.floor(touchY - startY) / 20
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}
},
handleTouchEnd () {
     // 滑动结束
this.touchStatus = false
}
}
<template>
<div>
<ul class="list">
<li class="item" v-for="item in letters" :key="item"
@click="handleLetterClick"
@touchstart="handleTouchStart"
@touchmove="handleTouchMove"
@touchend="handleTouchEnd"
:ref = 'item'
>{{item}}
</li>
</ul>
</div>
</template> <script>
export default {
name: 'CityAlphabet',
props: ['cities'],
data () {
return {
touchStatus: false
}
},
computed: {
letters () {
const letters = []
for (let i in this.cities) {
letters.push(i)
}
return letters
}
},
methods: {
handleLetterClick (e) {
this.$emit('change', e.target.innerHTML)
},
handleTouchStart () {
this.touchStatus = true
},
handleTouchMove (e) {
if (this.touchStatus) {
// A 字母距离搜索栏底部的距离
const startY = this.$refs['A'][0].offsetTop
const touchY = e.touches[0].clientY - 79
const index = Math.floor(touchY - startY) / 20
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}
},
handleTouchEnd () {
this.touchStatus = false
}
}
}
</script> <style lang="stylus" scoped>
@import "~styles/varibles.styl"
.list
position absolute
right 0
top 1.58rem
bottom 0
display flex
width .4rem
flex-direction column
justify-content center
.item
text-align center
line-height .4rem
color $bgColor
</style>

Alphabet.vue

二,列表切换性能优化

1.  滚动的优化

滚动重复执行运算:

this.$refs['A'][0].offsetTop

在 data 中定义 变量

  data () {
return {
startY: 0
}
}
添加生命周期钩子 updated:
  updated () {
this.startY = this.$refs['A'][0].offsetTop
}
     handleTouchMove (e) {
if (this.touchStatus) {
const touchY = e.touches[0].clientY - 79
const index = Math.floor(touchY - this.startY) / 20
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}
}

2. 节流限制 函数   handleTouchMove()  执行的频率

data中  定义   timer: null

  data () {
return {
touchStatus: false,
startY: 0,
timer: null
}

函数的改动:

    handleTouchMove (e) {
if (this.touchStatus) {
if (this.timer) {
clearTimeout(this.time)
}
this.timer = setTimeout(() => {
const touchY = e.touches[0].clientY - 79
const index = Math.floor(touchY - this.startY) / 20
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}, 16)
}
}

项目地址https://github.com/1417766861/Vue2.5-App/tree/master/Travel

Vue2.5开发去哪儿网App 城市列表开发之 兄弟组件间联动及列表性能优化的更多相关文章

  1. Vue2.5开发去哪儿网App 城市列表开发之 Vuex实现数据共享及高级使用

    一,数据共享 1.  安装: npm install vuex --save 2. 在src目录下 新建state文件夹,新建index.js文件 3. 创建一个 store import Vue f ...

  2. Vue2.5开发去哪儿网App 城市列表开发

     一,城市选择页面路由配置                                                                                        ...

  3. Vue2.5开发去哪儿网App 详情页面开发

    一,banner 图的设计 1. 新建detail的路由 import Detail from '@/pages/detail/Detail' ...... { path: '/detail', na ...

  4. Vue2.5 开发去哪儿网App

    Vue2.5开发去哪儿网App 技术栈和主要框架

  5. Vue2.5开发去哪儿网App 从零基础入门到实战项目

    第1章 课程介绍本章主要介绍课程的知识大纲,学习前提,讲授方式及预期收获. 1-1 课程简介 试看第2章 Vue 起步本章将快速讲解部分 Vue 基础语法,通过 TodoList 功能的编写,在熟悉基 ...

  6. Vue2.5开发去哪儿网App 第四章笔记 下

    1.解决非父子组件之间的传值问题 非父子组件传值(Bus/总线/发布订阅模式/观察者模式) 给 Vue类上挂在一个属性,然后创建vue实例时,实例就拥有了这个属性 Vue.prototype.bus ...

  7. Vue2.5开发去哪儿网App 第四章笔记 上

    一 .  组件细节知识点 1.  解决组件在h5中编码规范 例如 : table , ul , ol  等等 <table> <tbody> <row></r ...

  8. Vue2.5开发去哪儿网App 第二章笔记

    Vue完成  TodoList 1.默认方式 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. Vue2.5开发去哪儿网App 首页开发

    主页划 5 个组件,即 header  icon  swiper recommend weekend 一. header区域开发 1. 安装 stylus npm install stylus --s ...

随机推荐

  1. 2018.10.29 bzoj3718: [PA2014]Parking(树状数组)

    传送门 显然只用判断两个会相交的车会不会卡住就行了. 直接树状数组维护后缀最大值就行了. 代码: #include<bits/stdc++.h> using namespace std; ...

  2. Le Chapitre VIII

    J'appris bien vite à mieux connaître cette fleur. Il y avait toujours eu, sur la planète du petit pr ...

  3. python模块:sys

    # encoding: utf-8 # module sys # from (built-in) # by generator 1.145 """ This module ...

  4. yml的mybatis的sql查看;Mybatis+Springboot 控制台查看日志,Mybatis结合springboot打印日志

    1.配置如图 文件为yml mybatis: mapper-locations: classpath:com/springboot/transaction/mapper/*.xml configura ...

  5. nginx负载均衡的5种策略

    nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个. nginx的upstre ...

  6. python中的取整

    处理数据时,经常会遇到取整的问题,现总结如下 1,向下取整 int() >>>a = 3.1 >>>b = 3.7 >>>int(a) 3 > ...

  7. Java潜在的坑持续总结

    1.Java里如果有if (foo == 0),如果foo是null这里居然是会抛NPE异常而不是返回false: 2.Java里整形数值不能用==来比较,因为只有区间是[-128,127]的才能这么 ...

  8. Keras人工神经网络多分类(SGD)

    import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import ...

  9. 深入浅出javascript(三)封装和继承

    一.私有变量和公有变量 通过var修饰的是私有变量. 二.私有变量的访问方法 三.特权.公有和私有方法 一个例子: function f(name) { var name=name; //私有变量 t ...

  10. Unix传奇

    转自 http://coolshell.cn/articles/2322.html 了解过去,我们才能知其然,更知所以然.总结过去,我们才会知道我们明天该如何去规划,该如何去走.在时间的滚轮中,许许多 ...