我的代码就是这样,但是它在iPhone中确实可以正常运行,滚动时会波动。
此代码可在Android上运行,但是当我们尝试在iPhone上以Hybrid App形式运行时,它不起作用,粘性条会发生波动,滚动时会消失并停留在先前的位置,滚动完成后,它会变为现实位置。
<div class="header" id="myHeader">
/* Style the header */
.header {
padding: 10px 16px;
background: #555;
color: #f1f1f1;
}
/* Page content */
.content {
padding: 16px;
}
/* The sticky class is added to the header with JS when it reaches its scroll position */
.sticky {
position: fixed;
top: 0;
width: 100%
}
/* Add some top padding to the page content to prevent sudden quick movement (as the header gets a new position at the top of the page (position:fixed and top:0) */
.sticky + .content {
padding-top: 102px;
}
// When the user scrolls the page, execute myFunction
window.onscroll = function() {myFunction()};
// Get the header
var header = document.getElementById("myHeader");
// Get the offset position of the navbar
var sticky = header.offsetTop;
// Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
} else {
header.classList.remove("sticky");
}
}