持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第18天,点击查看活动详情
为什么要加验证码?
验证码(CAPTCHA)是 Completely Automated Public Turing test to tell Computers and Humans Apart的缩写。是一种区分用户是计算机还是人的公共全自动程序。可以防止:恶意破解密码、刷票、论坛灌水,有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试,实际上用验证码是现在很多网站通行的方式,我们利用比较简易的方式实现了这个功能。 有效制止大多数的恶意请求,减轻服务端的压力,可以有效的区分有效请求和无效请求。
常见的验证码方式
- 点击按钮通过验证
- 滑动拼图验证
- 文字点选
- 图标点选
- 推理拼图
- 语序点选
- 语音验证 当然还有其他的方式这里就不一一列举了今天我们这里主要说一下滑块验证
准备项目所需的依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!-- 操作图片-->
<dependency>
<groupId>com.jhlabs</groupId>
<artifactId>filters</artifactId>
<version>2.0.235-1</version>
</dependency>
PuzzleCaptcha 工具类
@Data
public class PuzzleCaptcha {
/** 默认宽度,用来计算阴影基本长度 */
private static final int DEFAULT_WIDTH = 280;
/** 随机数 */
private static final Random RANDOM = new Random();
/** 蒙版 */
private static Color color = new Color(255, 255, 255, 204);
/** alpha通道过滤器 */
private static InvertAlphaFilter alphaFilter = new InvertAlphaFilter();
/** 边距 */
private static int margin = 10;
/** 生成图片的宽度 */
private int width = DEFAULT_WIDTH;
/** 生成图片高度 */
private int height = 150;
/** x轴的坐标,由算法决定 */
private int x;
/** y轴的坐标,由算法决定 */
private int y;
/** 拼图长宽 */
private int vwh = 10 * 4;
/** 原图 */
private Image image;
/** 大图 */
private Image artwork;
/** 小图 */
private Image vacancy;
/** 是否注重速度 */
private boolean isFast = false;
/** 小图描边颜色 */
private Color vacancyBorderColor;
/** 小图描边线条的宽度 */
private float vacancyBorderWidth = 2.5f;
/** 主图描边的颜色 */
private Color artworkBorderColor;
/** 主图描边线条的宽度 */
private float artworkBorderWidth = 5f;
/**
* 最高放大倍数,合理的放大倍数可以使图像平滑且提高渲染速度
* 当isFast为false时,此属性生效
* 放大倍数越高,生成的图像越平滑,受原始图片大小的影响。
*/
private double maxRatio = 2;
/**
* 画质
*
* @see Image#SCALE_DEFAULT
* @see Image#SCALE_FAST
* @see Image#SCALE_SMOOTH
* @see Image#SCALE_REPLICATE
* @see Image#SCALE_AREA_AVERAGING
*/
private int imageQuality = Image.SCALE_SMOOTH;
/**
* 从文件中读取图片
*
* @param file
*/
public PuzzleCaptcha(File file) {
image = ImgUtil.read(file);
}
/**
* 从文件中读取图片,请使用绝对路径,使用相对路径会相对于ClassPath
*
* @param imageFilePath
*/
public PuzzleCaptcha(String imageFilePath) {
image = ImgUtil.read(imageFilePath);
}
/**
* 从{@link Resource}中读取图片
*
* @param resource
*/
public PuzzleCaptcha(Resource resource) {
image = ImgUtil.read(resource);
}
/**
* 从流中读取图片
*
* @param imageStream
*/
public PuzzleCaptcha(InputStream imageStream) {
image = ImgUtil.read(imageStream);
}
/**
* 从图片流中读取图片
*
* @param imageStream
*/
public PuzzleCaptcha(ImageInputStream imageStream) {
image = ImgUtil.read(imageStream);
}
/**
* 加载图片
*
* @param image
*/
public PuzzleCaptcha(Image image) {
this.image = image;
}
/**
* 加载图片
*
* @param bytes
*/
public PuzzleCaptcha(byte[] bytes) {
this.image = ImgUtil.read(new ByteArrayInputStream(bytes));
}
/**
* 生成随机x、y坐标
*/
private void init() {
if (x == 0 || y == 0) {
this.x = random(vwh, this.width - vwh - margin);
this.y = random(margin, this.height - vwh - margin);
}
}
/**
* 执行
*/
public void run() throws IOException {
init();
// 缩略图
Image thumbnail;
GeneralPath path;
int realW = image.getWidth(null);
int realH = image.getHeight(null);
int w = realW, h = realH;
double wScale = 1, hScale = 1;
// 如果原始图片比执行的图片还小,则先拉伸再裁剪
boolean isFast = this.isFast || w < this.width || h < this.height;
if (isFast) {
// 缩放,使用平滑模式
thumbnail = image.getScaledInstance(width, height, imageQuality);
path = paintBrick(1, 1);
w = this.width;
h = this.height;
} else {
// 缩小到一定的宽高,保证裁剪的圆润
boolean flag = false;
if (realW > width * maxRatio) {
// 不超过最大倍数且不超过原始图片的宽
w = Math.min((int) (width * maxRatio), realW);
flag = true;
}
if (realH > height * maxRatio) {
h = Math.min((int) (height * maxRatio), realH);
flag = true;
}
if (flag) {
// 若放大倍数生效,则缩小图片至最高放大倍数,再进行裁剪
thumbnail = image.getScaledInstance(w, h, imageQuality);
} else {
thumbnail = image;
}
hScale = NumberUtil.div(h, height);
wScale = NumberUtil.div(w, width);
path = paintBrick(wScale, hScale);
}
// 创建阴影过滤器
float radius = 5 * ((float) w / DEFAULT_WIDTH) * (float) wScale;
int left = 1;
ShadowFilter shadowFilter = new ShadowFilter(radius, 2 * (float) wScale, -1 * (float) hScale, 0.8f);
// 创建空白的图片
BufferedImage artwork = translucent(new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB));
BufferedImage localVacancy = translucent(new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB));
// 画小图
Graphics2D vg = localVacancy.createGraphics();
// 抗锯齿
vg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置画图路径范围
vg.setClip(path);
// 将区域中的图像画到小图中
vg.drawImage(thumbnail, null, null);
//描边
if (vacancyBorderColor != null) {
vg.setColor(vacancyBorderColor);
vg.setStroke(new BasicStroke(vacancyBorderWidth));
vg.draw(path);
}
// 释放图像
vg.dispose();
// 画大图
// 创建画笔
Graphics2D g = artwork.createGraphics();
// 抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 画上图片
g.drawImage(thumbnail, null, null);
// 设置画图路径范围
g.setClip(path);
// 填充缺口透明度 颜色混合,不透明在上
g.setComposite(AlphaComposite.SrcAtop);
// 填充一层白色的透明蒙版,透明度越高,白色越深 alpha:0-255
g.setColor(color);
g.fill(path);
//描边
if (artworkBorderColor != null) {
g.setColor(artworkBorderColor);
g.setStroke(new BasicStroke(artworkBorderWidth));
g.draw(path);
}
// 画上基于小图的内阴影,先反转alpha通道,然后创建阴影
g.drawImage(shadowFilter.filter(alphaFilter.filter(localVacancy, null), null), null, null);
// 释放图像
g.dispose();
// 裁剪掉多余的透明背景
localVacancy = ImageUtils.getSubimage(localVacancy, (int) (x * wScale - left), 0, (int) Math.ceil(path.getBounds().getWidth() + radius) + left, h);
if (isFast) {
// 添加阴影
this.vacancy = shadowFilter.filter(localVacancy, null);
this.artwork = artwork;
} else {
// 小图添加阴影
localVacancy = shadowFilter.filter(localVacancy, null);
// 大图缩放
this.artwork = artwork.getScaledInstance(width, height, imageQuality);
// 缩放时,需要加上阴影的宽度,再除以放大比例
this.vacancy = localVacancy.getScaledInstance((int) ((path.getBounds().getWidth() + radius) / wScale), height, imageQuality);
}
addWatermark(artwork);
}
/**
* 添加水印
* @param oriImage
* @return
* @throws IOException
*/
private static BufferedImage addWatermark(BufferedImage oriImage) throws IOException {
Graphics2D graphics2D = oriImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// 设置水印文字颜色
graphics2D.setColor(Color.white);
// 设置水印文字Font
Font font = new Font("隶书", Font.BOLD, 15);
graphics2D.setFont(font);
// 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
graphics2D.drawString("bug.vip", 230,190);
graphics2D.drawImage(oriImage, 0, 0, oriImage.getWidth(null), oriImage.getHeight(null), null);
// 设置水印文字透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
graphics2D.dispose(); //释放
return oriImage;
}
/**
*
* @param redisMovexp 缓存中的值
* @param sendMovepx 用户移动的值
* @return
*/
public static boolean checkVerifyImageStatus(Integer redisMovexp,Integer sendMovepx){
if(ObjectUtils.isEmpty(redisMovexp) || ObjectUtils.isEmpty(sendMovepx)){
return false;
}else{
if(redisMovexp-sendMovepx>5 || redisMovexp-sendMovepx<-5){
return false;
}else{
return true;
}
}
}
/**
* 绘制拼图块的路径
*
* @param xScale x轴放大比例
* @param yScale y轴放大比例
* @return
*/
private GeneralPath paintBrick(double xScale, double yScale) {
double x = this.x * xScale;
double y = this.y * yScale;
// 直线移动的基础距离
double hMoveL = vwh / 3f * yScale;
double wMoveL = vwh / 3f * xScale;
GeneralPath path = new GeneralPath();
path.moveTo(x, y);
path.lineTo(x + wMoveL, y);
// 上面的圆弧正东方向0°,顺时针负数,逆时针正数
path.append(arc(x + wMoveL, y - hMoveL / 2, wMoveL, hMoveL, 180, -180), true);
path.lineTo(x + wMoveL * 3, y);
path.lineTo(x + wMoveL * 3, y + hMoveL);
// 右边的圆弧
path.append(arc(x + wMoveL * 2 + wMoveL / 2, y + hMoveL, wMoveL, hMoveL, 90, -180), true);
path.lineTo(x + wMoveL * 3, y + hMoveL * 3);
path.lineTo(x, y + hMoveL * 3);
path.lineTo(x, y + hMoveL * 2);
// 左边的内圆弧
path.append(arc(x - wMoveL / 2, y + hMoveL, wMoveL, hMoveL, -90, 180), true);
path.lineTo(x, y);
path.closePath();
return path;
}
/**
* 绘制圆形、圆弧或者是椭圆形
* 正东方向0°,顺时针负数,逆时针正数
*
* @param x 左上角的x坐标
* @param y 左上角的y坐标
* @param w 宽
* @param h 高
* @param start 开始的角度
* @param extent 结束的角度
* @return
*/
private Arc2D arc(double x, double y, double w, double h, double start, double extent) {
return new Arc2D.Double(x, y, w, h, start, extent, Arc2D.OPEN);
}
/**
* 透明背景
*
* @param bufferedImage
* @return
*/
private BufferedImage translucent(BufferedImage bufferedImage) {
Graphics2D g = bufferedImage.createGraphics();
bufferedImage = g.getDeviceConfiguration().createCompatibleImage(bufferedImage.getWidth(), bufferedImage.getHeight(), Transparency.TRANSLUCENT);
g.dispose();
return bufferedImage;
}
/**
* 随机数
*
* @param min
* @param max
* @return
*/
private static int random(int min, int max) {
return RANDOM.ints(min, max + 1).findFirst().getAsInt();
}
}
服务端接口
@RequestMapping("/getCode")
public R getVrrifyCode() throws Exception {
log.info("获取生成验证码=================================》》》》》");
//这里用的是 阿里的OSS对象存储的图片
OSSObject object = ossClient.getObject("gulimall-oss-image", "10.jpg");
InputStream objectContent = object.getObjectContent();
//调用PuzzleCaptcha工具类生成
PuzzleCaptcha puzzleCaptcha = new PuzzleCaptcha(objectContent);
puzzleCaptcha.setImageQuality(Image.SCALE_SMOOTH);
puzzleCaptcha.run();
UUID uuid = UUID.randomUUID();
log.info("获取生成验证码设置缓存=================================》》》》》");
redisUtils.setCacheObject(RedisConstant.LOGIN_VERIFI_CODE+uuid.toString(),puzzleCaptcha.getX(),60,TimeUnit.SECONDS);
return R.ok().data("Image1",ImageConvertUtil.toDataUri(puzzleCaptcha.getArtwork(), "png"))
.data("Image2",ImageConvertUtil.toDataUri(puzzleCaptcha.getVacancy(), "png"))
.data("codekey",uuid.toString()).data("yHeight",puzzleCaptcha.getX());
}
Vue前端代码
<template>
<div id="puzzle" ref="puzzle" style="display:inline-block;">
<!-- :style="'padding:' + 16*scale + 'px ' + 16*scale + 'px ' + 28*scale + 'px;border-radius:'+16*scale+'px;'" -->
<div class="puzzle-container"
>
<div :style="'position:relative;overflow:hidden;width:'+ dataWidth +'px;'">
<div :style="'position:relative;width:' + dataWidth + 'px;height:' + dataHeight + 'px;'">
<img
id="scream"
ref="scream"
:src="imgRandom"
:style="'width:' + dataWidth + 'px;height:' + dataHeight + 'px;'"
/>
<canvas id="puzzle-box" ref="puzzleBox" :width="dataWidth" :height="dataHeight"></canvas>
</div>
<div
class="puzzle-lost-box"
:style="'left:' + left_Num + 'px;width:' + dataWidth + 'px;height:' + dataHeight + 'px;'"
>
<canvas id="puzzle-shadow" ref="puzzleShadow" :width="dataWidth" :height="dataHeight"></canvas>
<canvas id="puzzle-lost" ref="puzzleLost" :width="dataWidth" :height="dataHeight"></canvas>
</div>
<p
class="ver-tips"
ref="verTips"
:style="'height: '+22*scale+'px;line-height:'+22*scale+'px;bottom: ' + (displayTips==true ? 0 : -22*scale ) +'px;font-size: '+12*scale+'px;'"
>
<template v-if="verification">
<span :style="'color:#42ca6b;line-height:'+ 22*scale+'px;'">验证通过</span>
<span :style="'margin-left:'+4*scale+'px;line-height:'+ 22*scale+'px;'">哇喔,怪物吃了拼图,快去登录吧~</span>
</template>
<template v-if="!verification">
<span :style="'color:red;line-height:'+22*scale+'px;'">验证失败:</span>
<span :style="'margin-left:'+4*scale+'px;line-height:'+ 22*scale+'px;'">拖动滑块将悬浮图像正确拼合</span>
</template>
</p>
</div>
<div
class="re-btn"
@click="refreshImg"
:style="'height: '+28*scale+'px;padding: '+1*scale+'px '+16*scale+'px;font-size: '+18*scale+'px;'"
>
<a-icon type="redo" />
</div>
</div>
<br />
<div class="slider-container" :style="'width:' + dataWidth + 'px;'">
<div class="slider-bar" :style="'border-radius:'+ 24*scale+'px;'">
<p
class="slider-bar-text select-none"
onselectstart="return false"
:style="'line-height:' + 28*scale + 'px;font-size:'+12*scale+'px;'"
>按住滑块, 拖动完成上方拼图</p>
</div>
<div
class="slider-btn"
ref="sliderBtn"
@mousedown="startMove"
@touchstart="startMove"
:style="'top: '+ -7*scale + 'px;'"
>
<a-icon
type="pause-circle"
theme="twoTone"
twoToneColor="#52c41a"
:style="'font-size: '+44*scale+'px;'"
/>
<span class="slider-center-zzz">▍▍▍</span>
</div>
</div>
</div>
</template>
<script lang="js">
export default {
name: 'puzzle',
data () {
return {
moveStart: '',
displayTips: false,
verification: false,
randomX: null,
randomY: null,
imgRandom: '',
left_Num: 0,
dataWidth: 404,
dataHeight: 90,
puzzleSize: 100,
deviation: 11,
scale: 1,
reTimer: null,
isleavePage: false
}
},
props: {
width: {
type: [String, Number],
default: 340
},
height: {
type: [String, Number],
default: 120
},
cropImage: {
type: [Boolean],
default: false
},
puzzleImgList: {
type: Array,
default: () => [
'../../../static/img/a.png',
]
},
PlSize: {
type: [String, Number],
default: 48
},
Deviation:{
type: [String, Number],
default: 4
},
padding: {
type: [Number],
default: 20
},
onSuccess: {
type: Function,
default: () => {
alert('验证成功')
}
},
onError: {
type: Function,
default: () => {
alert('验证失败')
}
}
},
methods: {
/* 刷新 */
RandomNum(Min, Max) {
let Range = Max - Min + 1
let Rand = Math.random()
return Min + Math.floor(Rand * Range)
},
refreshImg () {
let imgRandomNum = this.RandomNum(0, this.puzzleImgList.length-1)
this.imgRandom = this.puzzleImgList[imgRandomNum]
this.initCanvas()
},
/* 画布初始化 */
initCanvas () {
this.clearCanvas()
// let self = this
let w = this.dataWidth
let h = this.dataHeight
// let PL_Size = 48
// 2019-02-12开始PL_Size使用可传递参数
let PL_Size = this.puzzleSize
let padding = 20
let MinN_X = padding + PL_Size
let MaxN_X = w - padding - PL_Size - PL_Size / 6
let MaxN_Y = padding
let MinN_Y = h - padding - PL_Size - PL_Size / 6
this.randomX = this.RandomNum(MinN_X, MaxN_X)
this.randomY = this.RandomNum(MinN_Y, MaxN_Y)
let X = this.randomX
let Y = this.randomY
this.left_Num = -X + 10
let d = PL_Size / 3
let c = this.$refs.puzzleBox
let c_l = this.$refs.puzzleLost
let c_s = this.$refs.puzzleShadow
let ctx = c.getContext('2d')
let ctx_l = c_l.getContext('2d')
let ctx_s = c_s.getContext('2d')
ctx.globalCompositeOperation = 'xor'
ctx.shadowBlur = 10
ctx.shadowColor = '#fff'
ctx.shadowOffsetX = 3
ctx.shadowOffsetY = 3
ctx.fillStyle = 'rgba(0,0,0,0.7)'
ctx.beginPath()
ctx.lineWidth = '1'
ctx.strokeStyle = 'rgba(0,0,0,0)'
ctx.moveTo(X, Y)
ctx.lineTo(X + d, Y)
ctx.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
ctx.lineTo(X + 3 * d, Y)
ctx.lineTo(X + 3 * d, Y + d)
ctx.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
ctx.lineTo(X + 3 * d, Y + 3 * d)
ctx.lineTo(X, Y + 3 * d)
ctx.closePath()
ctx.stroke()
ctx.fill()
let img = new Image()
img.src = this.imgRandom
img.onload = function () {
ctx_l.beginPath()
ctx_l.strokeStyle = 'rgba(0,0,0,0)'
ctx_l.moveTo(X, Y)
ctx_l.lineTo(X + d, Y)
ctx_l.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
ctx_l.lineTo(X + 3 * d, Y)
ctx_l.lineTo(X + 3 * d, Y + d)
ctx_l.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
ctx_l.lineTo(X + 3 * d, Y + 3 * d)
ctx_l.lineTo(X, Y + 3 * d)
ctx_l.closePath()
ctx_l.stroke()
ctx_l.shadowBlur = 10
ctx_l.shadowColor = 'black'
ctx_l.clip()
ctx_l.drawImage(img, 0, 0, w, h)
}
ctx_s.beginPath()
ctx_s.lineWidth = '1'
ctx_s.strokeStyle = 'rgba(0,0,0,0)'
ctx_s.moveTo(X, Y)
ctx_s.lineTo(X + d, Y)
ctx_s.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
ctx_s.lineTo(X + 3 * d, Y)
ctx_s.lineTo(X + 3 * d, Y + d)
ctx_s.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
ctx_s.lineTo(X + 3 * d, Y + 3 * d)
ctx_s.lineTo(X, Y + 3 * d)
ctx_s.closePath()
ctx_s.stroke()
ctx_s.shadowBlur = 20
ctx_s.shadowColor = 'black'
ctx_s.fill()
},
/* 通过重置画布尺寸清空画布,这种方式更彻底 */
clearCanvas () {
let c = this.$refs.puzzleBox
let c_l = this.$refs.puzzleLost
let c_s = this.$refs.puzzleShadow
c.setAttribute('height', c.getAttribute('height'))
c_l.setAttribute('height', c.getAttribute('height'))
c_s.setAttribute('height', c.getAttribute('height'))
// c.height = c.height
// c_l.height = c_l.height
// c_s.height = c_s.height
},
/* 按住滑块后初始化移动监听,记录初始位置 */
startMove (e) {
console.log(e)
e = e || window.event
this.$refs.sliderBtn.style.backgroundPosition = '0 -216px'
this.moveStart = e.pageX || e.targetTouches[0].pageX
this.addMouseMoveListener()
},
/* 滑块移动 */
moving (e) {
let self = this
e = e || window.event
let moveX = e.pageX || e.targetTouches[0].pageX
let d = moveX - self.moveStart
let w = self.dataWidth
let PL_Size = this.puzzleSize
let padding = 20
if (self.moveStart === '') {
return ''
}
if (d < 0 || d > (w - padding - PL_Size)) {
return ''
}
self.$refs.sliderBtn.style.left = d + 'px'
self.$refs.sliderBtn.style.transition = 'inherit'
self.$refs.puzzleLost.style.left = d + 'px'
self.$refs.puzzleLost.style.transition = 'inherit'
self.$refs.puzzleShadow.style.left = d + 'px'
self.$refs.puzzleShadow.style.transition = 'inherit'
},
/* 移动结束,验证并回调 */
moveEnd (e) {
let self = this
e = e || window.event
let moveEnd_X = (e.pageX || e.changedTouches[0].pageX) - self.moveStart
let ver_Num = self.randomX - 10
let deviation = this.deviation
let Min_left = ver_Num - deviation
let Max_left = ver_Num + deviation
if (self.moveStart !== '') {
if (Max_left > moveEnd_X && moveEnd_X > Min_left) {
self.displayTips = true
self.verification = true
setTimeout(function () {
if(self.isleavePage){
return
}
self.displayTips = false
self.initCanvas()
}, 2000)
/* 成功的回调函数 */
self.onSuccess()
} else {
self.displayTips = true
self.verification = false
setTimeout(function () {
if(self.isleavePage){
return
}
self.displayTips = false
}, 2000)
/* 失败的回调函数 */
self.onError()
}
}
if (typeof(self.$refs.sliderBtn) !== 'undefined' && typeof (self.$refs.puzzleLost) !== 'undefined' && typeof (self.$refs.puzzleShadow) !== 'undefined') {
self.reTimer = setTimeout(function () {
if(self.isleavePage){
return
}
self.$refs.sliderBtn.style.left = 0
self.$refs.sliderBtn.style.transition = 'left 0.5s'
self.$refs.puzzleLost.style.left = 0
self.$refs.puzzleLost.style.transition = 'left 0.5s'
self.$refs.puzzleShadow.style.left = 0
self.$refs.puzzleShadow.style.transition = 'left 0.5s'
}, 1000)
self.$refs.sliderBtn.style.backgroundPosition = '0 -84px'
}
self.moveStart = ''
},
/* 绑定滑块移动与滑动结束,移动过程中鼠标可在绑定的区域 */
addMouseMoveListener () {
let self = this
document.addEventListener('mousemove', self.moving)
document.addEventListener('touchmove', self.moving)
document.addEventListener('mouseup', self.moveEnd)
document.addEventListener('touchend', self.moveEnd)
}
},
mounted () {
let self = this
this.dataWidth = this.$refs.puzzle.clientWidth*0.884
this.dataHeight = this.dataWidth*(this.height/this.width)
this.scale = this.$refs.puzzle.clientWidth/260
this.puzzleSize = this.dataWidth*(this.PlSize/this.width)
setTimeout(self.refreshImg,0)
},
created () {
let imgRandomNum = this.RandomNum(0, this.puzzleImgList.length - 1)
this.imgRandom = this.puzzleImgList[imgRandomNum]
this.puzzleSize = Number(this.PlSize)
this.deviation = Number(this.Deviation)
// this.dataWidth = this.width
// this.dataHeight = this.height
// this.scale = this.width/260
},
watch: {
},
beforeDestroy() {
this.isleavePage = true
}
}
</script>
<style scoped>
.slider-btn {
position: absolute;
width: 90px;
height: 67px;
left: 0;
top: -7px;
z-index: 12;
cursor: pointer;
background-position: 0 -84px;
transition: inherit;
background-color: #f4f7fe;
border-radius: 45%;
display: flex;
}
.slider-center-zzz {
text-align: center;
line-height: 67px;
position: absolute;
color: #53C300;
height: 40px;
width: 100%;
}
.ver-tips {
position: absolute;
left: 0;
bottom: -22px;
background: rgba(255, 255, 255, 0.9);
height: 22px;
line-height: 22px;
font-size: 12px;
width: 100%;
margin: 0;
text-align: left;
padding: 0 8px;
transition: all 0.4s;
}
.slider-tips {
bottom: 0;
}
.ver-tips i {
display: inline-block;
width: 22px;
height: 22px;
vertical-align: top;
background-position: -4px -1229px;
}
.ver-tips span {
display: inline-block;
vertical-align: top;
line-height: 22px;
color: #455;
}
.active-tips {
display: block;
}
.hidden {
display: none;
}
.re-btn {
position: absolute;
left: 0;
bottom: 0;
height: 28px;
padding: 0 16px;
}
.re-btn a {
display: inline-block;
width: 14px;
height: 14px;
margin: 0px 0;
background-position: 0 -1179px;
cursor: pointer;
}
.re-btn a:hover {
background-position: 0 -1193px;
}
.puzzle-container {
position: relative;
/* padding: 16px 16px 28px; */
/* border: 1px solid #ddd; */
background: #e0e1e3;
/* border-radius: 16px; */
/* height: 83px; */
/* width: 340px; */
}
.slider-container {
position: relative;
margin: auto;
}
.slider-bar {
border: 1px solid #c3c3c3;
border-radius: 24px;
background: #e0e1e3;
box-shadow: 0 1px 1px rgba(12, 10, 10, 0.2) inset;
}
.slider-bar-text {
font-size: 12px;
color: #486c80;
line-height: 28px;
margin: 0;
text-align: right;
padding-right: 22px;
}
#puzzle-box {
position: absolute;
left: 0;
top: 0;
z-index: 22;
}
#puzzle-shadow {
position: absolute;
left: 0;
top: 0;
z-index: 22;
}
#puzzle-lost {
position: absolute;
left: 0;
top: 0;
z-index: 33;
}
.puzzle-lost-box {
position: absolute;
width: 260px;
height: 116px;
left: 0;
top: 0;
z-index: 111;
}
</style>
下面大家看看效果图
不是专业写前端的 凑合看看 😅😅😅
实践是检验真理的唯一方法! 明天见🥰🥰🥰