【发布时间】:2018-10-16 01:55:15
【问题描述】:
我在可导航表的顶部有一个搜索栏,我希望它的位置是固定的,以便在滚动时仍然可见。该表将自身置于搜索栏上。我在表格导航脚本中使用 scrollIntoView() 将视图移动到选定的行。这是一个出现问题的 jsfiddle:https://jsfiddle.net/5umztjty/
#screen {
position: relative;
z-index: 110;
width: 255px;
height: 140px;
overflow-y: hidden;
}
#pokemons-list {
padding-top: 3px;
border-spacing: 0px;
align-content: center;
position: absolute;
width: 100%;
}
#mySearch {
position: fixed;
}
<!-- begin snippet: js hide: false console: true babel: false -->
我也试过这个修复Using scrollIntoView with a fixed position header
但它仍然无法正常工作。在这种情况下,所选元素是表格行,所以我有:
var rows = document.getElementById("myTable").children[1].children;
var selectedRow
var yourHeight = '20px';
所以我写了
// scroll to your element
rows[selectedRow].scrollIntoView(true);
// now account for fixed header
var scrolledY = window.scrollY; if(scrolledY) {
window.scroll(0, scrolledY - yourHeight);
}
我没有放导航脚本但是效果很好(一开始var selectedRow 的值为0)
【问题讨论】:
标签: javascript html css