私は、特定の子要素をその親内のビューにスクロールする関数を作成しました。これは次のようになります:jQueryのアドバイス:要素をスクロールするためにこの機能を改善するにはどうすればよいですか?
function keepScrolledOver(elem)
{
frame = elem.parent();
var scrollPos = frame.scrollTop();
var offset = elem.attr("offsetTop");
// If the element is scrolled too high...
if(offset < scrollPos)
{
frame.scrollTop(offset);
// frame.attr("scrollTop", offset);
}
// If the element is scrolled too low...
else
{
var frameHeight = frame.height();
var offsetBottom = offset + elem.height();
var scrollBottom = scrollPos + frameHeight;
if(offsetBottom > scrollBottom)
{
// frame.attr("scrollTop", offsetBottom);
if(frameHeight < offsetBottom)
frame.scrollTop(offsetBottom - frameHeight);
// frame.attr("scrollTop", offsetBottom - frameHeight);
}
}
}
これまでのところ、私のFirefoxウェブアプリ(Firefoxは私がこれまでにそれをテストしたすべてで、私が意味する)、これは素晴らしい作品のために。唯一の問題点は、スクロールした要素が低すぎると、対象要素の最後までスクロールする傾向があることです。要素の詰め物と何か関係があるかどうか、私の数学がちょうど吸うかどうかは分かりません。
誰でもこれを改善するための素晴らしいアイデアはありますか?