用于确定图像是在视口中还是部分在视口中的逻辑

时间:2012-09-06 11:35:04

标签: javascript logic

我正在运行一个流程,只有视口中可见或部分可见的图像才相关。以下代码有效,如果img的任何部分在屏幕上,则返回true。但有没有更简洁的方式表达相同的逻辑?

//Can't figure out an easier way to do this!
    return (imgLeft>=winData.l && imgLeft<winData.r && imgTop>=winData.t && imgTop<winData.b) || //TL somewhere on screen
        (imgRight>=winData.l && imgRight<winData.r && imgTop>=winData.t && imgTop<winData.b) || //TR somewhere on screen
        (imgLeft>=winData.l && imgLeft<winData.r && imgBottom>=winData.t && imgBottom<winData.b) || //BL somewhere on screen
        (imgRight>=winData.l && imgRight<winData.r && imgBottom>=winData.t && imgBottom<winData.b) || //BR somewhere on screen
        (imgLeft<winData.l && imgRight>winData.r && imgTop>=winData.t && imgTop<winData.b) || //L offscreen L and R offscreen R, top on screen
        (imgLeft<winData.l && imgRight>winData.r && imgBottom>=winData.t && imgBottom<winData.b) || //L offscreen L and R offscreen R, bottom on screen
        (imgTop<winData.t && imgBottom>winData.b && imgLeft>=winData.l && imgLeft<winData.r) || //T offscreen T and B offscreen B, left on screen
        (imgTop<winData.t && imgBottom>winData.b && imgRight>=winData.l && imgRight<winData.r) || //T offscreen T and B offscreen B, right on screen
        (imgLeft<winData.l && imgRight>winData.r && imgTop<winData.t && imgBottom>winData.b) //All sides offscreen

1 个答案:

答案 0 :(得分:0)

好吧,我做了一顿那样的事......我觉得那天还有太多想法:)

return imgLeft < winRight &&
       imgRight > winLeft &&
       imgTop < winBottom &&
       imgBottom > winTop;