2013-07-08 28 views
0

因此,当作为参数给出的元素完全位于视口/窗口中时,下面的代码返回true。如果元素的0%以上位于视口/窗口中,则返回true

如何将其更改为在元素的任何位或任何超过0%的元素位于视口中时返回true?

function isElementInViewport(el){ 
     var rect = el.getBoundingClientRect(); 
     return(
      rect.top >= 0 && 
      rect.left >= 0 && 
      rect.bottom <= (window.innerHeight || document. documentElement.clientHeight) && 
      rect.right <= (window.innerWidth || document. documentElement.clientWidth) 
      ); 
} 

回答

1

交换topbottom,并交换leftright

function isElementInViewport(el){ 
    var rect = el.getBoundingClientRect(); 
    return rect.bottom >= 0 && 
     rect.right >= 0 && 
     rect.top <= (window.innerHeight || document. documentElement.clientHeight) && 
     rect.left <= (window.innerWidth || document. documentElement.clientWidth); 
} 

演示在这里:jsfiddle.net/w7ApB

相关问题