JS垂直以及横向滚动条精确定位居中某一元素

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;

},

实现的效果:

在这里插入图片描述

已经很完美的剧中啦

垂直滚动条定位

====================================================================

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

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

在这里插入图片描述

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

html:

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

js处理:

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

// 定位上次任务位置

lastTask() {

// 每个元素固定高度

const itemHeight = 47;

// 表头高度

const tableHeader = 59;

// tab标签高度

const tabHeight = 40;

// table 距离父标签的高度

const tableOffsetHeight = this. r e f s . m u l t i p l e T a b l e . refs.multipleTable. 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;

}

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值