移动端、uniapp自定义可拖拽大小弹窗 可拖拽弹窗高度可变化弹窗

//弹窗样式

<view class="addpop"  @tap.stop="" :class="show==false?'closepop':show==true?'showpop':''" id="addpop" :style="{height:popheight!=''?popheight  +'rpx':640+'rpx'}" >
			<!-- 拖拽 -->
			<view class="btnpop-box" @touchend='touchend'  @touchmove="touchmove($event)" @touchstart="touchstart($event)">
				<view class="drag">
				</view>
			</view>
			<view class="pop-tit">
				添加藏品材料
				<view class="pop-tit-line">
				</view>
			</view>
			<view class="pop-content">
				<template v-if="list.length>0">
					<view class="pop-list" @click.stop="addlist(index)" v-for="(item,index) in list" :key="index">
						<view class="poplist-left">
							<img :src="item.image" alt="">
						</view>
						<view class="poplist-right">
							<view class="pop-list-content">
								<view class="pop-list-tit">
									{{item.name}}
								</view>
								<view class="pop-list-num">
									X{{item.num}}
								</view>
							</view>
							<view class="pop-radio" v-if="item.checked==false">
								<img :src="Unchecked" alt="">
							</view>
							<view class="pop-radio" v-else>
								<img :src="checked" alt="">
							</view>
						</view>
					</view>
				</template>
				<view class="pop-list" v-else>
					<view class="poplist-left">
						<img :src="mage.image" alt="">
					</view>
					<view class="poplist-right">
						<view class="pop-list-content">
							<view class="pop-list-tit">
								藏品材料
							</view>
							<view class="pop-list-num">
								材料不足
							</view>
						</view>
						<view class="pop-radio">
							<!-- <img :src="checked" alt=""> -->
						</view>
					</view>
				</view>
			</view>
			<view class="pop-btn-box">
				<view class="pop-btn-left">
					已选择:<span>{{numL}}/{{numLs}}</span>
				</view>
				<!-- :class="numL>0?'pop-btn-submit':''" -->
				<view  :class="numL>0?'pop-btn-submit':'pop-btn-submit1'" @tap.stop="addmaterial()">
					{{numL>0?'添加材料':'请选择材料'}}
				</view>
			</view>
		</view>

js //这里默认弹窗高度为640 最高为1080

data(){
	startX: 0, //开始坐标
	startY: 0,
	popheight:'', //弹窗高度
	movebottom:null ,// 保存的向上拖动后的最大高度
	lastheight:null, //触摸结束后的弹窗高度
	show:null,
}
methods:{
	touchstart(e) {
			      //开始触摸时 
			      this.startY = e.changedTouches[0].clientY
			    },
			 touchmove:function(target) {
				let touchMoveY = target.changedTouches[0].clientY  //滑动变化坐标  滑动后的坐标
				 let movestar=Number(this.startY)- Number(touchMoveY)
				
				 if (movestar>0) {
					 if (this.lastheight!=null) {
					 	this.popheight= (this.lastheight+movestar)
					 } else{
					 	this.popheight= (640+movestar)
					 }
					if(this.popheight>1080){
						this.popheight=1080
					}
					this.movebottom=this.popheight
					
				 } else if(movestar<0){
					if (this.lastheight!=null) {
						this.popheight= (this.movebottom+movestar)
					} else{
						this.popheight= (640+movestar)
					}
					if(this.popheight<640){
						this.popheight=640
					}
				 }
				 
			},
			touchend(){
				this.lastheight=this.popheight
			},
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue3是最新版本的Vue.js框架,它具有更强大的功能和性能优化,可以实现可拖动弹窗的封装。 首先,我们可以利用Vue3的自定义指令来实现拖动效果。首先在弹窗组件中添加一个容器元素,用来表示弹窗的位置和大小。然后,在该容器元素上添加一个自定义指令,比如"v-draggable"。在该自定义指令中,我们可以通过监听鼠标事件来实现拖动功能。当鼠标按下时,记录当前鼠标的位置坐标,并监听鼠标移动事件,不断地更新弹窗容器的位置坐标。最后,在鼠标松开的时候,停止拖动。 接下来,我们可以为弹窗组件添加一些控制按钮,比如关闭按钮和最小化按钮。这些按钮可以通过Vue3的事件绑定来实现对应的功能。例如,点击关闭按钮后,可以通过触发一个"close"事件来关闭弹窗。 此外,我们还可以利用Vue3的响应式数据和计算属性来实现弹窗的位置和大小自动调整。通过监听窗口大小变化的事件,并实时更新弹窗容器的位置和大小,使之适应不同的显示设备和分辨率。 最后,为了防止拖动时超出边界的情况,我们可以在自定义指令中添加一些边界限制的判断逻辑,以确保弹窗不会超出容器范围。 综上所述,通过Vue3的自定义指令、事件绑定、响应式数据和计算属性,我们可以很方便地封装一个可拖动弹窗组件,并且可以根据需要添加其他功能和样式,以满足不同的需求。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值