uniapp 自定义全局弹窗

自定义全局弹窗可在js和.vue文件中调用,unipop样式不满足,需自定义样式。

效果图

目录结构

index.vue

javascript 复制代码
<template>
    <view class="uni-popup" v-if="isShow">
		<view class="uni-popup__mask uni-center ani uni-custom">
		</view>
       <view class="uni-popup__wrapper center uni-center ani uni-custom">
			<view class="uni-popup__wrapper-box">
				<view class="box">
					<div class="title">温馨提示</div>
					<div class="con">
						抱歉,由于当前用户访问量激增,部分功能加载较慢,请您耐心等待。</br></br>
						如有疑问,请致电咨询重庆市生态环境部门服务热线: 023-88888888。感谢您的理解与支持!
					</div>
					<div class="btn">
						<view @click="close">取消</view>
					</div>
				</view>
			</view>
       </view>
    </view>
</template>

<script>
    export default {
		data(){
			return {
				isShow:false
			}
		},
        props: {
            title: { //显示的文本
              type: String,
              default: '消息'
            },
            type: { 
            // 主题类型,不填默认为
            // default-灰黑色 ,error-红色 代表错误 ,primary-蓝色 uView的主色调
            // success-绿色 代表成功 ,
            // warning-黄色 代表警告 ,info-灰色 比default浅一点
              type: String,
              default: 'success'
            },
            duration:{ //toast的持续时间,单位ms
                type:Number,
                default: 2000
            },
            position:{ //toast出现的位置
                type: String,
                default:"center"
            },
            back:{ // toast结束后,是否返回上一页,优先级低于url参数
                type:Boolean,
                default:false
            },
            icon:{ // 是否显示显示type对应的图标,为false不显示图标
                type:Boolean,
                default:true
            },
            callback:Function ,//回调函数
            url:String// 弹窗时间结束后跳转到指定页面
        },
        created() {
            let that = this
            this.$nextTick(() => {
				  this.show()
               /* this.$refs.uToast.show({
                    title: that.title,
                    type: that.type,
                    duration: that.duration,
                    back:that.back,
                    position:that.position,
                    icon:that.icon,
                    url:that.url,
                    callback:that.callback
                }) */
                // x秒后删除dom节点
                /* setTimeout(() => {            if(document.body){
            that.$destroy();
                    document.body.removeChild(that.$el);
            }
                    
                }, that.duration); */
            })
        },
		methods:{
			show() {
				this.isShow = true
			},
			close() {
				this.isShow = false
				// window.close()
			}
		},
    }
</script>

<style lang="scss" scoped>
	.box {
		width: 500upx;
		height: 662upx;
		background-image: url('../../static/companyImg/popup.png');
		background-size: 100% 100%;
		background-position: center;
		.title {
			display: flex;
			justify-content: center;
			padding: 50upx 0 100upx 0;
			color: #fff;
			font-size: 36upx;
		}
		.con {
			height: 340upx;
			font-size: 28upx;
			padding: 0 40upx ;
			color: #686b73;
			border-bottom: 1px solid #e8e9ec;
		}
		.btn {
			position: absolute;
			bottom: 40upx;
			width: 100%;
			display: flex;
			justify-content: center;
			font-size: 36upx;
			color: #3ca1f2;
			cursor: pointer;
		
	
		}
		
	}
	.uni-popup {
		position: fixed;
		/*  #ifdef  H5  */
		top: 0px;
		// top: 50px;
		/*  #endif  */
		/*  #ifndef  H5  */
		top: 0px;
		/*  #endif  */
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 99999;
		overflow: hidden;

		&__mask {
			position: absolute;
			top: 0;
			bottom: 0;
			left: 0;
			right: 0;
			z-index: 998;
			background: rgba(0, 0, 0, 0.4);
			opacity: 0;

			&.ani {
				transition: all 0.3s;
			}

			&.uni-top,
			&.uni-bottom,
			&.uni-center {
				opacity: 1;
			}
		}

		&__wrapper {
			position: absolute;
			z-index: 999;
			box-sizing: border-box;

			&.ani {
				transition: all 0.3s;
			}

			&.top {
				top: 0;
				left: 0;
				width: 100%;
				transform: translateY(-100%);
			}

			&.bottom {
				bottom: 0;
				left: 0;
				width: 100%;
				transform: translateY(100%);
			}

			&.center {
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				transform: scale(1.2);
				opacity: 0;
			}

			&-box {
				position: relative;
				box-sizing: border-box;
			}

			&.uni-custom {
				& .uni-popup__wrapper-box {
					// padding: 30upx;
					background: #fff;
					border-radius: 16px;
				}

				&.center {
					& .uni-popup__wrapper-box {
						position: relative;
						// max-width: 80%;
						margin: 0upx 30upx;
						// width: 100%;
						box-sizing: border-box;
						max-height: 80%;
						overflow-y: visible;
					}
				}

				&.top,
				&.bottom {
					& .uni-popup__wrapper-box {
						width: 100%;
						max-height: 500px;
						overflow-y: scroll;
					}
				}
			}

			&.uni-top,
			&.uni-bottom {
				transform: translateY(0);
			}

			&.uni-center {
				transform: scale(1);
				opacity: 1;
			}
		}
	}
</style>

index.js

javascript 复制代码
import fullNameVue from './index.vue'

const FullToast = {};

FullToast.install = function (Vue, option) {
  const FullNameInstance = Vue.extend(fullNameVue);
  let name;
  const initInstance = () => {
    name = new FullNameInstance();
    let nameSpan = name.$mount().$el;
    document.body.appendChild(nameSpan);
  }
  Vue.prototype.$uToast = {
    showToast(option){
      initInstance();
      if(typeof option === 'string'){
        name.firstName = option;
      }else if(typeof option === 'object'){
        Object.assign(name, option);
      }
      return initInstance;
    }
  };
}

export default FullToast;

main.js中注册

import uToast from './components/uToast/index'

Vue.use(uToast);

在js和vue文件中调用自定义弹窗

javascript 复制代码
// js中调用
import Vue from 'vue'
const vm = new Vue()
vm.$uToast.showToast()

// vue文件中调用
this.$uToast.showToast()
相关推荐
鸭子嘎鹅子呱9 小时前
uniapp使用高德地图设置marker标记点,后续根据接口数据改变某个marker标记点,动态更新
uni-app·map·高德地图
计算机源码社14 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
Angus-zoe17 小时前
uniapp+vue+微信小程序实现侧边导航
vue.js·微信小程序·uni-app
Pluto & Ethereal17 小时前
uni-app尺寸单位、flex布局于背景图片
uni-app
天空下sky1 天前
uniapp+若依 开发租房小程序源码分享
uni-app
帅过二硕ฅ1 天前
uniapp点击跳转到对应位置
前端·javascript·uni-app
佩淇呢1 天前
uniapp vue3 梯形选项卡组件
前端·vue.js·uni-app
[廾匸]1 天前
uni-app获取设备唯一值、静态IP以及公网IP的方法
uni-app·ip·imei·唯一值
luckycoke2 天前
小程序的右侧抽屉开关动画手写效果
前端·javascript·微信小程序·uni-app
微刻时光2 天前
好课程:uni-app实战音频小说app小程序
小程序·uni-app