找到一个可滚动div内的顶部和最后一个元素 [英] find top and last element inside a scrollable div
本文介绍了找到一个可滚动div内的顶部和最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个固定高度的可滚动div。滚动时,我需要获取可见视口顶部的元素和视口底部的元素。我可以使用scrollTop获得滚动条的位置,但是我不确定如何获取元素。
更新:为了清晰起见,带格式的问题。
解决方案
获取您div的滚动位置
var scrollY = document.getElementById(yourDiv)。scrollTop;
循环内部元素并查看它们的位置
var divs = document.getElementById(yourDiv)。getElementsByTagName(div);
alert(divs [0] .offsetTop)
找出哪一个在scrollTop位置,另一个位于scrollTop + height position
I have a scrollable div with a fixed height. While scrolling I need to get the element at the top of the visible viewport and the one at the bottom of the viewport. I can get the position of the scrollbar using scrollTop, however I am not sure how to get the element.
Update: Formatted question for clarity.
解决方案
Get your div's scroll position
var scrollY = document.getElementById("yourDiv").scrollTop;
Loop through the elements inside and look at their position
var divs = document.getElementById("yourDiv").getElementsByTagName("div");
alert(divs[0].offsetTop)
Figure out which one is at the scrollTop position and the other one is at scrollTop + height position
这篇关于找到一个可滚动div内的顶部和最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文