VUE3组件开发——下拉菜单(DropDown)

VUE3组件开发——下拉菜单(DropDown)

这个组件主要是实现下拉多级列表的形式

1. 环境

主要用到的环境是VScode+TS+VUE3+Bootstrap

2. 代码

这里主要就是判断是不是点击打开了下拉菜单的部分,让它显示多级列表

2.1 组件

DropDown.vue

<template>
<!-- 写了样式也不骑作用 -->
<div class="dropdown" style="{display: inline-table}">
    <a href="#" class="btn btn-outline-light my-2 dropdown-toggle"  :style="{color :'black', fontSize: '18px'}">{
  {title}}</a>
    <!-- <ul class="dropdown_menu" :style="{display: 'block'}" v-if="isOpen"> -->
    <ul class="dropdown_menu" :style="{display: 'block'}">
    <li class="dropdown-item">
        <a href="#"  :style="{color :'bla
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Vue下拉刷新组件的封装示例: ```vue <template> <div class="pull-to-refresh" ref="wrapper"> <div class="pull-to-refresh__icon"> <i :class="iconClass"></i> </div> <div class="pull-to-refresh__text">{{text}}</div> <div class="pull-to-refresh__content"> <slot></slot> </div> </div> </template> <script> export default { name: 'PullToRefresh', props: { iconClass: { type: String, default: 'iconfont icon-refresh' }, text: { type: String, default: '下拉刷新' } }, data() { return { startY: 0, moveY: 0, distance: 0, isDragging: false } }, mounted() { this.$refs.wrapper.addEventListener('touchstart', this.handleTouchStart) this.$refs.wrapper.addEventListener('touchmove', this.handleTouchMove) this.$refs.wrapper.addEventListener('touchend', this.handleTouchEnd) }, beforeDestroy() { this.$refs.wrapper.removeEventListener('touchstart', this.handleTouchStart) this.$refs.wrapper.removeEventListener('touchmove', this.handleTouchMove) this.$refs.wrapper.removeEventListener('touchend', this.handleTouchEnd) }, methods: { handleTouchStart(e) { if (this.$refs.wrapper.scrollTop === 0) { this.startY = e.touches[0].clientY this.isDragging = true } }, handleTouchMove(e) { if (!this.isDragging) return this.moveY = e.touches[0].clientY this.distance = this.moveY - this.startY if (this.distance > 0) { e.preventDefault() this.$refs.wrapper.style.transform = `translate3d(0, ${this.distance}px, 0)` } }, handleTouchEnd() { if (!this.isDragging) return this.isDragging = false if (this.distance >= 80) { this.$emit('refresh') } else { this.$refs.wrapper.style.transform = `translate3d(0, 0, 0)` } this.distance = 0 } } } </script> <style scoped> .pull-to-refresh { position: relative; overflow: auto; -webkit-overflow-scrolling: touch; } .pull-to-refresh__icon { font-size: 20px; color: #999; text-align: center; margin-bottom: 10px; } .pull-to-refresh__text { font-size: 14px; color: #999; text-align: center; margin-bottom: 10px; } .pull-to-refresh__content { position: relative; z-index: 1; transform: translate3d(0, 0, 0); transition: all 0.3s ease-in-out; } </style> ``` 这个组件会在用户下拉到一定距离后触发 `refresh` 事件,你可以在使用组件的地方监听该事件并执行对应的操作,例如重新加载数据。 ```vue <template> <pull-to-refresh @refresh="loadData"> <ul> <li v-for="item in items" :key="item.id">{{item.title}}</li> </ul> </pull-to-refresh> </template> <script> import PullToRefresh from './PullToRefresh.vue' export default { components: { PullToRefresh }, data() { return { items: [] } }, methods: { loadData() { // 加载数据的逻辑 this.items = [...] } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值