在iOS中实现sticky header
经常在网页中看到这样一种效果,当页面滚动一段距离后,页面中的某个部分固定在一个区域(通常是头部导航),这种效果一般称为Sticky Header,如下图所示:
上述操作在Android系统中非常好实现,只需要监听onscroll事件并将实现逻辑写在里面即可,但iOS则不一样,它在页面scroll的时候,是不会执行代码的,直到页面停下来为止。
如此一来上面的效果就比较难实现了,因为不能实时更新导航条的位置,所以用户体验会与Android系统不一致。经过查阅资料,大概找了几种解决方案:
- 在iOS中同时监听onscroll与ontouchmove(然而尝试过后效果不好,特别是在屏幕上飞快地划一下然后把手指松开的时候);
- 自己模拟实现页面滚动,或者使用iScroll.js(感觉比较复杂没有试过);
- 使用postion:sticky属性(最终采用了这个,因为简单而且性能也好);
position:sticky是CSS3中新增的样式,它的表现相当于position:relative
和position: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的更多相关文章
- RSA算法及其在iOS中的使用
因为项目中需要传输用户密码,为了安全需要用RSA加密,所以就学习了下RSA加密在iOS中的应用. 关于RSA的历史及原理,下面的两篇文章讲的很清楚了: http://www.ruanyifeng.c ...
- 在iOS中创建静态库
如果您有不错的原创或译文,欢迎提交给我们,更欢迎其他朋友加入我们的翻译小组(联系qq:2408167315). =========================================== ...
- iOS中解析 XML / JSON
JSON数据格式 1. 概述: JSON (JavaScript Object Notation) 是⼀一种轻量级的数据交换格式 基于⽂文本格式,易于⼈人阅读和编写,同时也易于机器解析和⽣生成. 2. ...
- iOS中忽略NSLog打印信息(通过PCH文件中定义DEBUG宏解决)
iOS中忽略NSLog打印信息 解决办法: 1.新建PrefixHeader_pch文件,在该文件中定义一下宏 //通过DEBUG宏的定义来解决Debug状态下和Release状态下的输出 #ifde ...
- OS X 和iOS 中的多线程技术(上)
OS X 和iOS 中的多线程技术(上) 本文梳理了OS X 和iOS 系统中提供的多线程技术.并且对这些技术的使用给出了一些实用的建议. 多线程的目的:通过并发执行提高 CPU 的使用效率,进而提供 ...
- OS X 和iOS 中的多线程技术(下)
OS X 和iOS 中的多线程技术(下) 上篇文章中介绍了 pthread 和 NSThread 两种多线程的方式,本文将继续介绍 GCD 和 NSOperation 这两种方式.. 1.GCD 1. ...
- iOS中UIWebView执行JS代码(UIWebView)
iOS中UIWebView执行JS代码(UIWebView) 有时候iOS开发过程中使用 UIWebView 经常需要加载网页,但是网页中有很多明显的标记让人一眼就能看出来是加载的网页,而我们又不想被 ...
- iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用
iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用 前言 开发中经常使用三方库去实现某特定功能,而这些三方库通常又分为开源库和闭源库.开源库可以直接拿到源码,和自 ...
- 关于iOS中几种第三方对XML/JSON数据解析的使用
Json XML 大数据时代,我们需要从网络中获取海量的新鲜的各种信息,就不免要跟着两个家伙打交道,这是两种结构化的数据交换格式.一般来讲,我们会从网络获取XML或者Json格式的数据,这些数据有着特 ...
随机推荐
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- Extract - <<凤凰牌老熊-现代支付系统设计>>
本文摘录自: http://blog.lixf.cn/essay/2017/04/01/concept-01-overview/ 一.支付概述-- 1. 支付与交易 交易过程: 交易的存在是支付发生的 ...
- Python的网络编程[2] -> TFTP 协议[0] -> TFTP 的基本理论
TFTP 的基本理论 目录 通信流程 数据报文格式 传输终结 异常处理 数据丢失和超时 TFTP(Trivial File Transfer Protocol,简单文件传输协议)是UDP协议族中的一个 ...
- hdu 1425 Happy 2004
题目链接 hdu 1425 Happy 2004 题解 题目大意: 求 \[\sum_{d|2004^{x}}d\ mod\ 29\] 记为\(s(2004^x)\) \(sum(2004^{x})= ...
- 归档 & 解档
代码实现 遵守协议 class AccessToken: NSObject, NSCoding 实现协议方法 // MARK: - 归档&解档 required init(coder aDec ...
- Vue服务端渲染 VS Vue浏览器端渲染)
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- github 多账户配置
一直以来git都是自己业余玩玩,虽然也在github上有个账户,也只是小打小闹,弄了若干个测试仓库.最近公司要从TFS迁移到Gerrit,也算是正式用git了.不过遇到的一个问题就是账户问题,一个个人 ...
- [Android Traffic] 根据网络类型更改下载模式
转载自: http://blog.csdn.net/kesenhoo/article/details/7396321 Modifying your Download Patterns Based on ...
- elasticsearch 安装和部署
jdk要用1.8以上(elasticsearch版本是1.7.3) 下载elasticsearch的tar包,解压开,更改其名称 mv elasticsearch-5.x.x elasticsear ...
- SpringMVC 文本文件下载的配置
页面: <fieldset> <legend>Download annotator list</legend> <img src="pages/to ...