一,  兄弟组件间联动

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. FontAwesome 4.7.0 中完整的675个图标样式CSS参考

    FontAwesome 4.7.0 中完整的675个图标样式CSS参考 用法:首先引入CSS文件:<link href="https://maxcdn.bootstrapcdn.com ...

  2. Spring通过注解配置Bean

    @Component: 基本注解, 标识了一个受 Spring 管理的组件@Repository: 标识持久层组件@Service: 标识服务层(业务层)组件@Controller: 标识表现层组件 ...

  3. C#的math类的全部运算方法

    Abs 返回指定数字的绝对值.Acos 返回余弦值为指定数字的角度.Asin 返回正弦值为指定数字的角度.Atan 返回正切值为指定数字的角度.Atan2 返回正切值为两个指定数字的商的角度.BigM ...

  4. MVC框架-.net-摘

    MVC模式(三层架构模式)(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller) ...

  5. Html5与Css3知识点拾遗(一)

    1.元素 空元素: 可选的空格空格和斜杠 <img src="x.jpg" width="300" alt="pic" /> & ...

  6. 20169207《Linux内核原理与分析》第七周作业

    这周作业基本分为两个方面,第一方面,阅读学习教材「Linux内核设计与实现 (Linux Kernel Development)」第教材第9,10章.第二方面.学习MOOC「Linux内核分析」第五讲 ...

  7. Vue的路由设置

    一.路由基础介绍 1.什么是前端路由? 路由是根据不同的url地址展示不同的内容或页面 前端路由就是把不同路由对应不同的内容或页面的任务交给前端来做,之前是通过服务器根据url的不同返回不同的页面实现 ...

  8. bootstrap1相关学习文档

    <em>Bootstrap 框架</em>                                                    //倾斜 4.对齐 //设置文 ...

  9. PCI总线目标接口状态机设计

    module state_machine (devsel_l, trdy_l, stop_l, pci_ad_oe,      dts_oe, par_oe, bk_oe, pci_ad_en, hi ...

  10. 异步多线程 ASP.NET 同步调用异步 使用Result产生死锁

    一个方法调用了async方法,要将这个方法本身设计为async. public class BlogController : Controller { public async Task<Act ...