小技巧css解决移动端ios不兼容position:fixed属性,无需插件

本文主要是介绍小技巧css解决移动端ios不兼容position:fixed属性,无需插件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

移动端开发仿app头部底部固定设置position:fixed,android2.2以上已经实现。但是在ios8以下系统,当小键盘激活时,都会出现位置浮动问题。如图:


如何解决:

查阅资料之后想到一下几种解决方法

1,使用position:absolute模拟

<script type="text/javascript">window.οnscrοll=function(){$(".fixed").css("top",$(window).scrollTop());$(".foot").css("top",$(window).scrollTop()+$(window).height());}</script>

问题来了:滑动页面时头部底部div会有明显的抖动。

2,判断当前获得焦点元素是input则隐藏div改为position:absolute

<body οnlοad=setInterval("a()",500)><script type="text/javascript">function a(){if(document.activeElement.tagName == 'INPUT'){    $(".fixed").css({'position': 'absolute','top':'0'}); } else {  $(".fixed").css('position', 'fixed');  }}</script>

问题来了:不停监控dom,消耗资源。如果input个数较少,可在input里面添加onfocus事件好一些。

3,插件iscroll.js个人感觉不是很好用。可能方法不对,jQuery Mobile  没尝试,感觉会增负担。

4,重点来了:

只需要在中间部分外层div添加css样式position:fixed;top:50px; bottom:50px;overflow:scroll;就可以实现效果,无需插件。可拷贝下面代码运行。

<!DOCTYPE html><html lang="zh_cmn"><head><meta charset=utf-8 /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /><meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" /><title></title><style>.head,.foot{position:fixed;left:0;height:38px;line-height:38px;width:100%;background-color:#99CC00;}.head{top:0;}.foot{bottom:0;}.main{position:fixed;top:38px;bottom:38px;width:100%;overflow:scroll;background-color:#BABABA;}</style></head><body><header class="head">顶部固定区域</header><article class="main"  id="wrapper">  <div><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><p>当内容欲出隐藏时,灰色区域可上下拖动</p><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br><input type="text" value="" class="inputtext"> <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br></div></article><footer class="foot">底部固定区域</footer></body></html>

当小键盘出现时头部、底部自动跳到页面最顶端、最底端。键盘隐藏时又会固定在头部,底部。顿时感觉开朗了

转载来源:http://blog.csdn.net/liu__hua/article/details/40106595



这篇关于小技巧css解决移动端ios不兼容position:fixed属性,无需插件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/955072

相关文章

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

PyInstaller打包selenium-wire过程中常见问题和解决指南

《PyInstaller打包selenium-wire过程中常见问题和解决指南》常用的打包工具PyInstaller能将Python项目打包成单个可执行文件,但也会因为兼容性问题和路径管理而出现各种运... 目录前言1. 背景2. 可能遇到的问题概述3. PyInstaller 打包步骤及参数配置4. 依赖

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

如何解决mmcv无法安装或安装之后报错问题

《如何解决mmcv无法安装或安装之后报错问题》:本文主要介绍如何解决mmcv无法安装或安装之后报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mmcv无法安装或安装之后报错问题1.当我们运行YOwww.chinasem.cnLO时遇到2.找到下图所示这里3.

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.