React實(shí)現(xiàn)雙滑塊交叉滑動
本文實(shí)例為大家分享了React實(shí)現(xiàn)雙滑塊交叉滑動的具體代碼,供大家參考,具體內(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
// 獲取父級
this.parentWidth = ev.target.parentNode.offsetWidth - ev.target.offsetWidth
// 獲取父級
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ì)算絕對值就是線條的寬
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)擊鏈接查看第二種
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React實(shí)現(xiàn)單向數(shù)據(jù)流的方法
本文主要介紹了React實(shí)現(xiàn)單向數(shù)據(jù)流的方法2023-04-04
react中使用js實(shí)現(xiàn)頁面滾動監(jiān)聽(推薦)
這篇文章主要介紹了react中使用js實(shí)現(xiàn)頁面滾動監(jiān)聽,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
React服務(wù)端渲染和同構(gòu)的實(shí)現(xiàn)
本文主要介紹了React服務(wù)端渲染和同構(gòu)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
詳解React中setState回調(diào)函數(shù)
這篇文章主要介紹了詳解React中setState回調(diào)函數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
React?Native?中處理?Android?手機(jī)吞字的解決方案
這篇文章主要介紹了React?Native?中處理?Android?手機(jī)吞字的解決方案,作者在 React Native 0.67.4 環(huán)境下,編寫了一個小 demo 來復(fù)現(xiàn)這個問題,需要的朋友可以參考下2022-08-08
React Native 混合開發(fā)多入口加載方式詳解
這篇文章主要介紹了React Native 混合開發(fā)多入口加載方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
實(shí)現(xiàn)React單頁應(yīng)用的方法詳解
今天我們來學(xué)習(xí)React是如何構(gòu)建起一個單頁應(yīng)用的,React作為目前最流行的前端框架之一,其受歡迎程度不容小覷,從這門框架上我們可以學(xué)到許多其他前端框架所缺失的東西,也是其創(chuàng)新性所在的地方,比如虛擬DOM、JSX等。下面一起來看看。2016-08-08

