代码拉取完成,页面将自动刷新
//三、给滑块添加鼠标按下事件
slider.onmousedown = mousedownHandler;
//3.1鼠标按下事件的方法实现
function mousedownHandler(e){
bgColor.style.transition = "";
slider.style.transition = "";
var e = e || window.event || e.which;
downX = e.clientX;
//在鼠标按下时,分别给鼠标添加移动和松开事件
document.onmousemove = mousemoveHandler;
document.onmouseup = mouseupHandler;
};
//四、定义一个获取鼠标当前需要移动多少距离的方法
function getOffsetX(offset,min,max){
if(offset < min){
offset = min;
}else if(offset > max){
offset = max;
}
return offset;
}
//3.1.1鼠标移动事件的方法实现
function mousemoveHandler(e){
var e = e || window.event || e.which;
var moveX = e.clientX;
var offsetX = getOffsetX(moveX - downX,0,successMoveDistance);
bgColor.style.width = offsetX + "px";
slider.style.left = offsetX + "px";
if(offsetX == successMoveDistance){
success();
}
//如果不设置滑块滑动时会出现问题(目前还不知道为什么)
e.preventDefault();
};
//3.1.2鼠标松开事件的方法实现
function mouseupHandler(e){
if(!isSuccess){
bgColor.style.width = 0 + "px";
slider.style.left = 0 + "px";
bgColor.style.transition = "width 0.8s linear";
slider.style.transition = "left 0.8s linear";
}
document.onmousemove = null;
document.onmouseup = null;
};
//五、定义一个滑块解锁成功的方法
function success(){
isSuccess = true;
txt.innerHTML = "解锁成功";
bgColor.style.backgroundColor ="lightgreen";
slider.className = "slider active";
icon.className = "iconfont icon-xuanzhong";
//滑动成功时,移除鼠标按下事件和鼠标移动事件
slider.onmousedown = null;
document.onmousemove = null;
};
</script>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。