React实现下拉刷新、图片懒加载、上滑加载更多、图片预加载
1.首先在componentDidMount钩子函数中,调用接口,之后CarList渲染到页面
async componentDidMount() {
this.CarArray = await searchCar({ page: 1, mod: "宝马" });
this.setState({
CarList: this.CarArray
})
this.ScrollHeight = this.refs.loadMore.scrollHeight;
this.EveryHeight = this.ScrollHeight / this.CarArray.length;
this.ViewHeight = document.documentElement.clientHeight;
this.FnSetHeight();
for (let i = 0; i < this.ImageList.length; i++) {
let URL = this.ImageList[i];
let Images = new Image();
Images.src = URL;
Images.onload = function () {
this.ImagLoad++;
if (this.ImagLoad >= this.ImageList.length) {
this.setState({
CarList: this.CarArray,
loadingState: 0,
})
}
}.bind(this)
document.onscroll = this.FnDelayedLoad.bind(this);
}
}
2.将设置图片的高的代码进行封装,以便于下一次调用
FnSetHeight() {
for (let i = 0; i < this.CarArray.length; i++) {
let Car = this.CarArray[i];
Car.heights = this.EveryHeight * i;
if (this.ViewHeight >= Car.heights) {
Car.imageState = 1
this.ImageList.push(`http://www.ibugthree.com/${Car.img_src}`);
} else {
Car.imageState = 0
}
}
}
3.这里的imageState其实是根据不同的情况来显示
ImageState(item) {
if (item.imageState) {
return `http://www.ibugthree.com/${item.img_src}`;
} else {
return `http://www.ibugthree.com/default.gif`;
}
}
4.最后的话添加它的上滑加载更多
async FnDelayedLoad() {
this.ScrollTop = document.documentElement.scrollTop;
this.ViewHeight = document.documentElement.clientHeight;
for (let i = 0; i < this.state.CarList.length; i++) {
let Car = this.state.CarList[i];
this.ScrollHeight = this.refs.loadMore.scrollHeight;
this.EveryHeight = this.ScrollHeight / this.state.CarList.length;
Car.heights = this.EveryHeight * i;
if (this.ScrollTop + this.ViewHeight >= Car.heights) {
Car.imageState = 1
} else {
Car.imageState = 0
}
this.setState({
CarList: this.state.CarList
})
}
this.ScrollHeight = this.refs.loadMore.scrollHeight;
if (this.ScrollTop + this.ViewHeight >= this.ScrollHeight) {
if (this.LoadingSwith) {
this.LoadingSwith=false
this.page++;
let CarData = await searchCar({ mod: "宝马", page: this.page });
this.state.CarList = this.state.CarList.concat(CarData);
this.LoadingSwith=true
this.setState({
CarList: this.state.CarList.concat(CarData)
})
}
}
this.state.LoadingMore = true;
this.FnSetHeight();
this.setState({
CarList: this.state.CarList
})
}
最后实现一下下拉刷新
FnStart(ev) {
this.refs.loadMore.classList.remove('active');
this.startY = ev.changedTouches[0].pageY
this.PageY = this.startY - this.refs.loadMore.getBoundingClientRect().top;
}
FnMove(ev) {
this.ScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
this.moveY = ev.changedTouches[0].pageY;
if (this.moveY > this.startY && this.ScrollTop==0) {
this.Y = this.moveY - this.PageY;
this.scale = 1 - this.Y / 736;
if (this.scale <= .58) this.scale = .58;
this.refs.loadMore.style.transform = `translate(0,${this.Y * this.scale}px)`;
}
}
async FnEnd(ev) {
this.UpY = ev.changedTouches[0].pageY;
this.refs.loadMore.classList.add('active');
this.ScrollTop =
document.documentElement.scrollTop || document.body.scrollTop;
console.log(this.ScrollTop)
if (this.ScrollTop ==0) {
setTimeout(() => {
this.refs.loadMore.style.transform = `translate(0,0)`;
}, 3000)
if (this.UpY > this.startY) {
}
}
}
state里面定义的数据
constructor(...args) {
super(...args);
this.state = {
CarList: [],
loadingState: 1,
}
this.ImageList = []
this.ImagLoad = 0
this.page = 0
this.LoadingSwith = true
}
css部分
.load-more {
position: relative;
}
.load-more .load-more-loading {
width: 100px;
height: 100px;
background-size: cover;
position: absolute;
left: 0;
right: 0;
margin: auto;
top: 30px;
}
.load-more .shadow {
width: 100vw;
height: 100vh;
position: fixed;
left: 0;
top: 0;
background: #fff;
}
.Loading-shadow{
position: absolute;
height:100vh;
width:100vw;
left: 0px;
top: 0px;
z-index: 2;
touch-action:none;
background-color: white;
}
.load-more .shadow .loading {
width: 100px;
height: 40px;
margin: 0 auto;
margin-top: 100px;
}
.load-more .shadow .loading span {
display: inline-block;
width: 8px;
height: 100%;
border-radius: 4px;
background: lightgreen;
-webkit-animation: load 1s ease infinite;
margin: 0 3px 0 3px;
}
@-webkit-keyframes load {
0%,
100% {
height: 40px;
background: lightgreen;
}
50% {
height: 70px;
margin: -15px 0;
background: lightblue;
}
}
.load-more .shadow .loading span:nth-child(2) {
-webkit-animation-delay: 0.2s;
}
.load-more .shadow .loading span:nth-child(3) {
-webkit-animation-delay: 0.4s;
}
.load-more .shadow .loading span:nth-child(4) {
-webkit-animation-delay: 0.6s;
}
.load-more .shadow .loading span:nth-child(5) {
-webkit-animation-delay: 0.8s;
}
.load-more .load-more-list.active {
transition: .25s ease-in all;
}
.load-more .load-more-list {
background: #fff;
position: relative;
z-index: 2;
}
.load-more .load-more-list .load-more-car::after {
display: block;
content: "";
width: 723px;
height: 1px;
background: #ededed;
position: absolute;
right: 0;
margin: 42px 0 29px 0;
}
.load-more .load-more-list .load-more-car {
position: relative;
height: 230px;
padding: 29px 0 0 0;
}
.load-more .load-more-list .load-more-car .load-more-item {
display: flex;
flex-wrap: wrap;
}
.load-more .load-more-list .load-more-car .load-more-item .load-more-left {
padding: 0 0 0 14px;
width: 249px;
height: 187px;
}
.load-more .load-more-list .load-more-car .load-more-item .load-more-left .load-more-kmr {
width: 249px;
height: 187px;
border-radius: 10px;
}
.load-more .load-more-list .load-more-car .load-more-item .load-more-right {
padding: 0 0 0 19px;
}
.load-more .load-more-list .load-more-car .load-more-item .load-more-right .load-more-title {
width: 426px;
font-size: 27px;
color: #000;
font-weight: bold;
}
.load-more .load-more-list .load-more-car .load-more-item .load-more-right .load-more-information {
display: flex;
color: #ababab;
font-size: 19px;
line-height: 19px;
padding: 11px 0 0 0;
}
.load-more .load-more-list .load-more-car .load-more-item .load-more-right .load-more-information .load-more-line {
width: 3px;
height: 19px;
background: #ababab;
margin: 0px 7px 0 8px;
}
有什么不懂的可以私信我呀!!!!
|