控制鼠标滚动,滚动指定的距离

控制鼠标滚轮滚动指定的距离

滚动整屏内容

效果案例

鼠标滚轮事件控制需要滚动区域的 top ,滚轮每滚动一次,top移动的值是屏幕的高度,设置body的overflow:hidden
在这里插入图片描述

<template>
  <div id="fome">
    <!-- banner区域 -->
    <nav class="banner">
      <video
        class="nav-video"
        src="@/assets/video/earth.mp4"
        autoplay
        loop
        muted
      ></video>
      <!-- 文字 -->
      <div class="ban-word">
        <img src="@/assets/images/banner-word.png" alt="" />
      </div>
    </nav>
    <!-- 服务平台 -->
    <section class="pro-plat">
      <h1 class="title">便捷的端与边缘AI服务平台</h1>
      <section class="pro-con">
        <article class="pro-son1">
          <div class="son-title1">支持多来源模型</div>
          <div class="son-con son-con1">
            <div class="son1-top">
              <div class="son1-left">
                <p class="left-p1">上传本地模型</p>
                <p>支持多种深度学习框架和网络模型</p>
              </div>
              <div class="son1-right"></div>
            </div>
            <div class="son1-top">
              <div class="son1-left">
                <p class="left-p1">使用开源模型</p>
                <p>直接扫描体验Github丰富的开源模型</p>
              </div>
              <div class="son1-right"></div>
            </div>
          </div>
        </article>
        <article class="pro-son2">
          <div class="son-title1">零门槛AI芯片</div>
          <div class="son-con"></div>
        </article>
        <article class="pro-son3">
          <div class="son-title1">本地部署</div>
          <div class="son-con"></div>
        </article>
      </section>
    </section>
    <!-- 核心优势 -->
    <section style="height:100%;background:pink;"></section>
    <!-- 底部 -->
    <section style="height:220px;background:black;"></section>
  </div>
</template>

<script>
export default {
  name: "",
  mounted() {
    var x = 0;
    function debounce(handler, delay) {
      var timer;

      return function() {
        var self = this,
          arg = arguments;

        clearTimeout(timer);

        timer = setTimeout(function() {
          handler.apply(self, arg);
        }, delay);
      };
    }
    // 处理函数
    // function handle() {
    //   console.log(Math.random());
    // }
    // 滚动事件
    // window.addEventListener("scroll", debounce(handle, 1000));

    var scrollFunc = function(e) {
      e = e || window.event;
      // console.log("e", e);
      var fome = document.getElementById("fome");
      fome.style.height = document.body.clientHeight + "px";
      var changeHeight = document.body.clientHeight;
      console.log("changeHeight", changeHeight);
      if (e.wheelDelta) {
        if (e.wheelDelta > 0) {
          if (x <= -changeHeight * 2 - 1) {
            x = -changeHeight * 2;

            fome.style.top = `${-changeHeight * 2 + "px"}`;
          } else if (-changeHeight > x && x >= -changeHeight * 2) {
            console.log("x------", x);
            x = -changeHeight;
            fome.style.top = `${-changeHeight + "px"}`;
          } else if (x >= -changeHeight) {
            x = 0;
            fome.style.top = "0px";
            return;
          }

          console.log("fome", fome);

          fome.style.top += `${x + "px"}`;
        }
        if (e.wheelDelta < 0) {
          // 正确的指定距离的
          // if (x <= -2000) {
          //   x = -2234;
          //   fome.style.top = " -2234px";
          //   return;
          // } else {
          //   x += -1007;
          // }

          if (x <= -changeHeight * 2 + 200) {
            x = -changeHeight * 2 - 200;
            fome.style.top = `${-changeHeight * 2 - 200 + "px"}`;
            // fome.style.top = " -2234px";
            return;
          } else {
            x += -changeHeight;
          }
          console.log("x往下滚动", x);
          fome.style.top = `${x + "px"}`;
          //当滑轮向下滚动时
          console.log("下滚", document.body.clientHeight);
        }
      } else if (e.detail) {
        //Firefox滑轮事件
        if (e.detail > 0) {
          //当滑轮向下滚动时
          console.log("下滚");
        }
        if (e.detail < 0) {
          //当滑轮向上滚动时
          console.log("上滚");
        }
      }
    };
    /*IE、Opera注册事件*/
    if (document.attachEvent) {
      document.attachEvent("onmousewheel", scrollFunc);
    }
    //Firefox使用addEventListener添加n滚轮事件
    if (document.addEventListener) {
      //firefox
      document.addEventListener("DOMMouseScroll", scrollFunc, false);
    }
    //Safari与Chrome属于同一类型
    // window.onmousewheel = document.onmousewheel = scrollFunc;
    window.addEventListener("mousewheel", debounce(scrollFunc, 200), false);

    /*
    event.wheelDelta 滚动方向
    上:120
    下:-120
    Firefox:event.detail 滚动方向
    上:-3
    下:3
    */
  },
};
</script>

<style scoped>
#fome {
  position: relative;
  top: 0;
  transition: all 0.5s;
}
.banner {
  position: relative;
  /* width: 100%; */
  height: 100vh;
  overflow: hidden;
}
.nav-video {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* height: 100%; */
  /* width: 100%; */
  /* height: 100%; */
  /* height: 100vh; */
  /* height: 760px; */
  object-fit: fill;
}
.ban-word {
  position: absolute;
  top: 40%;
  left: 40%;
  transform: translate(-50%, -50%);
  z-index: 99999;
}
/* 产品与服务 */
.pro-plat {
  /* padding-top: 136px; */
  box-sizing: border-box;
  /* height: 735px; */
  height: 100vh;
}
.pro-plat .title {
  height: 100px;
  line-height: 100px;
  text-align: center;
  font-size: 40px;
  /* margin-bottom: 60px; */
}
.pro-plat .pro-con {
  display: flex;
  justify-content: space-between;
  /* height: 504px; */
}
.pro-son1,
.pro-son3 {
  width: 25%;
}
.pro-son2 {
  width: 40%;
}
.son-title1 {
  background: #d35201;
  color: #fff;
  height: 70px;
  line-height: 70px;
  text-align: center;
  font-size: 28px;
  margin-bottom: 38px;
}
.son-con {
  height: 100%;
  padding: 36px 26px;
  box-sizing: border-box;
  background: #f4f4f4;
}
.son-con1 {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.son1-top {
  width: 100%;
  height: 132px;
  background: #fff;
}
.son-con1 .son1-left {
  width: 170px;
  font-size: 20px;
  margin-left: 30px;
}
.left-p1 {
  height: 60px;
  line-height: 60px;
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
支持移动端html5 可以控制滚动条样式,方便实用。 /* 1、showArrows:是否显示滚动箭头,模式是false; 2、maintainPosition:当滚动区重新初始化后,是否保持滚动条的原有位置,默认是true; 3、stickToBottom:当maintainPosition设置为true,且滚动区域到达底部,当有新内容添加的时候任然会固定在滚动区域底部,默认是false; 4、stickToRight:与stickToBottom属性原理类似,只是方向是右侧而不是底部; 5、autoReinitialise:自动初始化滚动区,内部实现机制实际上是一个定时器,当检测到内部有内容新增时,重新初始化,由于性能原因,默认false; 6、autoReinitialiseDelay:当autoReinitialise设置为true时,该属性表示自动初始化的延时,默认是500ms; 7、verticalDragMinHeight:垂直可拖动的最小高度,默认是0; 8、verticalDragMaxHeight:垂直可拖动的最大高度,默认是99999; 9、horizontalDragMinWidth:水平可拖动的最小距离,默认是0; 10、horizontalDragMaxWidth:水平可拖动的最大距离,默认是99999; 11、contentWidth:滚动区域的宽度,一般不要设置,该插件会根据内容实际宽度计算,默认undefined; 12、animateScroll:当调用scrollTo 或者scrollBy的时候,设置一个动画效果,包括时长duration和渐变ease,默认false; 13、animateDuration:动画时长,默认300ms; 14、animateEase:动画渐变函数,默认linear; 15、hijackInternalLinks:劫持锚链接,定位到滚动区域指定位置,默认false; 16、verticalGutter:垂直方向,内容和滚动条之间的距离,默认是4px; 17、horizontalGutter:水平方向,内容和滚动条之间的距离,默认是4px; 18、mouseWheelSpeed:鼠标滚轮的速度,默认是10px; 19、arrowButtonSpeed:方向按钮滚动内容的速度,默认是10px; 20、arrowRepeatFreq:按住方向按钮,内容滚动的频率,默认是100ms; 21、arrowScrollOnHover:当鼠标悬浮在方向按钮上时,是否允许滚动,默认false; 22、verticalArrowPositions:垂直方向按钮和固定点的位置,默认split; 23、horizontalArrowPositions:同上,水平方向; 24、enableKeyboardNavigation:是否允许键盘导航,默认true; 25、hideFocus:是否隐藏焦点框,默认false; 26、clickOnTrack:当点击固定点的时候,是否向相应方向滚动内容,默认true; 27、trackClickSpeed:点击固定点的滚动速度,默认是30px; 28、trackClickRepeatFreq:点击固定点的滚动频率,默认是100ms。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值