react实现图片左右滑动和下拉加载

背景

移动端实现图片左右滑动,滑到右边加载更多,图片也可以上下滚动,滑到底部加载更多

实现

主要是通过react实现,也可以使用vue或者jquery都可以,实现方式可以做参考。代码只需要把imgList数组中的图片地址换掉就可以直接查看效果

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React 实例</title>
<!-- 可能会存在cdn访问失败的问题,自己在菜鸟类似的站点可以找到下面3个可以访问的react 核心库的cdn -->
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<style type="text/css">

.mall_list_container {
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  padding: 10px 24px;
}

img {
  width: 300px;
  height: 400px;
  margin-left: 10px;
}

/* 左右滑动的关键css代码 */
.hot_goods_content {
  overflow: auto;
  display: flex;
}

/* 上下滑动的关键css代码,要设置一个高度 */
.good_list {
  height: 600px;
  overflow-y: scroll;
}

.good_list img {
  display: block;
  margin-top: 10px;
  width: 260px;
  height: 300px;
}

</style>
<body>

<div id="example"></div>
<script type="text/babel">

//实现左右滑动并滑到最右边加载更多
class GoodsList extends React.Component{
    constructor(props) {
        super(props);
        //图片可以换成可以访问到的图片地址
        this.state={
            imgList:['img1.png','img2.png','good1.png','good2.png','img1.png','img2.png','good1.png']
        }
    }
    componentDidMount() {
        //左右滑动的关键js代码,this.hotGoods为将要滑动的内容区间所在的DOM元素
        this.hotGoods.addEventListener('scroll',()=>{
            //clientWidth:为元素所在的宽度,可以看见的元素宽度,即this.hotGoods的宽度
            const clientWidth = this.hotGoods.clientWidth; 
            //scrollWidth:为元素里面所有内容的宽度包括滚动的内容加起来
            const scrollWidth = this.hotGoods.scrollWidth;  
            //scrollLeft:当前滚动到的位置到刚出现滚动条的位置的距离
            const scrollLeft = Math.ceil(this.hotGoods.scrollLeft) ;
            // 把上面三个参数打印出来可以清晰的看出他们之间的关系
            //还可以参考:https://www.runoob.com/jsref/prop-element-clientwidth.html,图画的很清楚
            if(scrollWidth-clientWidth<= scrollLeft) {
                //进到这里说明图片滑到了最右边了
                const {imgList} = this.state;
                //在imgList中push来模拟加载,实际实现上可以调用分页接口来加载数据
                this.setState({
                    imgList:[...imgList,...imgList.slice(0,2)]
                })
            }
        })
    }
    render() {
        const {imgList} = this.state;
        const {goodIndex} = this.props;
        return (
            <div className="hot_recommend">
                <h3>左右滑动</h3>
                <div className="hot_goods_content" ref={(node)=>this.hotGoods = node}>
                    {
                        imgList.map((img,index)=>(
                            <img src={img} key={index}/>
                        ))
                    }
                </div>
            </div>
        )
    }
}
//上下滚动,到底部加载更多
class MoreGoods extends React.Component{
    constructor(props) {
        super(props);
        this.state={
            imgList:['img1.png','img2.png','good1.png','good2.png','img1.png','img2.png','good1.png']
        }
    }
    componentDidMount() {
        let box = document.querySelector('.good_list');
        box.addEventListener('scroll', ()=> {
            //clientHeight:元素的像素高度,包含元素的高度+内边距,不包含水平滚动条、边框和外边距
            const clientHeight = this.refs.goodList.clientHeight;

            //scrollHight:元素内容的高度,包括溢出的不可见
            const scrollHight = this.refs.goodList.scrollHeight;
            //scrollTop: 滚动条滑动的位置到开始出现滚动条的距离长度
            const scrollTop = Math.ceil(this.refs.goodList.scrollTop);
            // console.log(scrollHight,'scrollHight')
            // console.log(clientHeight,'clientHeight')
            // console.log(scrollTop,'scrollTop')
            if (scrollTop >= scrollHight - clientHeight) {
                const {imgList} = this.state;
                //模拟加载
                setTimeout(()=>{
                    this.setState({
                      imgList:[...imgList,...imgList.slice(0,3)]
                    })
                },1000)
                
            }
        })

    }
    render() {
        const {imgList} = this.state;
        const {moreIndex} = this.props;
        console.log(imgList.length,'图片长度')
        return (
            <div className="hot_recommend">
                <h3>上下滚动</h3>
                <div className="good_list" ref="goodList">
                    {imgList.map((item,index)=>(
                        <img src={item} key={index}/>
                    ))}
                </div>
            </div>
        )
    }
}

class MallHome extends React.Component{
  render () {
    return (
        <div className="mall_list_container" ref="mallContainer">
          <GoodsList/>
          <MoreGoods/>
        </div>
    )
  }
}
ReactDOM.render(
  <MallHome />,
  document.getElementById('example')
);
</script>

</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React实现列表图片的懒,我们可以结合使用 `react-lazyload` 库和 `Intersection Observer API`。以下是基于这两个工具的实现步骤: 1. 首先,安装 `react-lazyload` 库。 ```bash npm install react-lazyload ``` 2. 在列表组件中引入 `react-lazyload` 组件,并使用 `Intersection Observer API` 监听图片是否进入可视区域。 ```jsx import React from 'react'; import LazyLoad from 'react-lazyload'; const ListItem = ({ imageUrl, altText }) => { return ( <LazyLoad> <img src={imageUrl} alt={altText} /> </LazyLoad> ); }; const ListComponent = ({ items }) => { return ( <ul> {items.map((item) => ( <li key={item.id}> <ListItem imageUrl={item.imageUrl} altText={item.altText} /> </li> ))} </ul> ); }; export default ListComponent; ``` 在上述代码中,我们使用 `react-lazyload` 组件包裹了 `<img>` 元素,并将图片的地址和替代文本作为属性传递给子组件 `ListItem`。当图片进入可视区域时,`react-lazyload` 会自动图片。 3. 在父组件中使用 `ListComponent`,并传递图片数据给列表组件。 ```jsx import React from 'react'; import ListComponent from './ListComponent'; const App = () => { const items = [ { id: 1, imageUrl: 'image1.jpg', altText: 'Image 1' }, { id: 2, imageUrl: 'image2.jpg', altText: 'Image 2' }, { id: 3, imageUrl: 'image3.jpg', altText: 'Image 3' }, // 更多图片数据 ]; return ( <div> <h1>List of Images</h1> <ListComponent items={items} /> </div> ); }; export default App; ``` 在父组件中,我们创建了一个包含图片数据的数组,并将其传递给列表组件 `ListComponent`。列表组件会根据传递的数据生成相应的图片项。 通过以上步骤,我们可以在 React实现列表图片的懒。 `react-lazyload` 库会自动处理图片,只有当图片进入可视区域时才进行,从而提升页面性能和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值