js中获取首屏的宽高和滚动距离监听
2017-04-18 本文已影响126人
SmallTwo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
#content{
width: 100%;
height: 2000px;
background: -webkit-gradient(linear,0 0, 0 1500,from(yellow),to(blue));
}
#btn {
display: none;
position: fixed;
right: 100px;
bottom: 100px;
}
</style>
</head>
<body>
<div id="content">
<input type="button" value="点击" id="btn">
</div>
<script>
var btn = document.getElementById('btn'),content = document.getElementById('content');
window.onscroll = function () {
// 获取窗口滚动的距离
var scrolDis = document.documentElement.scrollTop || document.body.scrollTop;
// 获取首屏窗口的高度
var firHei = document.documentElement.clientHeight || document.body.clientHeight;
btn.style.display = scrolDis > firHei ? 'block' : 'none';
};
</script>
</body>
</html>