基于uniapp的弹出

本文转自https://ext.dcloud.net.cn/plugin?id=254#

仅用于个人学习

本人用于弹出评论

该插件源码popup-layer.vue

<template>
	<view>
		<view v-show="ifshow" @tap="ableClose" @touchmove.stop.prevent class="popup-layer" >
			
		</view>
		<view ref="popRef"  class="popup-content"   @tap.stop="stopEvent" :style="_location">
			<slot></slot>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'popup-layer',
		model: {
			prop: "showPop",
			event: "change"
		},
		props: {
			showPop:{
				type:Boolean,
				default:false,
			},
			direction: {
				type: String,
				default: 'top', // 方向  top,bottom,left,right 
			},
			autoClose: {
				type: Boolean,
				default: true,
			}
		},
		data() {
			return {
				ifshow: false, // 是否展示,
				translateValue: -100, // 位移距离
				site:-100,
				timer: null,
				iftoggle: false,

			};
		},
		computed: {
			_translate() {
				const transformObj = {
					'top': `transform:translateY(${-this.translateValue}%)`,
					'bottom': `transform:translateY(${this.translateValue}%)`,
					'left': `transform:translateX(${-this.translateValue}%)`,
					'right': `transform:translateX(${this.translateValue}%)`
				};
				return transformObj[this.direction]
			},
			_location() {
				const positionValue = {
					'top': `bottom:${this.site}%;width:100%;`,
					'bottom': `top:${this.site}%;width:100%;`,
					'left': `right:0px;top:0;height:100%;`,
					'right': `left:0px;top:0;height:100%;`,
				};
				return positionValue[this.direction]+ this._translate;
			}
		},
		mounted(){
			if(this.showPop){
				// console.log(222);
				this.show();
			}
		},
		watch:{
			showPop(value){
				console.log(value)
				if(value){
					this.show();
				}else{
					this.close();
				}
			}	
		},
		methods: {
			stopMove(event){
				return;
			},
			show(events) {
				this.ifshow = true;
				this.site=0;
				let _open = setTimeout(() => {
					this.translateValue = 0;
					_open = null;
				}, 100)
				let _toggle = setTimeout(() => {
					this.iftoggle = true;
					_toggle = null;
				}, 300);
			},
			close() {
				if (this.timer !== null || !this.iftoggle) {
					return;
				}
				this.translateValue = -100;
				this.timer = setTimeout(() => {
					this.ifshow = false;
					this.timer = null;
					this.iftoggle = false;
					this.$emit('closeCallBack', null);
					this.$emit('change',false)
				}, 300);
			},
			ableClose() {
				if (this.autoClose) {
					this.close();
				}
			},
			stopEvent(event) {},
			doSome(){
			}

		}
	}
</script>

<style lang="scss">
	.popup-layer {
		position: fixed;
		z-index: 999999;
		background: rgba(0, 0, 0, .3);
		height: 100%;
		width: 100%;
		top: 0px;
		left: 0px;
		overflow: hidden;
	}

	.popup-content {
		position: fixed;
		z-index: 1000000;
		background: #FFFFFF;
		transition: transform .3s ease;
		overflow: hidden;
		// border:1px solid red;
	}
</style>

属性

  • v-model:控制 弹窗的显示/隐藏(true/false)目前有两种方式,1.通过ref 2.通过v-model
  • direction: 组件弹出方向。 默认为 "top" // 方向 top,bottom,left,right
  • autoClose: 击弹出遮罩层是否关闭,默认为true, 可设置为false则不手动关闭

方法

  • show(): 弹出
  • close():关闭
  • @closeCallBack:弹窗关闭后的回调事件(响应客户需求,尽量满足)

使用方法

  1. 首先download包,将下载下来的popup-layer.vue文件拷贝到你自己的项目中。

  2. 在需要的地方引入组件并声明。

复制代码<script>
    import popupLayer from '../../components/popup-layer.vue';
    export default {
        components:{
            popupLayer
        }
    }
</script>
  1. 使用组件
复制代码<popup-layer ref="popupRef" :direction="'top'" v-model="boolShow">
  <view class="zidingyiBox">
  </view>
</popup-layer>
  1. 弹出与关闭
复制代码this.$refs.popupRef.show() // 弹出
this.$refs.popupRef.close() // 关闭
  1. Demo用例
复制代码<template>
  <view class="test">
    <button type="primary" @tap="show">弹出</button>
      <popup-layer ref="popupRef" :direction="'top'">
        <view class="box" >
          <button type="primary" @tap="close">关闭</button>
        </view>
      </popup-layer>
  </view>
</template>

<script>
    import popupLayer from '../../components/popup-layer.vue';
    export default {
        components:{
            popupLayer
        },
    data(){
      return {
         boolShow:false
      }
    },
        methods:{
            show(){
                this.$refs.popupRef.show(); // 或者 boolShow = rue
            },
            close(){
                this.$refs.popupRef.close();// 或者 boolShow = false
            }
        }
    }
</script>

个人案例弹出评论

 并且通过close和open方法完成显示与关闭

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
窗口有多种方法,以下是其中的一种: 1. 在需要弹出窗口的组件中,添加一个按钮,并添加点击事件: ``` <template> <div> <button @click="showPopup">弹出窗口</button> <popup v-if="popupVisible" @close="hidePopup"></popup> </div> </template> <script> import Popup from './Popup.vue'; export default { components: { Popup }, data() { return { popupVisible: false } }, methods: { showPopup() { this.popupVisible = true; }, hidePopup() { this.popupVisible = false; } } } </script> ``` 2. 创建一个弹出窗口组件 Popup.vue,里面包含一个关闭按钮和需要展示的内容: ``` <template> <div class="popup"> <div class="popup-content"> <button class="close-btn" @click="$emit('close')">关闭</button> <div class="popup-body"> <!-- 填写需要展示的内容 --> </div> </div> </div> </template> <script> export default { name: 'Popup' } </script> <style> .popup { position: fixed; top: 0; left: 0; z-index: 999; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .popup-content { background-color: #fff; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); max-width: 90%; max-height: 90%; overflow: auto; } .close-btn { position: absolute; top: 10px; right: 10px; } .popup-body { /* 填写需要展示的内容的样式 */ } </style> ``` 3. 根据需要,修改弹出窗口和内容的样式。 这种方法创建的弹出窗口是基于组件的,可以在多个地方使用,并且可以通过修改样式来满足不同的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值