React實現(xiàn)雙滑塊交叉滑動
更新時間:2021年09月09日 15:03:16 作者:燃燒的冰山..
這篇文章主要為大家詳細(xì)介紹了React實現(xiàn)雙滑塊交叉滑動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了React實現(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)點擊的距離
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
}
// 計算線條的寬 小球交互 計算絕對值就是線條的寬
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>
第二種方式:點擊鏈接查看第二種
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react以create-react-app為基礎(chǔ)創(chuàng)建項目
這篇文章主要介紹了react以create-react-app為基礎(chǔ)創(chuàng)建項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
ReactJS應(yīng)用程序中設(shè)置Axios攔截器方法demo
這篇文章主要為大家介紹了ReactJS應(yīng)用程序中設(shè)置Axios攔截器方法demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
React中useEffect原理的代碼簡單實現(xiàn)詳解
React的useEffect鉤子是React函數(shù)組件中處理副作用,本文將通過一個簡單的例子解釋如何用代碼實現(xiàn)useEffect的基本原理,感興趣的小伙伴可以了解下2023-12-12
字節(jié)封裝React組件手機號自動校驗格式FormItem
這篇文章主要為大家介紹了字節(jié)封裝React組件手機號自動校驗格式FormItem,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08

