react360全屏滚动

目录

在这里插入图片描述

index.jsx

import React from "react";
import { Fullpage, FullpageItem } from "../fullpage/fullpage";
class LoginPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  render() {
    return (
      <div className="box_container">
        <Fullpage>
          <FullpageItem>
            <div>
                <img src={require("../.././icon/国旗杆.webp")} alt="" />
            </div>
          </FullpageItem>
          <FullpageItem>
          <div>
                <img src={require("../.././icon/壁纸.jpg")} alt="" />
            </div>
          </FullpageItem>
          <FullpageItem>
            <div>
                <img src={require("../.././icon/青铜鼎.jpg")} alt="" />
            </div>
          </FullpageItem>
          <FullpageItem>
            <div>
                <img src={require("../.././icon/天安门.webp")} alt="" />
            </div>
          </FullpageItem>
          <FullpageItem>
            <div>
                <img src={require("../.././icon/国旗.webp")} alt="" />
            </div>
          </FullpageItem>
          <FullpageItem>
            <div>
                <img src={require("../.././icon/勿忘国耻.webp")} alt="" />
            </div>
          </FullpageItem>
        </Fullpage>
      </div>
    );
  }
}
export default LoginPage

fullpage.jsx

import React from "react";
import "./fullpage.css";
import { throttle } from "../../utlis/loadash";

class Fullpage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      currentPage: 1,
      pageList: this.props.children,
    };

    this.initPageListFromPropsChildren(); // 类似于vue的插槽,props带过来的children(FullpageItem)
    this.handleScroll = throttle(this.handleScroll, 600, true); //屏幕滚动时的节流函数
  }
  debounceHandleScroll = (e) => {
    e.persist();
    this.handleScroll(e);
  };
  changeCurrentPage(index) {
    this.setState({ currentPage: index });
  }
  handleScroll(e) {
    if (e.deltaY > 0) {
      if (this.state.currentPage === this.state.pageList.length) return;
      this.changeCurrentPage(this.state.currentPage + 1);
    } else {
      if (this.state.currentPage === 1) return;
      this.changeCurrentPage(this.state.currentPage - 1);
    }
  }
  initPageListFromPropsChildren() {
    // 初始化pageList(插槽)
    if (this.state.pageList === undefined) this.setState({ pageList: [] });
    if (
      typeof pageList === "object" &&
      this.state.pageList.length === undefined
    )
      this.setState({ pageList: [this.state.pageList] });
  }
  render() {
    return (
      <div className="fullpage_container" onWheel={this.debounceHandleScroll}>
        {this.state.pageList.map((x, i) => (
          <div
            className={`fullpage_item item_${i + 1}`}
            key={i}
            style={{
              top: -this.state.currentPage + i + 1 + "00vh",
            }}
          >
            {x}
          </div>
        ))}
        <div className="point_nav">
          {this.state.pageList.map((x, i) => (
            <div
              className="point_nav_item"
              style={{
                background:
                  i + 1 === this.state.currentPage ? "#333" : "#cccaca",
              }}
              key={i}
              onMouseEnter={() => this.changeCurrentPage(i + 1)}
            ></div>
          ))}
        </div>
      </div>
    );
  }
}
const FullpageItem = (props) => {
  let { children } = props;
  return <div style={{ height: "100%", width: "100%" }}>{children}</div>;
};
export { Fullpage, FullpageItem };


fullpage.css

.fullpage_container {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  position: relative;
}
.fullpage_container .fullpage_item {
  width: 100%;
  height: 100vh;
  position: absolute;
  transition: top 500ms;
}
img {
  width: 100%;
  height: 100vh;
}
.point_nav {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 100;
}
.point_nav .point_nav_item {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-bottom: 10px;
  transition: background 0.5s;
}

loadash.js

export function throttle(func, wait, immediate) {
    let previous = 0;
    let timeout;
    return function (...args) {
      if (immediate) {
        let now = Date.now();
        if (now - previous > wait) {
          func.apply(this, args);
          previous = now;
        }
      } else {
        if (!timeout) {
          timeout = setTimeout(() => {
            timeout = null;
            func.apply(this, args);
          }, wait);
        }
      }
    };
  }
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No DeBug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值