React實(shí)現(xiàn)雙滑塊交叉滑動(dòng)
本文實(shí)例為大家分享了React實(shí)現(xiàn)雙滑塊交叉滑動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
html代碼:
<body> <div id="root"></div> </body>
script代碼:
<script type="text/babel"> const root = document.querySelector('#root') class Comp extends React.Component { constructor(...args) { super(...args) } fn(ev) { // 獲取鼠標(biāo)點(diǎn)擊的距離 this.pageX = ev.changedTouches[0].pageX - ev.target.offsetLeft // 獲取父級(jí) this.parentWidth = ev.target.parentNode.offsetWidth - ev.target.offsetWidth // 獲取父級(jí) this.parent = ev.target.parentNode // 獲取線條 this.line = this.parent.children[2] // 獲取左邊小球 this.oBall = this.parent.children[0] // 右邊小球 this.oBallTwo = this.parent.children[1] document.ontouchmove = this.fnMove.bind(this) document.ontouchend = this.fnEnd } fnMove(ev) { // 盒子偏移量 this.X = ev.changedTouches[0].pageX - this.pageX // 判斷偏移量不能大于父盒子的寬 if (this.X >= this.parentWidth) { this.X = this.parentWidth } // 判斷不能小于0 if (this.X <= 0) { this.X = 0 } // 計(jì)算線條的寬 小球交互 計(jì)算絕對(duì)值就是線條的寬 this.lineWidth = Math.abs(this.oBallTwo.offsetLeft - this.oBall.offsetLeft) // 線條的寬度 this.line.style.width = this.lineWidth + 'px' // 小球距離左邊的距離 ev.target.style.left = this.X + 'px' // 判斷右邊小球的offsetLeft減掉左邊小球的offsetLeft值 如果小于0就是 右邊小球距離左邊最近 取出它的offsetLeft值就是線條距離左邊的值 if(this.oBallTwo.offsetLeft-this.oBall.offsetLeft<0){ this.line.style.left=this.oBallTwo.offsetLeft+'px' }else{ this.line.style.left=this.oBall.offsetLeft+'px' } } fnEnd() { document.ontouchmove = null document.ontouchend = null } render() { return (<div className='box'> <div className='ball' onTouchStart={this.fn.bind(this)}></div> <div className='ball ac' onTouchStart={this.fn.bind(this)}></div> <div className='line'></div> <div className='lineT'></div> </div>) } } ReactDOM.render(<Comp />, root) </script>
css樣式:
<style> body { margin: 0; padding: 0; } .box { width: 500px; height: 40px; background: #999; position: relative; margin: auto; margin-top: 100px; } .ball { width: 40px; height: 40px; background: red; position: absolute; border-radius: 50%; z-index: 10; } .ball.ac { background: #0f0; right: 0; } .line { height: 5px; width: 500px; background: rgb(200, 110, 7); position: absolute; top: 50%; left: 0; transform: translate(0, -50%); z-index: 5; } .lineT { height: 5px; width: 500px; background: #fff; position: absolute; top: 50%; left: 0; transform: translate(0, -50%); } </style>
第二種方式:點(diǎn)擊鏈接查看第二種
vue實(shí)現(xiàn)小球滑動(dòng)交叉效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React實(shí)現(xiàn)單向數(shù)據(jù)流的方法
本文主要介紹了React實(shí)現(xiàn)單向數(shù)據(jù)流的方法2023-04-04react中使用js實(shí)現(xiàn)頁(yè)面滾動(dòng)監(jiān)聽(tīng)(推薦)
這篇文章主要介紹了react中使用js實(shí)現(xiàn)頁(yè)面滾動(dòng)監(jiān)聽(tīng),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04React服務(wù)端渲染和同構(gòu)的實(shí)現(xiàn)
本文主要介紹了React服務(wù)端渲染和同構(gòu)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04詳解React中setState回調(diào)函數(shù)
這篇文章主要介紹了詳解React中setState回調(diào)函數(shù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06React?Native?中處理?Android?手機(jī)吞字的解決方案
這篇文章主要介紹了React?Native?中處理?Android?手機(jī)吞字的解決方案,作者在 React Native 0.67.4 環(huán)境下,編寫(xiě)了一個(gè)小 demo 來(lái)復(fù)現(xiàn)這個(gè)問(wèn)題,需要的朋友可以參考下2022-08-08React Native提供自動(dòng)完成的下拉菜單的方法示例
這篇文章主要為大家介紹了React Native提供自動(dòng)完成的下拉菜單的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10React Native 混合開(kāi)發(fā)多入口加載方式詳解
這篇文章主要介紹了React Native 混合開(kāi)發(fā)多入口加載方式詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09React中Refs的使用場(chǎng)景及核心要點(diǎn)詳解
在使用?React?進(jìn)行開(kāi)發(fā)過(guò)程中,或多或少使用過(guò)?Refs?進(jìn)行?DOM?操作,這篇文章主要介紹了?Refs?功能和使用場(chǎng)景以及注意事項(xiàng),希望對(duì)大家有所幫助2023-07-07實(shí)現(xiàn)React單頁(yè)應(yīng)用的方法詳解
今天我們來(lái)學(xué)習(xí)React是如何構(gòu)建起一個(gè)單頁(yè)應(yīng)用的,React作為目前最流行的前端框架之一,其受歡迎程度不容小覷,從這門(mén)框架上我們可以學(xué)到許多其他前端框架所缺失的東西,也是其創(chuàng)新性所在的地方,比如虛擬DOM、JSX等。下面一起來(lái)看看。2016-08-08