欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  互联网

微信小程序滑块验证实现方法

程序员文章站 2022-04-04 18:20:20
下面给大家介绍下微信小程序滑块验证的效果图及实例代码:如图:滑块验证组件puzzleverify目录index.wxml

下面给大家介绍下微信小程序滑块验证的效果图及实例代码:

如图:

微信小程序滑块验证实现方法

滑块验证组件

puzzleverify目录

微信小程序滑块验证实现方法

index.wxml

<!-- 滑动验证弹窗 -->
<view class="slide_model" wx:if="{{slidebel}}">
	<view class="slide_wrapper">
		<!-- 拼图图片部分 -->
		<view class="canvas_img" id="canvas_img">
			<!-- 背景图片 -->
			<canvas style="width: {{canvas_width}}px; height: {{canvas_width * 13 / 28}}px;" canvas-id="firstcanvas" id="firstcanvas"></canvas>
			<!-- 被抠方块 -->
			<cover-view class="canvas_view" style="left:{{canfile_x}}px;top:{{canfile_y}}px;"></cover-view>
			<!-- 可移动空格 -->
			<cover-image class="canfile_image"
				style="top:{{canfile_y}}px;left:{{slide_clientx > canvas_width-50 ? canvas_width-50 : slide_clientx}}px;"
				src="{{canfile_image}}"></cover-image>
		</view>
		<!-- 滑块 -->
		<view class="canvas_slide">
			<view class="canvas_width"
				style="width:{{slide_clientx > canvas_width-50?canvas_width-50:slide_clientx}}px;{{slide_status == 2?'background:#52ccba;':''}}{{slide_status == 3?'background:#f57a7a;':''}}">
			</view>
			<view class="canvas_kus" bindtouchstart="slide_start" bindtouchmove="slide_hmove" bindtouchend="slide_chend"
				style="left:{{slide_clientx > canvas_width-50?canvas_width-50:slide_clientx}}px; {{slide_status == 0?'color: #333;':''}} {{slide_status == 1?'background:#1991fa;':''}} {{slide_status == 2?'background:#52ccba;':''}} {{slide_status == 3?'background:#f57a7a;':''}}">
				<view wx:if="{{slide_status < 2}}"
					style="background-image: {{slide_status==0?'url(/imgs/puzzle-uncheck.png)':'url(/imgs/puzzle-uncheck2.png)'}}">
				</view>
				<view wx:if="{{slide_status == 2}}">
					<image src="/imgs/puzzle-checked.png" mode="aspectfit"
						style="width:40rpx;height:40rpx;margin-top:10px;">
					</image>
				</view>
				<view wx:if="{{slide_status == 3}}">
					<image src="/imgs/puzzle-fail.png" mode="aspectfit"
						style="width:40rpx;height:40rpx;margin-top:10px;">
					</image>
				</view>
			</view>
			<view wx:if="{{slide_status == 0 || slide_status == 1}}">拖动左边滑块完成上方拼图</view>
		</view>
		<!-- 底部按钮 -->
		<view class="canvas_guil">
			<image bindtap="visidlisd" class="footer-icon" src="/imgs/puzzle-close.png"
				style="width: 50rpx; height: 50rpx;"></image>
			<image bindtap="slide_tap" class="footer-icon" src="/imgs/puzzle-fresh.png"
				style="width: 48rpx; height: 48rpx;"></image>
		</view>
	</view>
</view>

index.wxss

/* 拼图滑动验证 */
.slide_model {
  width: 100%;
  height: 100vh;
  z-index: 999;
  position: fixed;
  left: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
}
.slide_wrapper {
  float: left;
  z-index: 1;
  position: relative;
  width: 90%;
  background-color: #fff;
}
.canvas_img {
  width: 90%;
  min-height: 150px;
  position: relative;
  margin: 25rpx auto 0;
}
#firstcanvas {
  z-index: 1 !important;
  width: 100%;
  height: 100%;
}
/* 被抠的空格 */
.canvas_view {
  width: 50px;
  height: 50px;
  position: absolute;
  background: rgba(0, 0, 0, 0.6);
  z-index: 2;
  box-shadow: 0 0 5px 2px rgba(255, 255, 255, 0.5);
}
/* 移动的空格 */
.canfile_image {
  width: 50px;
  height: 50px;
  position: absolute;
  left: 0;
  z-index: 3;
  box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.8);
  box-sizing: border-box;
}
.canfile_image::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  box-shadow: 0 0 8px 5px rgba(255, 255, 255, 0.8) inset;
}
.canvas_slide {
  width: 90%;
  height: 45px;
  background: #eee;
  text-align: center;
  line-height: 80rpx;
  margin: 0 auto;
  position: relative;
  font-size: 26rpx;
}
/* 滑条上滑块经过的部分 */
.canvas_width {
  position: absolute;
  left: 0;
  top: 0;
  height: 45px;
  background-color: #1991fa;
  width: 0;
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
}
/* 滑块 */
.canvas_kus {
  width: 48px;
  height: 45px;
  background-color: #fff;
  font-size: 36rpx;
  font-weight: 700;
  position: absolute;
  left: 0;
  top: 0;
  border: 1px solid #ddd;
  color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
.canvas_kus>view {
  width: 100%;
  height: 100%;
  background-position: center;
  background-size: 50% 40%;
  background-repeat: no-repeat;
}
/* 底部按钮位置 */
.canvas_guil {
  width: 100%;
  border-top: 1px solid #f4f4f4;
  height: 100rpx;
  display: flex;
  align-items: center;
  float: left;
  font-size: 30rpx;
  color: #666;
}
.canvas_guil>image {
  margin-left: 30rpx;
}

index.js

component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		sildeblockcont: {   //接受父组件值
			type: string
		}
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		canvas_width: 0,
		slidebel: false, //滑动弹窗
		canfile_image: '', //裁剪图片
		canfile_x: '', //被抠方块的水平位置
		canfile_y: '', //被抠方块的垂直位置
		slide_clientx: 0, //移动位置
		slide_status: 0, //0 停止操作   1 触发长按   2 正确   3 错误
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		// 弹窗
		visidlisd(e) {
			var that = this
			this.setdata({
				slidebel: !this.data.slidebel
			})
			if (this.data.slidebel) {
				if (this.data.canvas_width != 0) {
					this.slide_tap()
					return
				}
				wx.nexttick(() => {
					let query = this.createselectorquery()
					query.select('#canvas_img').boundingclientrect(function (rect) {
						that.setdata({
							canvas_width: rect.width
						})
						that.slide_tap()
					}).exec()
				})
			}
		},
		// 画布
		slide_tap(e) {
			var that = this
			var imgindex = math.round(math.random() * 13 + 1)
			that.setdata({
				canfile_x: math.round(math.random() * (this.data.canvas_width - 120) + 60),
				canfile_y: math.round(math.random() * (this.data.canvas_width * 13 / 28 - 60)),
				canfile_image: ''
			})
			settimeout(function () {
				var context = wx.createcanvascontext('firstcanvas', that)
				context.width = that.data.canvas_width
				context.height = that.data.canvas_width * 13 / 28
				
				//  /imgs/puzzle-bg-${imgindex}.jpg  为滑块背景图 从静态资源获取  如从接口获取可从that.properties.properties拿
				context.drawimage(`/imgs/puzzle-bg-${imgindex}.jpg`, 0, 0, context.width, context.height)
				context.draw(false, (() => {
					wx.canvastotempfilepath({
						x: that.data.canfile_x,
						y: that.data.canfile_y,
						width: 50,
						height: 50,
						canvasid: 'firstcanvas',
						success: function (res) {
							that.setdata({
								canfile_image: res.tempfilepath
							})
						}
					}, that);
				}))
			}, 50)
		},
		// 滑动开始
		slide_start(e) {
			this.setdata({
				slide_status: 1
			})
		},
		// 滑动中
		slide_hmove(e) {
			this.setdata({
				slide_clientx: (e.touches[0].clientx - 60) < 1 ? 0 : (e.touches[0].clientx - 60)
			})
		},
		//滑动结束
		slide_chend(e) {
			var that = this
			var cliextx;
			var maxx = this.data.canvas_width - 60
			if (that.data.slide_clientx < 1) {
				that.data.slide_status = 0
				return false
			}
			if (that.data.slide_clientx > maxx) {
				cliextx = maxx
			} else {
				cliextx = that.data.slide_clientx
			}
			if (((that.data.canfile_x + 5) > cliextx) && ((that.data.canfile_x - 5) < cliextx)) {
				that.setdata({
					slide_status: 2,
					slide_clientx: that.data.canfile_x,
				})
				settimeout(function () {
					that.setdata({
						slidebel: false,
					})
				}, 500)
				wx.showtoast({
					icon: 'success',
					title: '验证成功',
				})
				that.triggerevent('puzzleverify')
			} else {
				that.setdata({
					slide_status: 3,
				})
			}
			settimeout(function () {
				that.setdata({
					slide_status: 0,
					slide_clientx: 0,
				})
			}, 500)
		},
	},
	lifetimes: {
		created() {
			// 在组件实例刚刚被创建时执行
		},
		ready() {
			// 在组件在视图层布局完成后执行
			// console.log(this.properties.sildeblockcont);
		},
	}
})

index.json

{
  "component": true,
  "usingcomponents": {}
}

父组件调用滑块

微信小程序滑块验证实现方法

json

"usingcomponents": {
    "puzzleverify": "/components/puzzleverify/index"
  }

wxml

<button  bindtap="getcode"></button>
<puzzleverify id="puzzleverify" bind:puzzleverify="countdown" sildeblockcont="{{sildeblockcont}}"></puzzleverify>

js

	data: {
		sildeblockcont: '', //滑块背景图
	},
	onready: function () {
		this.puzzleverify = this.selectcomponent("#puzzleverify")
	},
	getcode() {
		this.puzzleverify.visidlisd()
	},
	// 滑块验证成功操作
	countdown() {}

滑块含有静态图片,完整代码和静态图片资源已上传链接: 微信小程序滑块验证源码.

到此这篇关于微信小程序滑块验证方法的文章就介绍到这了,更多相关微信小程序滑块验证内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!