【一天一道兼容性】之——IOS4及以下fixed失效
生活随笔
收集整理的這篇文章主要介紹了
【一天一道兼容性】之——IOS4及以下fixed失效
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
少敘閑言
如今手機換代都快趕上換衣服速度了,每每出新手機都是各種搭載最新系統,大家都在關心android5.0該不該叫切糕?IOS的最新版啥時候出完美越獄……,可偏偏就總有些人抱著舊系統來測你的頁面,沒有那金剛鉆,還非要攬這瓷器活?!android碎也就那么地了,你蘋果來湊什么熱鬧啊……,今兒接到個任務,說是領導家有個親戚,上我們時候首頁的一個fixed元素在ios舊版本中失效,讓做兼容……,好吧,為了什么鬼親戚,我只能蝴蝶效應了……
正題
demo:
?
<style>html, body, div, p {margin: 0;padding: 0; }body {height: 3000px;background-color:#a55;}#p {width: 100%;height: 50px;background: #5a5;position: fixed;bottom: 0;} </style> <body><p id = "p"></p> </body>?
如圖:(模擬失效環境)
?
解析問題:
1:IOS5以下版本的safari不支持fixed
2:android據說2.0以上就支持fixed,但我測試時候發現,2.2的i9000上,測試頁好使,但線上大量數據時候失效,可能由于搭載的硬件關系導致。
?
解決辦法:
利用position:absolute;bottom:0和scroll事件進行模擬fixed,另外,當觸發touchstart時候,瀏覽器會暫停頁面渲染,也就是我們看到的截屏效果,所以,為了滾動時候不影響美觀,所以先將其隱藏掉,事后再放出來。
1 <script> 2 var android2_3up = navigator.userAgent.match(/Android [2-9].[3-9][.\d]*/); 3 var ios5up = navigator.userAgent.match(/OS [5-9]_\d[_\d]*/); 4 var isMobile = !!navigator.userAgent.match(/AppleWebKit.*Mobile.*/); 5 if (isMobile && !ios5up || !android2_3up) {//匹配ios4 或android2.3以下 6 var p = document.getElementById("p"); 7 p.style.display = "absolute"; 8 function handler(event) { 9 var top = window.innerHeight + document.body.scrollTop - 50; 10 p.style.top = top + "px"; 11 p.style.opacity = 1; 12 } 13 function touchstart(event) { 14 p.style.opacity = 0; 15 } 16 function touchend(event) { 17 p.style.opacity = 1; 18 } 19 document.addEventListener("scroll", handler, false); 20 document.addEventListener("touchstart", touchstart, false); 21 document.addEventListener("touchend", touchend, false); 22 } 23 </script>?
?
?
?
?
轉載于:https://www.cnblogs.com/ccto/archive/2013/04/18/3029237.html
總結
以上是生活随笔為你收集整理的【一天一道兼容性】之——IOS4及以下fixed失效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Http协议中的各种长度限制总结
- 下一篇: AUTOCAD自学教程一