前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VUE开发一个组件——Vue H5城市选择控件

VUE开发一个组件——Vue H5城市选择控件

作者头像
Javanx
发布2019-09-04 16:42:43
2.1K0
发布2019-09-04 16:42:43
举报
文章被收录于专栏:web秀web秀

前言

昨天用《VUE开发一个组件——Vue PC城市选择控件》 , 有朋友说需要用字母筛选,其实已经用字母筛选了,不过是每4个,没有对单个,所以H5我们就用单个字母快速查找

数据部分

《VUE开发一个组件——Vue PC城市选择控件》 一样,但是不需要每4个再分组了。

代码语言:javascript
复制
cityListData(){
  let map = {};
  let temps = [];
  this.dataList.map(item=>{
      if(item.airportCode){
          let ekey = item.airportCode.charAt(0).toUpperCase();
          temps = map[ekey] || [];
          temps.push({
              airportCode: item.airportCode,
              airportName: item.cityName
          });
          map[ekey] = temps;
      }
  })
  let list = [];
  for(let gkey in map) {
      list.push({
          ckey: gkey,
          cityList: map[gkey]
      })
  }
  list = list.sort((li1, li2)=> li1.ckey.charCodeAt(0) - li2.ckey.charCodeAt(0));
  return list;
},
cityListKey(){
  let cityListKey = [];
  this.cityListData.map(item=>{
      cityListKey.push(item.ckey);
  })
  return cityListKey;
}

cityListData整理后的数据,cityListKey是所有字母。

页面部分

代码语言:javascript
复制
<div id="app">
    <div class="city city-wap">
      <div class="search">
        <input type="text" placeholder="搜索条件">
      </div>
      <div class="city-list">
        <div class="block-60"></div>
        <div v-for="item in cityListData" class="clearfix">
          <p :id="item.ckey">{{item.ckey}}</p>
          <ul>
            <li v-for="ritem in item.cityList">{{ritem.airportName}}</li>
          </ul>
        </div>
      </div>
      <div class="filter">
        <div v-for="item in cityListKey" @click="switchKey(item)">{{item}}</div>
      </div>
      <div class="active-key" v-if="activeKey">{{activeKey}}</div>
    </div>
</div>

search没有开发(用关键字搜索)、city-list遍历cityListDatafilter筛选字母列表、active-key提示当前选择是那个字母。

代码语言:javascript
复制
.city-wap{
  color: #3b4f62;
  .clearfix{
    &:after{
      content: '';
      display: block;
      clear: both;
    }
  }
  p{
    background: #fff;
    margin-bottom: 10px;
    padding: 0 12px;
  }
  .search{
    position: fixed;
    top: 0;
    box-shadow: 0 1px 3px 0 rgba(59,79,98,0.1);
    width: 100%;
    height: 50px;
    input{
      line-height: 50px;
      width: 100%;
      border: none;
      box-shadow: none;
      padding: 0 10px;
      &:focus { 
        outline: none; 
      } 
    }
  }
  .city-list{
    .block-60{
      height: 60px;
    }
    ul{
      padding: 0 10px;
      li{
        list-style: none;
        display: inline-block;
        margin-right: 10px;
        width: 29%;
        margin-bottom: 8px;
        line-height: 35px;
        text-align: center;
        color: #333;
        border-radius: 3px;
        background: #fff;
        font-size: 14px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding: 0 2px;
      }
    }
  }
  .filter{
    position: fixed;
    right: 3px;
    top: 60px;
    font-size: 15px;
    div{
      margin-top: 2px;
      text-align: center;
    }
  }
  .active-key{
    position: fixed;
    width: 100px;
    height: 100px;
    line-height: 100px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 100;
    background: #dedede;
    color: #fff;
    border-radius: 100%;
    text-align: center;
    font-size: 40px;
  }
}

这里的.block-60主要是用来占位,不然城市会被上方的搜索框盖住。

事件

代码语言:javascript
复制
switchKey(key){
  // 当前选中的字母
  this.activeKey = key;
  // 1秒后清空,让`active-key`隐藏
  setTimeout(()=>{
    this.activeKey = '';
  }, 1000)
  // 获取当前字母来cityList中距离顶部的位置
  let targetTop = document.querySelector('#'+key+'').offsetTop;
  window.scrollTo({ 
      top: targetTop - 60, // 60是search的高度
      behavior: "smooth" 
  });
}

这样就完工,是不是很简单了?

源码地址:vue-c-city

如果要PC<=>H5互换,需要修改main.js里面的代码。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 数据部分
  • 页面部分
  • 事件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档