在FB.Canvas API中有一些方法来获取滚动顶部,偏移顶部和客户端高度(通过getPageInfo())但我需要一种方法来找到最顶层的窗口滚动位置,以便我可以确定我需要的区域对客户可见的是可见的.用户将通过表格进行制表,并且比大多数屏幕更高.我需要确保表单项可见.
最佳答案
似乎getPageInfo().scrollTop属性和getPageInfo(..offsetTop一起)将为您提供顶部窗口滚动位置.
原文链接:/js/429502.html这是我用于在给定绝对/全局x和y坐标的情况下将元素滚动到视图中的最终代码.
function scrollElementIntoViewFB(applicationID,elementTop,elementBottom) {
var pageInfo = FB.Canvas.getPageInfo();
// fallback if running local
if (pageInfo.clientHeight==0) {
scrollElementIntoView(applicationID,elementBottom);
return;
}
var scrollPosition = pageInfo.scrollTop;
var viewportHeight = pageInfo.clientHeight;
var flashOffsetTop = pageInfo.offsetTop;
var elementAbsoluteTop = elementTop + flashOffsetTop;
var elementAbsoluteBottom = elementBottom + flashOffsetTop;
var visibleBottomPosition = viewportHeight + scrollPosition;
if (scrollPosition>elementAbsoluteTop) {
FB.Canvas.scrollTo(0,elementAbsoluteTop);
}
else if (visibleBottomPosition
如果由于某种原因你想要HTML唯一的版本(如果在像Facebook这样的另一个域内的iframe但是可以在你自己的网站上工作则不会起作用):
function scrollElementIntoView(applicationID,elementBottom) {
var scrollPosition = f_scrollTop();
var viewportHeight = f_clientHeight();
var flashElement = swfobject.getObjectById(applicationID);
var flashOffsetTop = flashElement.offsetTop; // not sure if this is cross browser
var elementAbsoluteTop = elementTop + flashOffsetTop;
var elementAbsoluteBottom = elementBottom + flashOffsetTop;
var visibleBottomPosition = viewportHeight + scrollPosition;
if (scrollPosition>elementAbsoluteTop) {
window.scrollTo(0,elementAbsoluteTop);
}
else if (visibleBottomPosition