移动浏览器上的标题

时间:2012-09-24 22:00:47

标签: jquery css sticky

我需要在移动浏览器上使用粘贴标题。我目前正在使用这个jquery脚本:

<script>
  $(function(){
    var stickyHeader = $('#persist-wrap').offset().top;
    $(window).scroll(function(){
      if( $(window).scrollTop() > stickyHeader ) {
        $('#persist-wrap').addClass("sticky");
      } else {
        $('#persist-wrap').removeClass("sticky");
      }
    });
  });
</script>

使用此CSS代码:

.sticky {
  position: fixed;
  top: 0;
}

标头的id为persist-wrap。这在桌面浏览器上运行良好,但在移动浏览器上完全没有。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我稍微调整了你的例子,它适用于运行iOS 6.01的运行ICS和iPad 3的三星Galaxy S2 :(没有其他人可以试用它)。

更改列表

  • 我缓存了多次使用过的所有对象。
  • 将其更改为一个可重复使用的函数,接受一个对象的参数,以便可以多次应用(这可以调整为一次接受多个对象)。

以下是示例:

$(function(){
  
	createSticky(jQuery("#header"));

});

function createSticky(sticky) {
	
	if (typeof sticky !== "undefined") {

		var	pos = sticky.offset().top,
				win = jQuery(window);
			
		win.on("scroll", function() {
    		win.scrollTop() >= pos ? sticky.addClass("fixed") : sticky.removeClass("fixed");      
		});			
	}
}
#header {
	background: #666;
	padding: 3px;
  padding: 10px 20px;
  color: #fff;
}
#header.fixed {
	position: fixed;
	top: 0;
	width: 100%;
}


/* For aesthetics only */
				
body {
	margin: 0;
    height: 10000px;
	font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Some Text before the header</p>
<div id="header">
  Home
</div>

相关问题