search
HomeWeChat AppletMini Program DevelopmentImplementation of image drag and drop function in WeChat applet development

This article mainly shares with you the implementation of the image drag and drop function in WeChat applet development. We often encounter the drag and drop function, and hope it can help everyone.

1. Write the page structure: moveimg.wxml

##

<view class="container" style="height:{{screenHeight}}px;" >
	<view class="cnt">
		<image class="image-style" src="../uploads/2.jpg" style="width:
		{{imgwidth}}px;height:{{imgheight}}px;"bindtouchstart="startEvent"
		 bindtouchmove="moveEvent"bindload="imageLoad"animation="
		 {{animationData}}"bindtouchend="endEvent">
		</image>
	</view>
</view>

2. Write page style: moveimg.wxss

.container {
    box-sizing:border-box;
    padding:1rem;
} 
.cnt{
    width:100%;
    height:70%;
    border: 1px solid #ccc;
    position:relative;
    overflow: hidden;
    z-index:9999;
}
.image-style{
	-webkit-transform:translate(-38px,0px);  
}

3.Set data: moveimg.js

var app = getApp()
Page({
	data: {
    	imgwidth:0,
    	imgheight:0,
    	animationData: {},
    	movex:0,
    	movey:0,
    	x:0,
    	y:0,
	},
	onLoad: function() {
		 // 页面初始化 options为页面跳转所带来的参数
		var _this = this;
		wx.getSystemInfo({
			success: function(res) {
				_this.setData({
					screenHeight: res.windowHeight,
					screenWidth: res.windowWidth,
				});
			}
		});
	},
	onReady:function(){
	    // 页面渲染完成
	    //实例化一个动画
	    this.animation = wx.createAnimation({
	    	duration: 1000, 
	    	timingFunction: &#39;linear&#39;,//均匀
	    	transformOrigin: "left top 0px",
	    })
	},
	imageLoad: function(e) {  
        var _this=this; 
        //1.框的宽高
        var cnt_offetw=_this.data.screenWidth-38,
        	cnt_offeth=(_this.data.screenHeight-38)*0.7;
        //2.获取图片真实宽度
        var $width=e.detail.width,    //获取图片真实宽度  
            $height=e.detail.height,  
            ratio=$width/$height;   //图片的真实宽高比例
       	//3.进行判断:当图片小于框时候,图片大小等于框大小,当大于框的时候,则成比例呈现
       	var viewWidth=_this.data.screenWidth;           //设置图片固定宽度值,  
        var	viewHeight=parseInt(viewWidth/ratio);    //计算的高度值
     	if (viewHeight< cnt_offeth) {
            viewHeight=cnt_offeth;
        }
        _this.setData({ 
            imgwidth:viewWidth,  
            imgheight:viewHeight,
            cnt_boxw:cnt_offetw,
            cnt_boxh:cnt_offeth,
        })
    },
    startEvent:function(e){
    	//1.获取鼠标点击下去的
    	this.setData({
	     	startx: e.touches[0].pageX,
	      	starty: e.touches[0].pageY
	    })
    },
    moveEvent: function(e) { 
		var _this=this;
		//2.鼠标移动的位置
		var pageX = e.touches[0].pageX;
		var pageY = e.touches[0].pageY;	
		var x,y;
		//3.记住初始化图片x,y
		var endx=_this.data.x,endy=_this.data.y;
		//4.判断
		var w_x=_this.data.imgwidth-_this.data.cnt_boxw;//x拖拽值:图片宽-框的宽
		var h_y=parseInt(_this.data.imgheight-_this.data.cnt_boxh);//y拖拽值:图片高-框高
		var DistanceX = pageX - _this.data.startx;//x:当鼠标点击到移动的点之间的距离
		var DistanceY =pageY - _this.data.starty;//y:当鼠标点击到移动的点之间的距离
		if (DistanceX>0){
			//往右移动 如果当前的值大于等于0时则不移动,否则当前值加上鼠标拖拽的距离
			if (endx >= 0) {
		        x = 0;
		    } else {
		        x = endx + DistanceX;
		    }
	    }else{
	    	//往左移动:x拖拽值大于等于当前的值,说明已经到边上了,就等于拖拽值,否则当前的值加上鼠标拖拽的距离
			if (w_x >= endx) {
		        x = -w_x;
		    } else {
		        x = endx + DistanceX;
		    }
	    }
	    if (DistanceY>0){
	    	//往下移动:如果当前的值大于等于0时则不移动,否则当前值加上鼠标拖拽的距离
			if(endy>=0){
				y =0;
		    }else{
		        y = endy + DistanceY;
		    }
	    }else{
	    	//往上移动:y拖拽值大于等于当前的值,说明已经到边上了,就等于拖拽值,否则当前的值加上鼠标拖拽的距离
	    	if(-endy==h_y||-endy>h_y){
	    		y=-h_y;
	    		console.log("da0")
	    	}else{
	    		y=endy+DistanceY;
	    		console.log("da 1")
	    	}
	 	}
   		setTimeout(function() {
	      	_this.animation.translate(x, y).step();
			_this.setData({
	       		animationData: this.animation.export()
	   		})
	    }.bind(this), 0)

	    _this.setData({
	    	x:x,
	    	y:y,
	    }) 
	    endx=x;endy=y;//记住这次的图片移动的范围
	    _this.data.startx=pageX;_this.data.starty=pageY;// 每移动一次把上一次的点作为原点
    },
    endEvent:function(){
 		clearTimeout(function(){
    		this.animation.translate(this.data.x,this.data.y).step();
			this.setData({
	       		animationData: this.animation.export()
	   		})
     	});
    }
})

Because there was a problem with this last time, the effect of the last move was not recorded. So this time I used the power of prehistoric times and modified the code.

Related recommendations:

How to make a drag-and-drop image upload preview component in H5

JavaScript method to implement image drag-and-drop upload

JavaScript method to achieve text and image dragging effect_javascript skills

The above is the detailed content of Implementation of image drag and drop function in WeChat applet development. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)