scrollIntoView滚动太远 [英] scrollIntoView Scrolls just too far

查看:689
本文介绍了scrollIntoView滚动太远的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中从数据库动态生成一个滚动条,其中包含表格行,其中包含div。每个表格行都像一个链接,就像您在视频播放器旁边的YouTube播放列表中看到的一样。

I have a page where a scroll bar containing table rows with divs in them is dynamically generated from the database. Each table row acts like a link, sort of like you'd see on a YouTube playlist next to the video player.

当用户访问页面时,他们会选择上应该转到滚动div的顶部。此功能正在运行。问题是它走得太远了。就像它们启用的选项一样,大约过高10px。因此,访问了该页面,该URL用于标识选择了哪个选项,然后将该选项滚动到滚动div的顶部。注意:这不是窗口的滚动条,它是带有滚动条的div。

When a user visits the page, the option they are on is supposed to go to the top of the scrolling div. This functionality is working. The issue is that it goes just a tad too far. Like the option they are on is about 10px too high. So, the page is visited, the url is used to identify which option was selected and then scrolls that option to the top of the scrolling div. Note: This is not the scroll bar for the window, it is a div with a scrollbar.

我正在使用此代码使它将选定的选项移到顶部div的值:

I am using this code to make it move the selected option to the top of the div:

var pathArray = window.location.pathname.split( '/' );

var el = document.getElementById(pathArray[5]);

el.scrollIntoView(true);

它将其移至div的顶部,但距离顶部约10像素。
任何人都知道如何解决该问题吗?

It moves it to the top of the div but about 10 pixels too far up. Anyone know how to fix that?

推荐答案

如果约为10px,那么我想您可以手动调整包含 div 的滚动偏移量,例如:

If it's about 10px, then I guess you could simply manually adjust the containing div's scroll offset like that:

el.scrollIntoView(true);
document.getElementById("containingDiv").scrollTop -= 10;

这篇关于scrollIntoView滚动太远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆