首先看一下横向滚动条

我说一下完成这个效果我所用到的思路

  1. 使用某一位置 居左的距离 / 总div距离 * 滚动条宽度 = 滚动条居左的距离(错误)
    上面说法错误是因为当时误判断了滚动条滚动的距离不是元素的offsetLeft
  2. 通过元素的offsetLeft直接赋值给滚动条的ScrollLeft, 发现已经实现了定位效果,但是并不是居中,是顶到最左侧的,为了居中我们就需要一点点骚操作

先看HTML的绑定详情:

<div class="tasks" ref="tasks" style="overflow: hidden; overflow-x: auto; height: 780px"  id="crossScroll">
    <div>
      <task-tab
        ref="tab"
        :hasTaskPower="hasTaskPower"
        :pzmbis="pzmbis"
        @tabClick="tabClick"
        @setTemplate="setTemplate"
        @taskEexport="taskEexport"
        @importTaskTemplate="importTaskTemplate"
        @boardList="boardList">
      </task-tab>
      <div
        class="tasksContent clearfix;"
        style="margin-top: 21px; white-space: nowrap; text-align: left"
      >

        <p style="text-align: left; padding-left: 6px; margin-top: 
        ...

ref为task的就是我们要操作的容器, 绑定好id和ref
绑定事件

const scroll = document.getElementById('crossScroll');
scroll.addEventListener('scroll', debounce(this.handleScroll, 500, { leading: true }), true);

计算距离

// 等差数列求居中, 页面中每多容纳一个元素就要减少0.5个元素
    // 三个参数分别为: 初始值,个数, 以及差值
    arithmeticSequence(init, num, change) {
      return init + (num - 1) * change;
    },
    // 定位到任务
    scrollToTask() {
      const location = 5;
      // 页面固定每一个元素marginLeft为36
      const marginLeft = 36;
      const item = this.$refs.kanbanItem[location].clientWidth + 36;
      const itemLeft = this.$refs.kanbanItem[location].offsetLeft;
      const windowWidth = this.$refs.tasks.clientWidth;
      const showNum = windowWidth / item;
      // 窗口大小小于等于元素时可以不用处理居中,直接使用距左距离即可
      const scrollLeft = windowWidth <= item ? itemLeft : itemLeft
      - this.arithmeticSequence(0, showNum, 0.5) * item - marginLeft / 2;
      this.$refs.tasks.scrollLeft = scrollLeft;
    },

实现的效果:

jquery 横向滚动 指定位置 js横向滚动条_jquery 横向滚动 指定位置


已经很完美的剧中啦

垂直滚动条定位

  1. 本文中一共两种情况, 一种是只有一个window的滚动条,另一种是不仅window滚动条要定位,table中还有滚动条要定位,也是要求居中

这里我直接写最麻烦的两个滚动条联动定位居中的代码

jquery 横向滚动 指定位置 js横向滚动条_html5_02


由图可见,rable中一个滚动条,window一个滚动条,要做居中可谓是比较麻烦了

html:

绑定了两个标签,一个是父级的div, 算距离页面顶部距离用的, 另一个是table算table距离div顶部的距离就没了, 就是两个工具属性,因为为了精确定位需要把这些元素带上

js处理:
我写了很多的注释,如果还有看不懂的可以评论区留言

// 定位上次任务位置
    lastTask() {
      // 每个元素固定高度
      const itemHeight = 47;
      // 表头高度
      const tableHeader = 59;
      // tab标签高度
      const tabHeight = 40;
      // table 距离父标签的高度
      const tableOffsetHeight = this.$refs.multipleTable.$el.offsetTop;
      // 包含table和tab的父标签mytask距离页面顶部的距离,不包括页面顶部固定栏
      const myTaskOffsetTop = this.$refs.myTask.offsetTop;
      // 浏览器内部高度
      const { innerHeight } = window;
      // 这里只用知道他是第27个元素就可以了
      const response = {
        location: 27,
        pageNum: 3,
      };
      const { location } = response;
      // 到第一个table中元素的高度
      const topToTabHeight = myTaskOffsetTop + tabHeight + tableOffsetHeight;
      const tableBodyHeight = this.tableHeight - tableHeader;
      // table能显示的数量
      const showNum = tableBodyHeight / itemHeight;
      // 元素距离父容器的高度, 居中+半个item高度
      const itemOffsetTop = itemHeight * (location + 0.5);
      // 计算table滚动条应定位的高度
      const tableScrollTop = itemOffsetTop > tableBodyHeight / 2
        ? itemOffsetTop - tableBodyHeight / 2
        : 0;
      // 浏览器滚动高度 = 默认滚动到table头部 +
      // (只要窗口大小小于table高度,那么就位移 (table总高度 - 窗口高度) 除以 2 后 再位移表头的距离即居中)
      let windowScrollTop = innerHeight > this.tableHeight
        ? topToTabHeight : topToTabHeight + (this.tableHeight - innerHeight) / 2 + tableHeader;
      if (this.total - location < showNum / 2) {
        // 多加0.5数据为了准确的居中
        windowScrollTop += ((showNum / 2) - (this.total - location) + 0.5) * itemHeight;
      }
      if (location < showNum / 2) {
        // 多减0.5数据为了准确的居中
        windowScrollTop -= ((showNum / 2) - location - 0.5) * itemHeight;
      }
      this.taskLocation = location;
      // 定位table body滚动条的高度
      this.$refs.multipleTable.bodyWrapper.scrollTop = tableScrollTop;
      // 浏览器滚动条定位
      // 因为这里父级是一个父组件,需要使用eventbus传值
      this.$bus.$emit('scrollLocation', windowScrollTop);
    },

实现效果如图:
图之后再补,项目目前没法启动

就是无论窗口被压得多小都可以准确定位到居中位置