Facebook Canvas 应用程序内的滚动事件
我希望使用图像的 LazyLoad 技术和无限滚动来将新内容加载到我的页面中。这两件事都利用了 Facebook 画布应用程序中不起作用的 $(window).scrollTop()
函数。我知道我可以使用 FB.Canvas.getPageInfo() 来获取当前的scrollTop值,但是我遇到了一些性能问题,我的代码如下:
var oldScroll = 0; // current scroll
var newScroll = null; // new scroll (fetched from FB.Canvas)
// Override the normal function to work within fb
// !!This seems to kill the browser
$.fn.scrollTop = function() {
return FB.Canvas.getPageInfo().scrollTop;
};
// poll to check for any changes in scroll
setInterval(function() {
newScroll = FB.Canvas.getPageInfo().scrollTop;
if(oldScroll != newScroll) { // have we scrolled at all?
oldScroll = newScroll;
$(window).trigger($.Event('scroll')); // fire a scroll event to keep the rest of the application that is listening
}
}, 1000);
如果我不这样做,似乎没问题不要覆盖scrollTop函数,但是一旦我这样做,我的浏览器很快就会崩溃。
我的做法完全错误吗?有人已经创建了一种在 FB 画布内执行此操作的方法吗?干杯。
I'm looking to use a LazyLoad technique for images and infinite scroll to load new content into my page. Both these things make use of the $(window).scrollTop()
function that inside a Facebook canvas application doesn't work. I know i can use FB.Canvas.getPageInfo()
to get the current scrollTop value however I'm encountering some performance issues with this, my code is as follows:
var oldScroll = 0; // current scroll
var newScroll = null; // new scroll (fetched from FB.Canvas)
// Override the normal function to work within fb
// !!This seems to kill the browser
$.fn.scrollTop = function() {
return FB.Canvas.getPageInfo().scrollTop;
};
// poll to check for any changes in scroll
setInterval(function() {
newScroll = FB.Canvas.getPageInfo().scrollTop;
if(oldScroll != newScroll) { // have we scrolled at all?
oldScroll = newScroll;
$(window).trigger($.Event('scroll')); // fire a scroll event to keep the rest of the application that is listening
}
}, 1000);
It appears to be fine if i don't override the scrollTop function but once I do my browser soon crashes.
Am i going about this completely wrong? Has someone already created a way to do this inside FB canvas? Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我试图解决同样的问题,我的解决方案是 Facebook 应用程序内的附加元素(div#xscroll,pos:abs,L)+ setInterval:
现在您可以使用这个 #xscroll 进行 LazyLoad:
问题是 - FB 时 CPU 使用率巨大.Canvas.getPageInfo,我的间隔是2000 - 并且CPU使用率很高。经过一小时的脚本工作 - Safari 速度变慢并且内存泄漏......
Im trying to solve the same problem and my solution is a additional element (div#xscroll, pos:abs, L) inside Facebook application + setInterval:
now you can use this #xscroll for LazyLoad:
the problem is - huge CPU usage while FB.Canvas.getPageInfo, my interval is 2000 - and CPU usage is hight. After hour of script work - Safari slows down and memoryleaked...
您是否坚持要重写
scrollTop()
函数?最初它适用于您通过 jQuery 选择器提供的任何元素,而您似乎将其限制为仅适用于 facebook 画布。如果任何其他 JavaScript 尝试使用scrollTop()
,它都会惨败,不是吗?至于解决方案,我已经以几乎相同的方式完成了无限滚动 -
setInterval()
来查看是否已到达底部,然后加载内容,然后再次检查滚动,依此类推。但我不会随时触发窗口的原始滚动事件,因为没有必要 - 至少在我的情况下是这样。Do you insist on having the
scrollTop()
function overridden? Originally it works for any element that you supply via the jQuery selector, while you seem to restrict it to only the facebook canvas. If any other javascript tries to usescrollTop()
, it will fail miserably, won't it?As for the solution, I've done infinite scrolling pretty much the same way -
setInterval()
to see if you've reached the bottom, then load content, then check scroll again, and so on. But I'm not triggering the original scroll event of the window at any time, as there is no need to - at least in my case.