衛生署,想通了
首先,我通過獲取可視區域
var viewableTop = $("#parentDiv").scrollTop;
var viewableBottom = $("#parentDiv").innerHeight() + $("#parentDiv").scrollTop;
因此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()
然後
//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
謝謝,是我最終想出來的我自己,做眼前這個。我張貼我自己的答案哈哈。我原本試過這個,但它沒有工作......然後在嘗試其他事情一個小時後,我回到它並讓一位同事看着它...... 他只是指出「scrollTop」是沒有拼寫「srollTop」哈哈哦,簡單的錯誤....然後,我想我的頭撞在我的辦公桌上,這樣的事情很瑣碎 – BinarySolo00100 2010-01-12 21:59:52
兩組眼睛(http://c2.com/cgi/wiki?TwoSetsOfEyes)! – 2010-01-12 22:14:09