<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
p{
height: 2000px;
}
#box{
height: 300px;
background-color: hotpink;
}
</style>
</head>
<body>
<p></p>
<div id="box">
<div id="pox"></div>
</div>
<p></p>
<script src="jquery-1.11.3.js"></script>
<script>
$.fn.myLazyload=function (callback) {
function scroll() {
if(window.pageXOffset!=undefined){
return{
left:window.pageXOffset,
top:window.pageYOffset
}
}else{
return{
left:document.documentElement.scrollLeft,
top:document.documentElement.scrollTop
}
}
}
function getClient() {
if(window.innerWidth!=undefined){
return{
width:window.innerWidth,
height:window.innerHeight
}
}else{
return{
width:document.documentElement.clientWidth,
height:document.documentElement.clientHeight
}
}
}
var _this=this;
var boo=true;
function myevent() {
var min=_this.offset().top-getClient().height;
var max=_this.offset().top+_this[0].scrollHeight;
if(scroll().top>min && scroll().top<max){
if(typeof callback=="function" && boo){
callback();
boo=false
}
}
}
window.addEventListener("scroll",myevent);
window.addEventListener("load",myevent);
}
$("#box").myLazyload(function () {
console.log("调用ajax");
})
</script>
</body>
</html>