移动浏览器上的标头 [英] Sticky header on mobile browser

查看:43
本文介绍了移动浏览器上的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在移动浏览器上粘贴标头.我目前正在使用此jquery脚本:

I need a sticky header on a mobile browser. I'm currently using this jquery script:

<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.在台式机浏览器上可以很好地工作,但在移动浏览器上却不能.

The header has an id of persist-wrap. This works fine on a desktop browser but not at all on a mobile browser.

有什么想法吗?

推荐答案

我对您的示例进行了一些调整,它在运行ICS的Samsung Galaxy S2和运行iOS 6.01的iPad 3上适用于我:其他人可以进行测试).

I've tweaked your example a little and it works for me on the Samsung Galaxy S2 running ICS and iPad 3 running iOS 6.01: (don't have any others to hand to test it on).

更改列表

  • 我缓存了多次使用的所有对象.
  • 对其进行了更改,使其具有可重用的功能,可以接受对象的参数,因此可以多次应用(可以进行调整以一次接受多个对象).

这里是例子:

$(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>

这篇关于移动浏览器上的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆