原生JS實(shí)現(xiàn)滑動(dòng)按鈕效果
利用Js制作的滑動(dòng)按鈕的具體代碼,供大家參考,具體內(nèi)容如下
首先貼上效果圖
再貼上源碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div style="position: relative;width:100vw;height:100vh"> <div id="container"> <svg style="width:inherit;height:inherit"> <circle id="c" cx="25" cy="25" r="23" style="fill:white; stroke:gray; stroke-width:2;" onmousedown="down(event)" onmouseup="up(event)" onmouseleave="up(event)" /> </svg> </div> </div> <!-- <script> setTimeout(function () { let c = document.querySelector('circle'); console.log(c.parentNode.parentNode.style) },500) </script> --> <style> body{ margin:0; background-color:azure; } #container{ position:absolute; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%); width: 200px; height: 50px; background-color: black; border-radius: 50px; } </style> <script> let circle = document.getElementById('c'), clicked = false, x = 0,y = 0; circle.addEventListener("mousemove",function(e){ x = e.offsetX; if(clicked){ circle.setAttribute("cx",x) } }) function t(e){ circle.setAttribute("cx",e.offsetX); } function down(e){ clicked = true; } function up(){ if(clicked){ let flag; if(x <= 100) new Promise(function(resolve,reject){ flag = setInterval(function(){ x -= 2; circle.setAttribute("cx",x); if(x <= 25){ circle.setAttribute("cx",25) circle.setAttribute("style","fill:white; stroke:gray; stroke-width:2;") resolve("ok") } }) }).then(res => { clearInterval(flag) }) else new Promise(function(resolve,reject){ flag = setInterval(function(){ x += 2; circle.setAttribute("cx",x); if(x >= 175){ circle.setAttribute("cx",175); circle.setAttribute("style","fill:black; stroke:gray; stroke-width:2;") resolve("ok") } }) }).then(res => { clearInterval(flag) }) } clicked = false; } </script> </body> </html>
知識(shí)點(diǎn)和制作思路及步驟
1、基本布局(父相子絕,left: 50%; top: 50%; transform: translateX(-50%)
translateY(-50%);)
2、svg的circle( cx )控制移動(dòng), 對(duì)于circle的cx采用setAtrribute來進(jìn)行控制。
3、**Promise.then()**用來保證結(jié)束后進(jìn)行clearInterval
4、circle監(jiān)聽了mousemove,mouseup,mousedown事件。 當(dāng)mousedown事件觸發(fā)會(huì)將cliked置為true進(jìn)而move事件會(huì)進(jìn)行reset;
5、mouseup和mouseleave會(huì)將cliked置為false;進(jìn)而無法觸發(fā)move事件的reset(停止);
6、當(dāng)停止?fàn)顟B(tài)下,判斷原點(diǎn)在左側(cè)還是右側(cè), 動(dòng)畫: 如果在左半部分則利用setInterval進(jìn)行10ms一幀每次1.5px的移動(dòng),判斷到達(dá)開始或者結(jié)束點(diǎn)則停止。
7、再進(jìn)行樣式切換。
代碼本人全部原創(chuàng),請(qǐng)盡情抄襲,代碼寫完沒有經(jīng)過整理,可能存在無效變量,僅僅代表我的思路。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序如何使用canvas二維碼保存至手機(jī)相冊(cè)
這篇文章主要介紹了微信小程序如何使用canvas二維碼保存至手機(jī)相冊(cè)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07js動(dòng)態(tài)往表格的td中添加圖片并注冊(cè)事件
這篇文章主要介紹了使用js動(dòng)態(tài)往表格的td中添加圖片并注冊(cè)事件,需要的朋友可以參考下2014-06-06PhotoSwipe異步動(dòng)態(tài)加載圖片方法
這篇文章主要為大家詳細(xì)介紹了PhotoSwipe異步動(dòng)態(tài)加載圖片方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08從QQ網(wǎng)站中提取的純JS省市區(qū)三級(jí)聯(lián)動(dòng)菜單
在瀏覽網(wǎng)頁過程中發(fā)現(xiàn)QQ自己的JS省市區(qū)三級(jí)聯(lián)動(dòng)。所以研究了一下,就將其提取出來了。他的界面如下,喜歡的朋友可以學(xué)習(xí)下2013-12-12一個(gè)級(jí)聯(lián)菜單(IE ONLY),不過代碼很精簡(jiǎn)!
一個(gè)級(jí)聯(lián)菜單(IE ONLY),不過代碼很精簡(jiǎn)!...2006-10-10深入淺出ES6新特性之函數(shù)默認(rèn)參數(shù)和箭頭函數(shù)
這篇文章主要介紹了深入淺出ES6新特性之函數(shù)默認(rèn)參數(shù)和箭頭函數(shù) 的相關(guān)資料,需要的朋友可以參考下2016-08-08