admin管理员组文章数量:1417464
I currently have a a scrollable div that is populated dynamically.
I have a function that captures UpArrow and DownArrow keyPresses and changes the classes within the parent div to have one child selected at a time
(basically this mimics a select input)
Here is what I want to do: I need the div's viewable area to change (go down or up) to show the most recently "selected" child, but only if that child is not already in the viewable area of the parent.
So I need to obtain the viewable area in relation to the scrollHeight of the parent div somehow...but i am not sure how to do this...
Also, I am not sure how to set the viewable area of the parent div.
Any help would be greatly appreciated!
I currently have a a scrollable div that is populated dynamically.
I have a function that captures UpArrow and DownArrow keyPresses and changes the classes within the parent div to have one child selected at a time
(basically this mimics a select input)
Here is what I want to do: I need the div's viewable area to change (go down or up) to show the most recently "selected" child, but only if that child is not already in the viewable area of the parent.
So I need to obtain the viewable area in relation to the scrollHeight of the parent div somehow...but i am not sure how to do this...
Also, I am not sure how to set the viewable area of the parent div.
Any help would be greatly appreciated!
Share Improve this question asked Jan 12, 2010 at 20:54 BinarySolo00100BinarySolo00100 1,0253 gold badges12 silver badges18 bronze badges3 Answers
Reset to default 2Doh, figured it out
First I get the viewable area via
var viewableTop = $("#parentDiv").scrollTop;
var viewableBottom = $("#parentDiv").innerHeight() + $("#parentDiv").scrollTop;
so anything between the viewableTop and viewableBottom is viewable. But really you don't need to know that. Instead you need to know the following
//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()
then
//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
If you are using jQuery, you can get the position of an element relative to it's positioned parent by using position()
. The scrollable div can be set to relative/absolute positioning to make it positioned.
Also, you can change scrollTop property to change the scroll position. Or jquery scrollTop(pos)
.
you need to get the inner div's scrollTop, add outter div's height to that and that will give you the viewable dimensions
本文标签: javascriptjs or Jqueryobtaining viewable area of scrollable divStack Overflow
版权声明:本文标题:javascript - js or Jquery - obtaining viewable area of scrollable div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745263639a2650471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论