全屏上下滚动效果

效果如下:

 

1,准备结构

<div id="root">
    <div ref="container" id="container" @mousewheel="scrollDirection">
      <div class="page page1" :style="{width: screenWidth+'px',height: screenHeight+'px'}">page 1</div>
      <div class="page page2" :style="{width: screenWidth+'px',height: screenHeight+'px'}">page 2</div>
      <div class="page page3" :style="{width: screenWidth+'px',height: screenHeight+'px'}">page 3</div>
      <div class="page page4" :style="{width: screenWidth+'px',height: screenHeight+'px'}">page 4</div>
    </div>
</div>

2,设置必要样式(缺一不可)

      #container {
        overflow: hidden;
        height: 100vh;
      }
      .page {
        position: relative;
        transition: all 1s ease;
        top: 0;
      }

3,初始化,定义数据,获取屏幕宽高,总页数

        data() {
          return {
            screenWidth: 0,
            screenHeight: 0,
            totalPage: 0,
            currentPage: 0,
            isScrolling: false,
          };
        },
        mounted() {
          this.screenWidth = this.$refs.container.clientWidth;
          this.screenHeight = this.$refs.container.clientHeight;
          this.totalPage = this.$refs.container.children.length;

          window.addEventListener('resize', () => {
            this.screenWidth = this.$refs.container.clientWidth;
            this.screenHeight = this.$refs.container.clientHeight;
            this.scrollFn(this.currentPage)
          })
        },

当屏幕尺寸变化时重新获取容器宽度(注意要使用clientWidth,clientHeight)

4,容器监听到滚轮变化时,首先判断其滚动方向,再控制当前页数,调取滚动函数(可传滚动速度,这里省略了)

          // 判断滚轮方向
          scrollDirection(e) {
            if (!this.isScrolling) {
              if (e.deltaY > 0) {
                //滚轮下滑
                this.currentPage++;
                if (this.currentPage > this.totalPage - 1) {
                  return (this.currentPage = this.totalPage - 1);
                }
                this.scrollFn(this.currentPage);
              } else {
                // 滚轮上滑
                this.currentPage--;
                if (this.currentPage < 0) {
                  return (this.currentPage = 0);
                }
                this.scrollFn(this.currentPage);
              }
            }
          },
          // 滚动控制
          scrollFn(page, speed = 0.5) {
            let pages = document.querySelectorAll(".page");
            let that = this;
            pages.forEach(item => {
              item.style.transition = `all ${speed}s ease`;
              item.style.top = -that.screenHeight * page + "px";
            });
            this.isScrolling = true;
            setTimeout(() => {
              this.isScrolling = false;
            },speed*1000)
          },

isScrolling相当于节流,不至于出现连续滚动。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值