React實(shí)現(xiàn)類似淘寶tab居中切換效果的示例代碼
效果
DOM布局
const label = {
lettersort: false,
paramname: "label",
paramid: 0,
title: "車源列表篩選項(xiàng)",
option: [{
value: 1,
text: "全部"
},
{
value: 2,
text: "本地求購"
},
{
value: 3,
text: "精準(zhǔn)收車"
},
{
value: 4,
text: "全國收車"
},
{
value: 5,
text: "同行詢價(jià)"
},
{
value: 6,
text: "可批可售"
},
{
value: 7,
text: "車抵貸款"
},
{
value: 8,
text: "消費(fèi)貸款"
},
{
value: 9,
text: "商家?guī)烊?
},
{
value: 10,
text: "代理合作"
},
{
value: 11,
text: "過戶轉(zhuǎn)籍"
},
{
value: 12,
text: "尋車拖車"
},
{
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)文章
React 中常用的幾種路由跳轉(zhuǎn)方式小結(jié)
基本路由跳轉(zhuǎn)是最常見的一種方式,下面介紹React 中常用的幾種路由跳轉(zhuǎn)方式,感興趣的朋友一起看看吧2023-12-12
React中進(jìn)行條件渲染的實(shí)現(xiàn)方法
React是一種流行的JavaScript庫,它被廣泛應(yīng)用于構(gòu)建Web應(yīng)用程序,在React中,條件渲染是一個(gè)非常重要的概念,它允許我們根據(jù)不同的條件來呈現(xiàn)不同的內(nèi)容,在本文中,我們將探討React如何進(jìn)行條件渲染,需要的朋友可以參考下2023-11-11
React實(shí)現(xiàn)Excel文件的導(dǎo)出與在線預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了如何利用?React?18?的強(qiáng)大功能,演示如何使用?React?18?編寫?Excel?文件的導(dǎo)出與在線預(yù)覽功能,需要的小伙伴可以參考下2023-12-12
React native ListView 增加頂部下拉刷新和底下點(diǎn)擊刷新示例
這篇文章主要介紹了React native ListView 增加頂部下拉刷新和底下點(diǎn)擊刷新示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
react中如何對(duì)自己的組件使用setFieldsValue
react中如何對(duì)自己的組件使用setFieldsValue問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
react框架next.js學(xué)習(xí)之API?路由篇詳解
這篇文章主要為大家介紹了react框架next.js學(xué)習(xí)之API?路由篇詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
react實(shí)現(xiàn)列表滾動(dòng)組件功能
在開發(fā)項(xiàng)目的時(shí)候,從服務(wù)端獲取到數(shù)據(jù)列表后,展示給用戶看,需要實(shí)現(xiàn)數(shù)據(jù)自動(dòng)滾動(dòng)效果,怎么實(shí)現(xiàn)呢,下面小編給大家分享react實(shí)現(xiàn)列表滾動(dòng)組件功能實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2023-09-09

