Bin*_*100 5 javascript scroll custom-scrolling
我目前有一个动态填充的可滚动div.
我有一个函数捕获UpArrow和DownArrow keyPresses并更改父div中的类以一次选择一个子(基本上这模仿选择输入)
这就是我想要做的事情:我需要div的可视区域来更改(向下或向上)以显示最近"选定"的子项,但前提是该子项尚未位于父项的可查看区域中.
所以我需要以某种方式获得与父div的scrollHeight相关的可视区域......但我不知道该怎么做...
另外,我不知道如何设置父div的可视区域.
任何帮助将不胜感激!
呵呵,明白了
首先我通过以下方式获取可视区域
var viewableTop = $("#parentDiv").scrollTop;
var viewableBottom = $("#parentDiv").innerHeight() + $("#parentDiv").scrollTop;
Run Code Online (Sandbox Code Playgroud)
所以 viewableTop 和 viewableBottom 之间的任何内容都是可见的。但实际上你不需要知道这一点。相反,您需要了解以下内容
//getting child position within the parent
var childPos = $("#childDiv").position().top;
//getting difference between the childs top and parents viewable area
var yDiff = ($("#childDiv").position().top + $("#childDiv").outerHeight()) - $("#parentDiv").innerHeight()
Run Code Online (Sandbox Code Playgroud)
然后
//if upArrow and childPosition is above viewable area (that's why it goes negative)
if(event.keyCode == 38 && childPos < 0)
$("#parentDiv").scrollTop += childPos;//add the negative number to the scrollTop
//if downArrow and the difference between childs top and parents viewable area is greater than the height of a childDiv
else if(event.keyCode == 40 && yDiff > $("#childDiv").outerHeight()-1)
$("#parentDiv").scrollTop += yDiff;//add the difference to the parents scrollTop
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5502 次 |
| 最近记录: |