vue3自定义顶部弹窗

1.组件的封装

javascript 复制代码
<template>
    <view v-if="show" class="bidding-type-container">
        <!-- 遮罩层 -->
        <view class="bidding-type-mask" @click="handleClose"></view>
        <!-- 弹窗内容 -->
        <view class="bidding-type-popup">
            <view class="popup-content">
                <text>人生若只如初见,何事秋风悲画扇</text>
            </view>
        </view>
    </view>
</template>

<script setup>
defineOptions({
    name: 'BiddingType'
})

defineProps({
    show: {
        type: Boolean,
        default: false
    },
    headerHeight: {
        type: Number,
        default: 0
    }
})

const emit = defineEmits(['close'])

const handleClose = () => {
    emit('close')
}
</script>

<style scoped lang="scss">
.bidding-type-container {
    position: fixed;
    top: v-bind('headerHeight + "px"');
    left: 0;
    right: 0;
    bottom: 0;
}

.bidding-type-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
}

.bidding-type-popup {
    position: relative;
    background-color: #ffffff;
    border-top: 2rpx solid #e9ebf1;
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.popup-content {
    padding: 40rpx;
}
</style>

2.父组件使用

  • 引入
javascript 复制代码
import BiddingType from './components/biddingType.vue'

//变量定义
const showBiddingType = ref(false) 

onLoad((options) => {
  // 获取头部高度,使组件更贴合
  const query = uni.createSelectorQuery()
  console.log('query:', query)
  query.select('.rank-header').boundingClientRect(data => {
    if (data) {
      headerHeight.value = data.height
    }
  }).exec()
})
  • 使用
javascript 复制代码
<bidding-type :show="showBiddingType" @close="showBiddingType = false"></bidding-type>
相关推荐
To_OC5 小时前
面试被问了三回三栏布局,这次我终于把 BFC 那层窗户纸捅破了
前端·css·面试
To_OC5 小时前
啃完 TS 工具类型我发现:Pick 和 Omit 原来就是一层窗户纸
前端·面试·typescript
风月说与山鬼7 小时前
三、大括号语法
前端·react.js
kyriewen7 小时前
我把最常踩的8个CORS跨域报错整理了一遍——第8个去年还不存在
前端·javascript
Csvn8 小时前
🕰️ 闭包 + setTimeout 的 5 个经典陷阱:为什么定时器看到的永远不是最新的值?
前端
lilian2338 小时前
Harmony os 技术实战|拼豆制图27:用单字符编码承载 50 张 70×70 图纸
前端·数据库·华为·harmonyos
CarIise8 小时前
CSS选择器与样式关联
前端·css·tensorflow
里欧跑得慢10 小时前
CSS 模块化架构的演进:BEM、CSS Modules 到 CSS-in-JS 的反思
前端·css·flutter·web·css-in-js
IT_陈寒10 小时前
Vue的computed属性把我坑惨了,原来我一直用错姿势
前端·人工智能·后端