Try
var viewed = Array.prototype.map.call(document.querySelectorAll("body *")
, function (el, i) {
return (el.getBoundingClientRect().bottom <= window.innerHeight
&& el.getBoundingClientRect().left <= window.innerWidth)
&& $(el).addClass("already-viewed") && el
}).filter(Boolean);
$(document).ready(function () {
var body = $("body");
$.each(new Array(180), function () {
body.append(
$("<img>"))
});
var viewed = Array.prototype.map.call(document.querySelectorAll("body *"), function (el, i) {
return (el.getBoundingClientRect().bottom <= window.innerHeight
&& el.getBoundingClientRect().left <= window.innerWidth)
&& $(el).addClass("already-viewed") && el
}).filter(Boolean);
body
.append("total images: " + $("img").length
+ ", already viewed: " + $(".already-viewed").length);
console.log(viewed.length, $(viewed))
});
body {
width : 1000px;
height : 1000px;
}
img {
width : 50px;
height : 50px;
background : navy;
}
.already-viewed {
outline:0.15em solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…