需求场景:
当页面滚动到指定位置时,底部按钮出现并固定到底部。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
body {
position: relative;
}
.fs {
position: sticky;
display: none;
bottom: 0;
left: 0;
right: 0;
height: 60px;
background-color: #f90;
}
</style>
</head>
<body>
<div style="height: 2000px"></div>
<div class="fs"></div>
<div style="height: 500px"></div>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
function shImgFS() {
var winTop =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop;
if (winTop > 600) {
$('.fs').show();
} else {
$('.fs').hide();
}
}
window.addEventListener('scroll', function (event) {
shImgFS();
});
if ('ontouchstart' in window) {
document.body.addEventListener('touchmove', function (event) {
event.preventDefault();
shImgFS();
});
}
</script>
</body>
</html>