確定HTML元素是否在視口中的最有效方法?

[英]Most efficient way to determine if an HTML element is in the viewport?


I'm currently using prototype, with some code like:

我目前正在使用原型,其代碼如下:

function in_viewport(foo) {
  var offset = foo.viewportOffset().top;
  var viewportHeight = document.viewport.getHeight();
  if (offset > (0 - foo.getHeight()) && offset < viewportHeight) {
    return true;
  } else {
    return false;
  }
}

Is there a faster/easier/more-efficient/better way of doing this?

有沒有更快/更容易/更有效/更好的方法來做到這一點?

1 个解决方案

#1


With various JavaScript libraries like Dojo you can get pretty specific and cross browser compatible code that you can use to do this. It still wont look much prettier than what you have there. If you're doing it from scratch it gets much more complex as you have browser quirks, browser version quirks, have to deal with scrolling, etc.

使用Dojo等各種JavaScript庫,您可以獲得非常具體的跨瀏覽器兼容代碼,您可以使用它們來執行此操作。它仍然看起來不像你在那里漂亮。如果你是從頭開始做它會變得更加復雜,因為你有瀏覽器怪癖,瀏覽器版本怪癖,必須處理滾動等。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/04/14/f06df10dca53006bcce5eefd9aff8b38.html



 
  © 2014-2022 ITdaan.com 联系我们: