uni-app做小程序元素的拖拽效果(vue适用)

直接上代码 ,直接复制到编译器可看效果

<template>
	<view>
		<view class="area">
			<view v-for="(item,index) in list" :key="index" class="movable">
				<view v-show="!item.select" :data-item="item" :data-index="index" class="movable-view"
					:style="{backgroundColor:item.color}" @touchstart="test.touchstart" @touchmove="test.touchmove"
					@touchend="test.touchend" @touchcancel="test.touchend">
					{{item.name}}
				</view>
				<view v-if="item.select" :style="{backgroundColor: '#3B0000'}">空空如也</view>
			</view>
		</view>
		<view class="box">
			<view class="box-view">添加购物车</view>
		</view>
		<view class="area"> 
			<view v-for="(item,index) in selectList" :key="index" class="movable" @click="deleteItem(item.id)">
				<view class="movable-view" :style="{backgroundColor:item.color}">
					{{item.name}}
				</view>
			</view>
		</view>
	</view>
</template>
<script module="test" lang="wxs">
	var startX = 0
	var startY = 0

	function touchstart(event, ins) {
		console.log("touchstart")
		var touch = event.touches[0] || event.changedTouches[0]
		startX = touch.pageX
		startY = touch.pageY
		console.log(startX, startY);
		ins.selectAllComponents('.movable-view')[event.target.dataset.index].setStyle({
			position: 'fixed',
			left: startX + 'px',
			top: startY + 'px',
			transform: 'scale(1.2) translate(-50%,-50%)',
			opacity: '0.8',
		})
	}

	function touchmove(event, ins) {
		var touch = event.touches[0] || event.changedTouches[0]
		var pageX = touch.pageX
		var pageY = touch.pageY
		startX = pageX
		startY = pageY
		console.log(startX, startY);
		ins.selectAllComponents('.movable-view')[event.target.dataset.index].setStyle({
			position: 'fixed',
			left: pageX + 'px',
			top: pageY + 'px',
			transform: 'scale(1.2) translate(-50%,-50%)',
			opacity: startY > 400 ? '1' : '0.8'
		})
		if (startY > 400) {
			ins.selectComponent('.box-view').setStyle({
				'border-color': '#14279B',
				color: '#14279B',
			})
		} else {
			ins.selectComponent('.box-view').setStyle({
				'border-color': '#18B566',
				color: '#18B566',
			})
		}
		return false
	}

	function touchend(event, ins) {
		console.log(JSON.stringify(event));
		var index = event.target.dataset.index;
		var item = event.target.dataset.item;
		console.log(startX, startY);
		console.log(index);
		if (startY > 400) {
			ins.callMethod('selectItem', item.id);
		}
		ins.selectAllComponents('.movable-view')[index].setStyle({
			position: 'static',
			left: item.startX + 'px',
			top: item.startY + 'px',
			transform: 'scale(1)',
			opacity: '1',
		})
		ins.selectComponent('.box-view').setStyle({
			'border-color': '#18B566',
			color: '#18B566',
		})
	}

	module.exports = {
		msg: 'Hello',
		touchstart: touchstart,
		touchmove: touchmove,
		touchend: touchend,
	}
</script>

<script>
	export default {
		data() {
			return {
				list: [{
					id: 1,
					name: "香蕉",
					color: "#F0A500",
					select: false,
				}, {
					id: 2,
					name: "苹果",
					color: "#334756",
					select: false,
				}, {
					id: 3,
					name: "西红柿",
					color: "#082032",
					select: false,
				}, {
					id: 4,
					name: "草莓",
					color: "#90AACB",
					select: false,
				}, {
					id: 5,
					name: "橙子",
					color: "#F9D5A7",
					select: false,
				}, ],
				selectList: [],
			}
		},
		methods: {
			selectItem(id) {
				const index = this.list.findIndex(item => item.id == id);
				this.$set(this.list[index], 'select', true);
				const item = this.list[index];
				this.selectList.push(item);
			},
			deleteItem(id) {
				const i = this.list.findIndex(item => item.id == id);
				this.$set(this.list[i], 'select', false);
				const index = this.selectList.findIndex(item => item.id == id);
				this.selectList.splice(index, 1);
			},
		}
	}
</script>

<style>
	.area {
		padding: 16px;
		height: 400px;
		overflow: hidden;
	}

	.movable {
		float: left;
		width: 33.33%;
		height: 90px;
	}

	.movable view {
		width: 50px;
		height: 50px;
		margin: 20px auto;
		color: white;
		text-align: center;
		line-height: 50px;
		background-color: red;
	}

	.box view {
		border: 1px solid #18B566;
		color: #18B566;
		text-align: center;
		line-height: 40px;
		margin: 20px;
	}
</style>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值