Vue实现简单的移动端城市选择和搜索页面

页面展示

可实现模糊搜索,点击右侧导航栏跳到对应字母的位置等

1.基本结构搭建及样式

 比较简单,可以自行修改

<template>
  <div class="home">
    <!-- 头部 -->
    <header class="header"></header>
    <!-- 搜索栏 -->
    <div class="search">
      <input type="text" placeholder="输入关键字搜索" v-model="keyWord">
    </div>
    <div class="container">
      <div class="container-left" id="box">
        <!-- 热门城市 -->
        <div class="hot-box">
          <div v-for="city in hotCitys">{{ city.name }}</div>
        </div>
        <!-- 城市列表 -->
        <section class="section" v-for="item in group" :id="item.groupName">
          <div class="section-title">{{ item.groupName }}</div>
          <div class="section-list">
            <div class="item" v-for="city in item.groupList">{{ city.name }}</div>
          </div>
        </section>
      </div>
      <!-- 右侧导航栏 -->
      <div class="container-right">
        <div class="index">
          <div class="index-item" v-for="item in indexs" @click="jump(item)">{{ item }}</div>
        </div>
      </div>
    </div>

    <!-- 展示搜索结果 -->
    <div class="container" v-if="searchList.length" style="position: fixed;top: 88px;width: 100%;background-color: #ccc;">
      <div class="container-left">
        <section class="section">
          <div class="section-list" v-for="city in searchList">
            <div class="item">{{ city.name }}</div>
          </div>
        </section>
      </div>
    </div>
  </div>
</template>
<style>
* {
  margin: 0;
  padding: 0;
}

body,
html,
.home,
#app {
  height: 100%;
}

.home {
  display: flex;
  flex-direction: column;
}

.header {
  height: 44px;
  background-color: rgb(221, 221, 221);
}

.search {
  height: 44px;
  background-color: rgb(133, 105, 255);
}

.container {
  display: flex;
  flex: 1;
  background-color: rgb(255, 255, 255);
  overflow: hidden;
}

.container-left {
  flex: 1;
  overflow-y: auto;
  position: relative;
}

.container-right {
  width: 20px;
  display: flex;
  align-items: center;
}

.section-title {
  padding-left: 20px;
  height: 36px;
  line-height: 36px;
  background-color: #ccc;
}

.item {
  padding-left: 20px;
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #ccc;
}

.index-item {
  width: 20px;
  height: 20px;
  text-align: center;
  cursor: pointer;
  background-color: rgb(214, 189, 4);
}
</style>
2.js部分
<script>
export default {
  name: 'HomeView',
  data() {
    return {
      keyWord: "",//搜索关键字
      //raw为城市数据内容的数组,可以使用自己的接口或者自己填写
      raw: [
        {
          cityList: 110100,
          name: '北京',
          pinyin: 'beijing',
          isHot: 1,
        },
        {
          cityList: 110100,
          name: '北海',
          pinyin: 'beihai',
        },
        {
          cityList: 120100,
          name: '天津',
          pinyin: 'tianjin',
        },
        {
          cityList: 130100,
          name: '石家庄',
          pinyin: 'shijiazhang',
        },
        {
          cityList: 110100,
          name: '大理',
          pinyin: 'dali',
        },
        {
          cityList: 110100,
          name: '拉萨',
          pinyin: 'lasa',
        },
        {
          cityList: 120100,
          name: '曼岛',
          pinyin: 'mandao',
        },
        {
          cityList: 130100,
          name: '百慕大',
          pinyin: 'baimuda',
        },
        {
          cityList: 110100,
          name: '驻马店',
          pinyin: 'zhumadian',
        },
        {
          cityList: 110100,
          name: '南京',
          pinyin: 'nanjing',
        },
        {
          cityList: 120100,
          name: '重庆',
          pinyin: 'chongqing',
        },
        {
          cityList: 130100,
          name: '厦门',
          pinyin: 'xiamen',
        },
        {
          cityList: 130100,
          name: '深圳',
          pinyin: 'shenzhen',
          isHot: 1,
        },
        {
          cityList: 130100,
          name: '泉州',
          pinyin: 'quanzhou',
          isHot: 1,
        },
        {
          cityList: 130100,
          name: '杭州',
          pinyin: 'hangzhou',
          isHot: 1,
        },
        {
          cityList: 130100,
          name: '广州',
          pinyin: 'guangzhou',
          isHot: 1,
        },
        {
          cityList: 130100,
          name: '香洲',
          pinyin: 'xiangzhou',
          isHot: 1,
        },
        {
          cityList: 130100,
          name: '雷州',
          pinyin: 'leizhou',
          isHot: 1,
        },
        {
          cityList: 130100,
          name: '赣州',
          pinyin: 'ganzhou',
          isHot: 1,
        },
        {
          cityList: 130100,
          name: '明日方州',
          pinyin: 'mingrifangzhou',
          isHot: 1,
        },
      ]
    }
  },
  computed: {
    group() {
      let result = []
      // 遍历raw
      this.raw.forEach(city => {
        // 获取pinyin首字母
        const groupName = city.pinyin[0].toUpperCase()
        // 获取raw数组内所有pinyin的首字母
        const index = result.findIndex(item => item.groupName === groupName)
        // 判断index是否为空
        if (index > -1) {
          result[index].groupList.push(city)
        } else {
          // 把首字母放到数组里面
          result.push({
            groupName: groupName,
            groupList: [city]
          })
        }
      });
      return result.sort((a, b) => {
        // return a.groupName - b.groupName ? 1 : -1;
        return a.groupName.charCodeAt(0) - b.groupName.charCodeAt(0);
      });
    },

    indexs() {
      return this.group.map((item) => item.groupName)
    },

    hotCitys() {
      return this.raw.filter((item) => item.isHot === 1)
    },

    // 搜索结果,依赖项:keyWord,raw
    searchList() {
      // 精准搜索
      // return this.raw.filter((item) => item.name === this.keyWord)

      // 模糊搜索
      if (!this.keyWord) return []
      return this.raw.filter((item) => item.name.includes(this.keyWord))

      // 等价于
      // if (!this.keyWord) {
      //   return [];
      // } else {
      //   return this.raw.filter((item) => item.name.includes(this.keyWord))
      // }
    },

  },
  methods: {
    //点击右侧导航栏,跳到对应字母的位置
    jump(data) {
      // 根据data找到对应id
      const dom = document.querySelector(`#${data}`)
      // 计算该dom距离顶部的距离
      const offsetTop = dom.offsetTop;
      // 修改滚动元素的scrollTop值
      document.querySelector('#box').scrollTop = offsetTop;
    }
  }
}
</script>

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
基于Vue实现移动端小米应用商城页面需要进行以下步骤: 1. 界面设计:根据小米应用商城的界面设计需求,使用Vue框架中的组件库和样式预处理器进行页面的布局和设计。可以使用小米官方提供的UI组件库或者其他第三方组件库来提高开发效率和界面质量。 2. 路由设置:使用Vue Router来设置页面的路由。根据小米应用商城的功能需求,在路由配置中设置每一个页面对应的路由路径和组件,实现页面之间的切换和导航。 3. 数据获取和展示:使用Vue的响应式数据绑定功能,结合小米应用商城的数据接口,通过网络请求获取数据,并将数据进行展示。可以使用Vue的生命周期钩子函数来进行数据的初始化和更新。 4. 搜索和筛选:实现小米应用商城中的搜索和筛选功能。使用Vue实现搜索框的输入和结果展示的实时更新,并根据用户的筛选条件实时显示相应的应用列表。 5. 购物车功能:实现小米应用商城中的购物车功能。使用Vue的状态管理库(如Vuex)来管理购物车中的商品信息,实现商品的添加、删除、数量修改等操作,并能够计算出购物车中商品的总价。 6. 移动适配:使用Vue的移动适配方案(如vw/vh、rem)来实现页面的适配和响应式布局。根据不同的屏幕尺寸和设备类型,设置相应的样式规则,以确保页面在不同设备上能够良好展示。 7. 动画和交互:使用Vue的过渡效果和动画库来实现页面切换、菜单展开等动画效果,提升用户体验。同时,根据小米应用商城的交互功能需求,合理利用Vue的指令、事件绑定等特性来实现用户的点击、滑动、拖拽等交互操作。 通过以上步骤,可以基于Vue框架实现移动端小米应用商城页面,并达到用户友好、界面美观和功能完备的目标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值