此功能可以在iPhone中使用,

$(document).ready(function() {
    $('#head').css('position','fixed');
  window.onscroll = function() {
      document.getElementById('head').style.top =
         (window.pageYOffset + window.innerHeight + 25) + 'px';
        // alert((window.pageYOffset + window.innerHeight - 25) + 'px');
    };
});


但是可以将div(25px)保持在页面底部,无论滚动多少,我都需要在页面顶部

我像这样

$(document).ready(function() {
    $('#head').css('position','fixed');
    var height = $('#head').height();
  window.onscroll = function() {
      document.getElementById('head').style.top =
         (window.pageYOffset) - height + 'px';
        // alert(window.pageYOffset); alert(window.innerHeight);
    };
});


但似乎#head div没有正确跟随滚动(似乎弹跳),任何想法我都错过了吗?

最佳答案

位置fixed在iPhone中不起作用。因此,只要您滚动页面直到scroll处理程序设置其新位置,它肯定会反弹。

$(document).ready(function() {
    $('#head').css('position','absolute');
    $(window).scroll(function() {
      $('#head').css({
         top: window.pageYOffset
      });
    });
});

09-25 17:16
查看更多