经常在网页中看到这样一种效果,当页面滚动一段距离后,页面中的某个部分固定在一个区域(通常是头部导航),这种效果一般称为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. codeforces-540C

    题目连接:http://codeforces.com/problemset/problem/540/C C. Ice Cave time limit per test 2 seconds memory ...

  2. [POI2008]Mirror Trap

    题目大意: 一个$n(n\le10^5)$个顶点的格点多边形,每条边平行于网格线,每个角度数均为$90^\circ$或$270^\circ$,周长小于$3\times10^5$,每个顶点可以安装激光发 ...

  3. yum安装openresty

    在群里看到春哥发的,先记录下来.一切都以官网为准,以后安装部署生态会越来越完善的. OpenResty 官方现在开始维护自己的打包虚机集合了,新的 linux 包仓库正在陆续登陆 openresty. ...

  4. 三. Java类与对象9. 源文件的声明规则

    当在一个源文件中定义多个类,并且还有import语句和package语句时,要特别注意这些规则: 一个源文件中只能有一个public类. 一个源文件可以有多个非public类. 源文件的名称应该和pu ...

  5. CentOS 6与CentOS 7的区别收集

    说明: 1.CentOS与Ubuntu没有什么可比性,底层都是Linux,并且Ubuntu在YY广泛的使用,这些并不能说明那个强大哪个不行,只要能解决问题的都是好家伙. 2.市面上教程基本都是基于6, ...

  6. strace 使用案例

    http://www.cnblogs.com/lixigang/articles/5512527.html

  7. linux 远程同步数据工具rsync (2)

    在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后本机作为rsync的一个客 户端去连接远程的rsync服务器.如何去配置一台rsync服务器. 首先配置/etc/rs ...

  8. 控制面板cpl大全

    ALSNDMGR.CPL AC97 Audio组态设定appwiz.cpl 添加和删除程序bthprops.cpldesk.cpl   显示属性firewall.cpl Windows防火墙hdwwi ...

  9. JAVA之HashMap集合

    /** * HashMap集合讲解 * HashMap集合不允许集合元素的Key重复 */package com.test; import java.util.*; public class test ...

  10. javascript正则表达式小技巧

    一.中括号[]里面的特殊字符是不用转义的,例如[/].[.].[*].[?].[+]都是可以直接匹配对应的字符\ . *?+.下面是测试结果: 所以,/[\d.]/这个正则表达式实际上是匹配数字字符或 ...