js滑动加载

导读:本篇文章讲解 js滑动加载,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1、普通JS方法

//滑动

function getScrollTop()

{

 var scrollTop = 0;

 if (document.documentElement && document.documentElement.scrollTop) {

   scrollTop = document.documentElement.scrollTop;

  }else if (document.body) {

  scrollTop = document.body.scrollTop;

  }

  return scrollTop;

}

//获取当前可视范围的高度

function getClientHeight()

{

 var clientHeight = 0;

 if (document.body.clientHeight && document.documentElement.clientHeight) {

   clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);

 }else {

   clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);

 }

   return clientHeight;

}

//获取文档完整的高度

function getScrollHeight()

{

   return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);

}

//绑定事件

window.onscroll = function ()

{

 if (getScrollTop() + getClientHeight() == getScrollHeight()) {

  //dosomething

 }

}

2、jquery方法

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>jQuery实现当拉动滚动条到底部加载数据</title>
<style type="text/css">
*{margin:0;pa动态加载数据ing:0;}
body{font-size:14px;color:#444;font-family:"微软雅黑",Arial;background:#fff;}
a{color:#444;text-decoration: none;}
a:hover{color:#065BC2;text-decoration: none;}
.clear{clear:both;}
img{border:none;vertical-align: mi动态加载数据le;}
ul{list-style: none;}
</style>
</head>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
var totalheight = 0;     //定义一个总的高度变量
function loa动态加载数据ata()
{ 
    totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());     //浏览器的高度加上滚动条的高度 
 
    if ($(document).height() <= totalheight)     //当文档的高度小于或者等于总的高度的时候,开始动态加载数据
    { 
        //加载数据
        $("#container").append("我是新加载出来的数据<br/>");
    } 
} 
 
$(window).scroll( function() { 
    console.log("滚动条到顶部的垂直高度: "+$(document).scrollTop()); 
    console.log("页面的文档高度 :"+$(document).height());
    console.log('浏览器的高度:'+$(window).height());
    loa动态加载数据ata();
}); 
</script>
</head>
<body>
    <div id="container">
        动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>动态加载数据<br/>
    </div>
</body>
</html>

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/71285.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!