React實(shí)現(xiàn)類似淘寶tab居中切換效果的示例代碼
效果
DOM布局
const label = { lettersort: false, paramname: "label", paramid: 0, title: "車(chē)源列表篩選項(xiàng)", option: [{ value: 1, text: "全部" }, { value: 2, text: "本地求購(gòu)" }, { value: 3, text: "精準(zhǔn)收車(chē)" }, { value: 4, text: "全國(guó)收車(chē)" }, { value: 5, text: "同行詢價(jià)" }, { value: 6, text: "可批可售" }, { value: 7, text: "車(chē)抵貸款" }, { value: 8, text: "消費(fèi)貸款" }, { value: 9, text: "商家?guī)烊? }, { value: 10, text: "代理合作" }, { value: 11, text: "過(guò)戶轉(zhuǎn)籍" }, { value: 12, text: "尋車(chē)拖車(chē)" }, { value: 13, text: "解壓抵押" }, { value: 14, text: "抵押核驗(yàn)" } ] } filterDom = () => { let filterJson = label; let arr = filterJson.option; return ( <div ref="filterBar" className="filter-list"> {arr.map((item, index) => { if (item.value == this.state.filterSelect) { return ( <div ref={item.value} className="filter-item active" key={index} value={item.value}> {item.text} <div className="zhishi"></div> </div> ); } else { return ( <div className="filter-item" onClick={() => { this.filterBarClick(item); }} ref={item.value} key={index} value={item.value}> {item.text} </div> ); } })} </div> ); }; render(){ return( <div> ... <div className="filter-content" style={{ display: this.state.filterBarShow }}> {this.filterDom()} <div className="shadow"></div> {/* 按鈕和占位 */} <div className="filte-btn-content" onClick={() => { this.filterBtnClick(); }}> <div className="filte-btn"></div> </div> </div> ... </div> ) }
scss樣式表
.filter { width: 100%; // position: fixed; } .filter-content { overflow: hidden; padding-right: pxToRem(27px); position: relative; background: #fff; .filter-list { display: flex; overflow-x: auto; justify-content: space-between; height: pxToRem(90px); color: #333333; align-items: center; -webkit-overflow-scrolling: touch; font-size: pxToRem(32px); font-family:PingFangSC-Light,PingFang SC; font-weight:300; background: #fff; margin-right: pxToRem(100px); .filter-item { text-align: center; display: flex; // flex-basis: 17px; flex-shrink: 0; white-space: nowrap; padding: 0 pxToRem(25px); background: #fff; height: pxToRem(90px); align-items: center; justify-content: center; } .active{ font-size: pxToRem(36px); font-weight: 600; height: pxToRem(90px); display: flex; align-items: center; justify-content: center; position: relative; flex: 1; flex-direction: column; } .zhishi{ background: url("./../img/zhishi.png"); background-repeat: no-repeat; background-size: 100%; width: pxToRem(25px); height: pxToRem(6px); position: absolute; bottom: pxToRem(10px);; left: 50%; transform: translate(-50%, 0); z-index: 999; } } .shadow{ height: pxToRem(90px); width: pxToRem(133px); position: absolute; right: pxToRem(101px); top: 0; background:linear-gradient(270deg,rgba(255,255,255,1) 0%,rgba(255,255,255,0.14) 100%); pointer-events: none; } .filte-btn{ background: url("./../img/shaixuan.png"); background-repeat: no-repeat; background-size: 100%; width: pxToRem(40px); height: pxToRem(40px); } .filte-btn-content { height: pxToRem(90px); position: absolute; right: pxToRem(27px); top: 0; background: #fff; width: pxToRem(74px); display: flex; align-items: center; justify-content: flex-end; } }
實(shí)現(xiàn)
想要居中展示首先是需要找到中心點(diǎn),然后在點(diǎn)擊是計(jì)算偏移量,把對(duì)應(yīng)的標(biāo)簽滾動(dòng)到中心位置
filterBarClick = param => { const { value, text } = param; this.setState({ filterSelect: value }); let dom = this.refs; //獲取點(diǎn)擊時(shí)當(dāng)前標(biāo)簽的DOM let valDom = dom[value]; //獲取標(biāo)簽父元素DOM let contentDom = dom.filterBar; //計(jì)算當(dāng)前標(biāo)簽到最左側(cè)的寬度 let valLeft = valDom.offsetLeft; //計(jì)算當(dāng)前標(biāo)簽本身的寬度 let valWidth = valDom.clientWidth; //當(dāng)前標(biāo)簽中心點(diǎn)到最左側(cè)的距離 let valCenter = valLeft + valWidth / 2; //可視屏幕寬度 let clientWidth = document.querySelector('body').offsetWidth; //可視屏幕中心點(diǎn)(減去的30是列表兩邊的15像素的留白) let center = (clientWidth - 30) / 2; //計(jì)算當(dāng)前標(biāo)簽中心點(diǎn)和屏幕中心點(diǎn)的偏移量 然后滾動(dòng)相應(yīng)的距離 if (valCenter > center) { contentDom.scrollTo({ left: valCenter - center, behavior: 'smooth' }); } else { contentDom.scrollTo({ left: 0, behavior: 'smooth' }); } };
總結(jié)
到此這篇關(guān)于React實(shí)現(xiàn)類似淘寶tab居中切換效果的文章就介紹到這了,更多相關(guān)react tab居中切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
webpack 2的react開(kāi)發(fā)配置實(shí)例代碼
本篇文章主要介紹了webpack 2的react開(kāi)發(fā)配置實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07詳解基于webpack搭建react運(yùn)行環(huán)境
本篇文章主要介紹了詳解基于webpack搭建react運(yùn)行環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06React中如何設(shè)置多個(gè)className
這篇文章主要介紹了React中如何設(shè)置多個(gè)className問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01React Native實(shí)現(xiàn)地址挑選器功能
這篇文章主要為大家詳細(xì)介紹了React Native仿地址挑選器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10使用Node搭建reactSSR服務(wù)端渲染架構(gòu)
這篇文章主要介紹了使用Node搭建reactSSR服務(wù)端渲染架構(gòu),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08