前端:uniapp手搓浮动层,简单可复用型组件。

类似于dialog的方式,简单很好修改。这里以传图片为例

复用性组件

<template>

    <view v-if="showPopup" @click="hidePopup" class="popup-container">

        <view class="popup-content">

            <image :src="imageUrl" mode="aspectFill" class="popup-image"></image>

        </view>

    </view>

</template>

<script>

export default {

    props: {

        imageUrl: {

            type: String,

            required: true

        },

        showPopup: {

            type: Boolean,

            default: false

        }

    },

    methods: {

        hidePopup() {

            this.$emit('update:showPopup', false);

        }

    }

};

</script>

<style>

.popup-container {

    position: fixed;

    top: 0;

    left: 0;

    width: 100vw;

    height: 100vh;

    background-color: rgba(0, 0, 0, 0.5);

    display: flex;

    justify-content: center;

    align-items: center;

    z-index: 1001;

}

.popup-content {

    background-color: #fff;

    padding: 20rpx;

    border-radius: 10rpx;

}

.popup-image {

    width: 500rpx;

    height: 500rpx;

}

</style>

父组件内引入

先import,比如 import PopupComponent from '@xxx/PopupComponent';

并在 components中引入该组件 PopupComponent

父组件中使用:

<PopupComponent :imageUrl="imagePath" :showPopup="popupVisible" @update:showPopup="popupVisible = $event" />

data() { return { imagePath: xxxx.jpg', popupVisible: false }; },

在实战中,通常是点击按钮后,弹出框,所以可以在对应的按钮事件上增加逻辑。如:

showPopup() { this.popupVisible = true; },或者可以在方法中赋值图片地址,如:

showPopup() {

this.imagePath="xxxx";

this.popupVisible = true; }

效果如下,当再次点击的时候则会关闭浮层

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值