npm上已有react-fullpage,可是他的實現方式是使用錨點,從新刷新後會出現bug.
所以本身造了一個輪子.歡迎你們使用,star,PRjavascript
插件已經上傳npm.
css
npm install react-fullslip
複製代碼
import {FullSlip,SlipItem} from "react-fullslip";
複製代碼
render() {
let options = {
navigation: true,//是否開啓導航點,默認爲true
activeClass: 'active',自定義導航點類名,默認爲active,.navigation-dot下的.active
duration:1000,//屏幕滑動切換的時間,默認爲1000
transverse:true,//是否更改成橫向滑動,默認爲false
navImage:[require('./assets/1.jpg'),require('./assets/2.jpg'),require('./assets/3.jpg')]//導航點圖片,可選,默認無圖片
arrowNav:true, //是否開啓箭頭導航 默認false不開啓
};
return (
<div className="App">
<FullSlip {...options}>
<SlipItem style={{backgroundColor:'#C6E2FF'}}>
page1
</SlipItem>
<SlipItem style={{backgroundColor:'#C1FFC1'}}>
page2
</SlipItem>
<SlipItem style={{backgroundColor:'#FFEC8B'}}>
page3
</SlipItem>
</FullSlip>
</div>
);
}複製代碼
這裏我定義了兩個組件,FullSlip和SlipItem,由FullSlip包住SlipItem而且在SlipItem裏面完成頁面.
html
全屏滾動的容器組件在componentDidMount時綁定mouseWheel事件,這裏作了兼容處理:
componentDidMount() {
if (document.addEventListener) {
document.addEventListener('DOMMouseScroll', this.mouseScroll.bind(this), false);
}
window.onmousewheel = document.onmousewheel = this.mouseScroll.bind(this);
}
複製代碼
mouseScroll中的邏輯:
java
//翻頁函數 n=1向後翻頁 n=-1向前翻頁
scroll(n) {
this.setState({
isScroll: true,
currentPage: this.state.currentPage + n
});
setTimeout(() => {//動畫 duration時間結束後再把狀態切換爲沒有滑動
this.setState({
isScroll: false
})
}, this.state.duration);
}//給document/window綁定的滾輪時間
mouseScroll(e) {
e = e || window.event;
if (this.state.isScroll) {
return false;//若是正在滾動,取消事件
}
if (e.wheelDelta < 0 || e.detail > 0) {//小於0說明向下滾動
if (this.state.currentPage >= this.state.pageCount) {//邊界判斷
return false;
}
this.scroll(1)
} else if (e.wheelDelta > 0 || e.detail < 0) {
if (this.state.currentPage <= 0) {
return false;
}
this.scroll(-1)
}
};
複製代碼
點擊導航切換到對應頁:
react
//給導航點綁定點擊事件 index爲傳入的頁面索引
handleNavClick(index) {
this.setState({
currentPage: index
})
}
複製代碼
//給箭頭導航綁定點擊事件
handleArrowClick(n) {
if (this.state.currentPage > this.state.pageCount) {//邊界判斷
return false;
}
this.scroll(n);
}複製代碼
爲了避免顯示全屏滾動條,在css中作了處理
git
//隱藏滾動條
html {
overflow: -moz-scrollbars-none; //firefox
-ms-overflow-style: none; //ie
}
html::-webkit-scrollbar { /*webkit內核*/
display: none;
}複製代碼
單純的頁面容器組件,在這裏能夠編寫頁面
再次歡迎你們使用,star,PRgithub