中间滚动 左右两次固定布局+返回顶部的中往下滑动中断回顶部

DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Titletitle>
  head>
  <body>
    <div id="app">
      <div class="container">
        <div class="left-container">
          <div :class="['content',fixedLeft]">div>
        div>
        <div class="center-container">
          <div :class="['search_content',fixedSearch]">div>
          <div v-for="iem in 500">{{iem}}div>
        div>
        <div class="right-container">
          <div :class="['content',fixedRight]">div>
        div>
        <img
          ref="backTop"
          v-show="isTopShow"
          src="./static/icon/rect.svg"
          width="40"
          height="40"
          class="backTop"
          @click="scollTop"
        />
      div>
    div>
  body>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    body {
      background-color: #5b5f80;
      display: flex;
      justify-content: center;
    }

    .container {
      display: flex;
      justify-content: center;
      padding-top: 50px;
      position: relative;
      width: 1400px;
    }

    .left-container .content,
    .right-container .content {
      width: 300px;
      height: 100%;
      background-color: orange;
    }

    .fixed-left {
      position: fixed;
      top: 0;
      transform: translateX(-100%);
    }

    .fixed-right {
      position: fixed;
      top: 0;
    }

    .center-container {
      width: 800px;
      min-height: 100vh;
      background-color: white;
    }

    .center-container .search_content {
      height: 100px;
      width: 800px;
      background-color: #161829;
    }

    .fixed-search {
      position: fixed;
      top: 0;
      background-color: rgba(22, 24, 41, 0.9) !important;
    }

    .container .backTop {
      position: fixed;
      bottom: 40px;
      cursor: pointer;
    }
    /* 滚动条样式 */
    ::-webkit-scrollbar {
      width: 6px;
      background-color: transparent;
    }
    ::-webkit-scrollbar-thumb {
      background-color: rgba(255, 255, 255, 0.5);
      height: 50px;
      border-radius: 2px;
      /* outline-offset: -2px; */
      /* outline: 2px solid #fff; */
    }
  style>
  <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js">script>
  <script>
    var vm = new Vue({
      el: "#app",
      data() {
        return {
          fixedLeft: "",
          fixedRight: "",
          fixedSearch: "",
          isTopShow: false,
          isclick: true,
          top: 0, // 距离顶部的位置
          upRoll: null, // 控制是否回到顶部的定时器
        };
      },
      watch: {
        top: {
          handler(newValue, oldValue) {
            if (newValue > oldValue) {
              clearInterval(this.upRoll); // 回到顶部清除定时器
            }
          },
        },
      },
      mounted() {
        // 监听滚动的值
        window.addEventListener(
          "scroll",
          () => {
            this.top = document.documentElement.scrollTop;
          },
          true
        );
        window.onload = () => {
          document.body.onscroll = (e) => {
            if (window.scrollY >= 50) {
              this.fixedLeft = "fixed-left";
              this.fixedRight = "fixed-right";
              this.fixedSearch = "fixed-search";
              this.isTopShow = true;

              // 控制返回顶部按钮的定位 首先是盒子距离左侧的位置 + 1400 盒子的宽度 + 20 是你想在盒子左侧的距离
              this.$refs.backTop.style.left =
                document.querySelector(".container").offsetLeft +
                1400 +
                20 +
                "px";
            } else {
              this.fixedLeft = "";
              this.fixedRight = "";
              this.fixedSearch = "";
              this.isTopShow = false;
            }
          };
        };
      },
      methods: {
        // 回到顶部
        scollTop() {
          if (this.isclick) {
            this.isclick = false;
            this.upRoll = setInterval(() => {
              const top = document.documentElement.scrollTop; // 每次获取页面被卷去的部分
              const speed = Math.ceil(top / 10); // 每次滚动多少 (步长值)
              if (document.documentElement.scrollTop > 0) {
                document.documentElement.scrollTop -= speed; // 不在顶部 每次滚动到的位置
              } else {
                clearInterval(this.upRoll); // 回到顶部清除定时器
              }
            }, 20);
             setTimeout(() => {
             this.isclick = true;
           }, 1500);
          }
        },
      },
    });
  script>
html>

你可能感兴趣的:(css,前端,javascript)