经常在网页中看到这样一种效果,当页面滚动一段距离后,页面中的某个部分固定在一个区域(通常是头部导航),这种效果一般称为Sticky Header,如下图所示:

上述操作在Android系统中非常好实现,只需要监听onscroll事件并将实现逻辑写在里面即可,但iOS则不一样,它在页面scroll的时候,是不会执行代码的,直到页面停下来为止。

如此一来上面的效果就比较难实现了,因为不能实时更新导航条的位置,所以用户体验会与Android系统不一致。经过查阅资料,大概找了几种解决方案:

  1. 在iOS中同时监听onscroll与ontouchmove(然而尝试过后效果不好,特别是在屏幕上飞快地划一下然后把手指松开的时候);
  2. 自己模拟实现页面滚动,或者使用iScroll.js(感觉比较复杂没有试过);
  3. 使用postion:sticky属性(最终采用了这个,因为简单而且性能也好);

position:sticky是CSS3中新增的样式,它的表现相当于position:relativeposition:fixed的集合,当目标区域在屏幕中可见时,它的行为就像position:relative; 而当页面滚动超出目标区域时,它的表现就像position:fixed,它会固定在目标位置。比如:

.header{
position:-webkit-sticky;
position:sticky;
top:0
}

当header滚动出可视窗口外时,它就会执行position:fixed操作,并定位在top:0的位置。当header重新出现在视口内时,它执行position:relative操作,看起来就像元素仍然在原来的位置。

以上经过3GS真机测试并且测试通过。测试代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Sticky Header for iOS</title>
<style>
body{
margin: 0;
color: #ffffff;
}
.banner{
height: 100px;
background-color: #3b9a90;
}
.header{
height: 30px;
line-height: 30px;
background-color: #daac76;
position: -webkit-sticky;
position: sticky;
top:0;
}
.content{
background-color: #557b4d;
height: 900px;
padding: 10px;
line-height: 24px;
text-indent:2em;
}
</style>
</head>
<body>
<div class="banner">banner</div>
<div class="header">navigation</div>
<div class="content">A young man was getting ready to graduate from college. For many months he had admired a beautiful sports car in a dealer's showroom, and knowing his father could well afford it, he told him that was all he wanted. As Graduation Day approached, the young man awaited signs that his father had purchased the car. Finally, on the morning of his graduation, his father called him into his private study. His father told him how proud he was to have such a fine son, and told him how much he loved him. He handed his son a beautiful wrapped gift box. Curious, but somewhat disappointed, the young man opened the box and found a lovely, leather-bound Bible, with the young man's name embossed in gold. Angrily, he raised his voice to his father and said, "With all your money you give me a Bible?" He then stormed out of the house, leaving the Bible. Many years passed and the young man was very successful in business. He had a beautiful home and a wonderful family, but realizing his father was very old, he thought perhaps he should go to see him. He had not seen him since that graduation day. Before he could make the arrangements, he received a telegram telling him his father had passed away, and willed all of his possessions to his son. He needed to come home immediately and take care of things. When he arrived at his father's house, sudden sadness and regret filled his heart. He began to search through his father's important papers and saw the still new Bible, just as he had left it years ago. With tears, he opened the Bible and began to turn the pages. As he was reading, a car key dropped from the back of the Bible. It had a tag with the dealer's name, the same dealer who had the sports car he had desired. On the tag was the date of his graduation, and the words... "PAID IN FULL". How many times do we miss blessings because they are not packaged as we expected? Do not spoil what you have by desiring what you have not; but remember that what you now have was once among the things you only hoped for. Sometimes we don't realize the good fortune we have or we could have because we expect "the packaging" to be different. What may appear as bad fortune may in fact be the door that is just waiting to be opened.</div>
</body>
</html>

如果要在onscroll里实时执行逻辑操作,以上代码就力不从心了,只能找别的解决方案。

在iOS中实现sticky header的更多相关文章

  1. RSA算法及其在iOS中的使用

    因为项目中需要传输用户密码,为了安全需要用RSA加密,所以就学习了下RSA加密在iOS中的应用. 关于RSA的历史及原理,下面的两篇文章讲的很清楚了:  http://www.ruanyifeng.c ...

  2. 在iOS中创建静态库

    如果您有不错的原创或译文,欢迎提交给我们,更欢迎其他朋友加入我们的翻译小组(联系qq:2408167315).  =========================================== ...

  3. iOS中解析 XML / JSON

    JSON数据格式 1. 概述: JSON (JavaScript Object Notation) 是⼀一种轻量级的数据交换格式 基于⽂文本格式,易于⼈人阅读和编写,同时也易于机器解析和⽣生成. 2. ...

  4. iOS中忽略NSLog打印信息(通过PCH文件中定义DEBUG宏解决)

    iOS中忽略NSLog打印信息 解决办法: 1.新建PrefixHeader_pch文件,在该文件中定义一下宏 //通过DEBUG宏的定义来解决Debug状态下和Release状态下的输出 #ifde ...

  5. OS X 和iOS 中的多线程技术(上)

    OS X 和iOS 中的多线程技术(上) 本文梳理了OS X 和iOS 系统中提供的多线程技术.并且对这些技术的使用给出了一些实用的建议. 多线程的目的:通过并发执行提高 CPU 的使用效率,进而提供 ...

  6. OS X 和iOS 中的多线程技术(下)

    OS X 和iOS 中的多线程技术(下) 上篇文章中介绍了 pthread 和 NSThread 两种多线程的方式,本文将继续介绍 GCD 和 NSOperation 这两种方式.. 1.GCD 1. ...

  7. iOS中UIWebView执行JS代码(UIWebView)

    iOS中UIWebView执行JS代码(UIWebView) 有时候iOS开发过程中使用 UIWebView 经常需要加载网页,但是网页中有很多明显的标记让人一眼就能看出来是加载的网页,而我们又不想被 ...

  8. iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用

    iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用 前言 开发中经常使用三方库去实现某特定功能,而这些三方库通常又分为开源库和闭源库.开源库可以直接拿到源码,和自 ...

  9. 关于iOS中几种第三方对XML/JSON数据解析的使用

    Json XML 大数据时代,我们需要从网络中获取海量的新鲜的各种信息,就不免要跟着两个家伙打交道,这是两种结构化的数据交换格式.一般来讲,我们会从网络获取XML或者Json格式的数据,这些数据有着特 ...

随机推荐

  1. sourceinsight tab 空格 对齐 等宽字体

    参考:http://bbs.chinaunix.net/thread-587409-1-1.html 1. SMART TAB的用法. 解决自动缩进. 新开一个PROJECT后,点Options-&g ...

  2. ASIHTTPRequest学习(四)

    如果是IOS5的版本,可能集成过程中会遇到一些问题,我也找到了一些解决方案,比如,集成完后可能会遇到编译提示找不到"libxml/HTMLparser.h",解决这个问题可以参考这 ...

  3. java 中的VO,PO,DTO,DO对象

    经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析. 得出的主要结论是:在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应 ...

  4. pycharm批量清楚pyc、$py.class文件

    By default, the .pyc and $py.class files are ignored, and thus are not visible in the Project tool w ...

  5. 在CcentOS系统上将deb包转换为rpm包

    deb文件格式本是ubuntu/debian系统下的安装文件,那么我想要在redhat/centos/fedora中安装,需要把deb格式的软件包转化成rpm格式. 需要用到的转换工具:alien_8 ...

  6. 一分钟学awk够用

    [转载于58同城沈剑] 1.什么是AWK(1)Aho.Weinberger.Kernighan三位发明者名字首字母:(2)一个行文本处理工具: 2.AWK基本原理2.1原理:逐行处理文件中的数据 2. ...

  7. [转载]CentOS 6.5 安装五笔输入法

    FROM:http://blog.sina.com.cn/s/blog_49d6d41c0101i0zs.html 1.一般安装了中文环境会默认安装了好多输入法,先删除了ibus  sudo yum ...

  8. [转载]使用java.lang.Process类的简单例子

    FROM: http://segmentfault.com/blog/lidonghao/1190000000372192 ProcessBuilder类是J2SE 1.5在java.lang中新添加 ...

  9. [Functional Programming] Define Discrete State Transitions using the State ADT

    We build our first state transactions as two discrete transactions, each working on a specific porti ...

  10. 从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?

    这一章节我们来讨论一下如何通过属性注入Bean? 这一章节分为两部分,第一部分我们通过属性向对象注入值,第二部分我们通过属性向对象注入还有一个对象的引用. 1.如何通过属性向对象注入值? (1)dom ...