日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

滑动解锁

發布時間:2025/3/12 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 滑动解锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

模仿手機解鎖滑動,點擊向右滑動完畢即可顯示解鎖成功。

程序詳解
1.點擊和松開事件
2. 未解鎖之前顯示的樣式和背景色
3. 解鎖之后顯示的樣式和背景色
4. 滑動完畢后才能解鎖

圖片演示

原始界面

點擊滑動后

解鎖成功

下面我們跟隨我一起來欣賞一下代碼

代碼演示

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title></title><!--注:iconfont語法,即矢量圖標--><link rel="stylesheet" href="font/iconfont.css"><style>*{padding: 0;margin: 0;}#box{position: relative;width: 300px;height: 40px;margin: 0 auto;margin-top: 10px; background-color: #e8e8e8;box-shadow: 1px 1px 5px rgba(0,0,0,0.2);}.groundcolor{position: absolute;left:0;top:0;width:40px;height: 40px;background-color: lightblue;}.text{position: absolute;width: 100%;height: 40px;line-height: 40px;font-size: 14px;color: #000;text-align: center;}.slider{position: absolute;left:0;top:0;width: 50px;height: 38px;border: 1px solid #ccc;background: #fff;text-align: center;cursor: move;}.slider>i{position: absolute;top:50%;left:50%;transform: translate(-50%,-50%);}.slider.active>i{color:green;}</style> </head> <body><div id="box" onselectstart="return false;"><div class="groundcolor"></div><div class="text" >滑動解鎖</div><!--添加相應字體圖標的類名即可--><div class="slider"><i class="iconfont icon-double-right"></i></div></div> <script>//獲取元素function getEle(selector){return document.querySelector(selector);}var box = getEle("#box"),//背景色bgColor = getEle(".groundcolor"),txt = getEle(".text"),slider = getEle(".slider"),icon = getEle(".slider>i"),//滑動的距離successMoveDistance = box.offsetWidth- slider.offsetWidth,downX,//是否解鎖成功isSuccess = false;slider.onmousedown = mousedownHandler;//鼠標按下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;}//鼠標移動事件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();};//鼠標松開事件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> </body> </html>

總結

以上是生活随笔為你收集整理的滑动解锁的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。