рдореЛрдмрд╛рдЗрд▓ рдмреНрд░рд╛рдЙрдЬрд╝рд░ рдФрд░ рд╕реНрдерд┐рддрд┐: рдирд┐рд╢реНрдЪрд┐рдд

CSS position:fixed рдореЛрдмрд╛рдЗрд▓ рд╕рдлрд╛рд░реА рдореЗрдВ position:fixed рдкреНрд░реЙрдкрд░реНрдЯреА рдХрд╛рдлреА рд╣рдж рддрдХ iOS 5 рдХреЗ рд╕рд╛рде рд╢реБрд░реВ рд╣реЛрддреА рд╣реИред рдПрдВрдбреНрд░реЙрдЗрдб рдореЗрдВ, рджреЗрд╢реА рдмреНрд░рд╛рдЙрдЬрд╝рд░ рдЖрдВрд╢рд┐рдХ рд░реВрдк рд╕реЗ рд╕рд┐рд╕реНрдЯрдо рд╕рдВрд╕реНрдХрд░рдг 2.1 рд╕реЗ рд╢реБрд░реВ рд╣реЛрдиреЗ рд╡рд╛рд▓реА рдЗрд╕ рд╕рдВрдкрддреНрддрд┐ рдХреЛ рд╕рдордЭрддрд╛ рд╣реИ, рдкрд░реНрдпрд╛рдкреНрдд рд░реВрдк рд╕реЗ 2.2 рдХреЗ рд╕рд╛рде, 3.0 рдХреЗ рд╕рд╛рде рдкреВрд░реНрдг рд╕рдорд░реНрдердиред рдЕрдзрд┐рдХ: рд╕рдорд░реНрдерди рддрд╛рд▓рд┐рдХрд╛ position:fixed ред

HTML:
 <div id="topbar">Fixed Title</div> <div id="content"> <h2>Start</h2> <p>Main content text </p> ... <p>Main content text </p> <h2>End</h2> </div> <div id="bottombar">Fixed footer</div> 

рд╕реАрдПрд╕рдПрд╕:
 #content{ padding: 50px 0; /*   ,         */ } #topbar, #bottombar { position: fixed; left: 0; width:100%; height: 50px; /*     */ line-height:50px; background:#eee; text-align: center; } #topbar {top: 0;} #bottombar {bottom: 0;} 

рдЕрдм рдЖрдзреБрдирд┐рдХ рд╕реНрдорд╛рд░реНрдЯрд╕ рдореЗрдВ, рд╣рдорд╛рд░реЗ рдЯреЙрдкрдмрд╛рд░ рдФрд░ рдмреЙрдореНрдмреЛрдмрд░ рдХреНрд░рдорд╢рдГ рдЦрд┐рдбрд╝рдХреА рдХреЗ рдКрдкрд░ рдФрд░ рдиреАрдЪреЗ "рдиреЗрд▓реНрдб" рд╣реЛрддреЗ рд╣реИрдВред IOS рдореЗрдВ рдкрд╣рд▓реЗ рд╕реНрдХреНрд░реЙрд▓ рдХреЗ рджреМрд░рд╛рди рдкреЛрдЬрд┐рд╢рдирд┐рдВрдЧ рдХреА рд╕рдорд╕реНрдпрд╛ рдХреЛ рдорд┐рдиреА- Javacript ( рдлрд┐рдХреНрд╕реНрдб ) рджреНрд╡рд╛рд░рд╛ рд╣рд▓ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ:
 window.scrollBy(0, 1); 


"рдмреВрдврд╝реЗ" рдХреЗ рд▓рд┐рдП рд╣рдо рдкреНрд░рдЧрддрд┐рд╢реАрд▓ рд╕рдВрд╡рд░реНрджреНрдзрди рдХреЗ рд╕рд┐рджреНрдзрд╛рдВрдд рдкрд░ рдирдХрд▓ рдХрд░рддреЗ рд╣реИрдВред

рд╣рдо position:fixed; рд▓рд┐рдП рд╕рдорд░реНрдерди рдХреЛ рдкрд░рд┐рднрд╛рд╖рд┐рдд рдХрд░рддреЗ рд╣реИрдВ position:fixed; рд╕реНрдЯреИрдХрдУрд╡рд░рдлрд╝реНрд▓реЛ рдХреЗ рд╕рд╛рде рд╡рд┐рдзрд┐ рдХреЗ рдЕрдиреБрд╕рд╛рд░ рдФрд░ рдЧреИрд░-рд╕рд╣рд╛рдпрдХ рд╡рд╛рд▓реЗ ( рддрдп ) рдХреЗ рд▓рд┐рдП рдПрдХ рд╕рдВрджрд░реНрдн рд╡рд░реНрдЧ рдЬреЛрдбрд╝реЗрдВ:
 //   position: fixed;[start] var isFixedSupported = (function(){ var isSupported = null; if (document.createElement) { var el = document.createElement("div"); if (el && el.style) { el.style.position = "fixed"; el.style.top = "10px"; var root = document.body; if (root && root.appendChild && root.removeChild) { root.appendChild(el); isSupported = el.offsetTop === 10; root.removeChild(el); } } } return isSupported; })(); //    "" window.onload = function(){ if (!isFixedSupported){ document.body.className += ' no-fixed-supported' : ''; } //  scroll window.scrollBy(0, 1); } 

рддрджрдиреБрд╕рд╛рд░ рд╕реАрдПрд╕рдПрд╕:
 .no-fixed-supported #topbar, .no-fixed-supported #bottombar { position: absolute; } 

рд╕реНрдкрд░реНрд╢ рдФрд░ рд╕реНрдХреНрд░реЙрд▓ рдИрд╡реЗрдВрдЯ рд╣реИрдВрдбрд▓рд┐рдВрдЧ рдЬреЛрдбрд╝реЗрдВ:
 //  position: fixed; var topbar = document.getElementById('topbar'); var bottombar = document.getElementById('bottombar'); var bottomBarHeight = bottombar.offsetHeight; var windowHeight = window.innerHeight; //   touch  scroll window.ontouchstart = function(e) { if (event.target !== topbar){ topbar.style = ""; } } window.onscroll = function(){ var scrollTop = window.scrollY; topbar.style.top = scrollTop + 'px'; bottombar.style.bottom = (scrollTop + windowHeight - bottomBarHeight) + 'px'; }; 

рдкрд░рд┐рдгрд╛рдо (рдирд┐рд╢реНрдЪрд┐рдд)
 <!DOCTYPE html> <html> <head> <title>TEST</title> <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no" /> <style type="text/css"> #content{ padding: 50px 0; /*   ,         */ } #topbar, #bottombar { position: fixed; left: 0; width:100%; height: 50px; /*   topbar'   */ line-height:50px; background:#eee; text-align: center; } #topbar {top: 0;} #bottombar {bottom: 0;} .no-fixed-supported #topbar, .no-fixed-supported #bottombar { position: absolute; } </style> </head> <body> <div id="topbar">Fixed Title</div> <div id="content"> <h2>Start</h2> <p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p><p>Main content text</p> <h2>End</h2> </div> <div id="bottombar">Fixed footer</div> <script type="text/javascript"> //   position: fixed;[start] var isFixedSupported = (function(){ var isSupported = null; if (document.createElement) { var el = document.createElement("div"); if (el && el.style) { el.style.position = "fixed"; el.style.top = "10px"; var root = document.body; if (root && root.appendChild && root.removeChild) { root.appendChild(el); isSupported = (el.offsetTop === 10); root.removeChild(el); } } } return isSupported; })(); window.onload = function(){ if (!isFixedSupported){ //    "" document.body.className += ' no-fixed-supported'; //  position: fixed; var topbar = document.getElementById('topbar'); var bottombar = document.getElementById('bottombar'); var bottomBarHeight = bottombar.offsetHeight; var windowHeight = window.innerHeight; //   touch  scroll window.ontouchmove = function(e) { if (event.target !== topbar){ topbar.style = ""; } } window.onscroll = function(){ var scrollTop = window.scrollY; topbar.style.top = scrollTop + 'px'; bottombar.style.bottom = (scrollTop + windowHeight - bottomBarHeight) + 'px'; }; } //  scroll window.scrollBy(0, 1); } </script> </body> </html> 
рдбреЗрдореЛ

UPD: рдкреНрд░реЗрд░рдгрд╛ рдХрд╛ рд╕реНрд░реЛрдд ред
UPD2: рдХрд┐рдП рдЧрдП рдкрд░рд┐рд╡рд░реНрддрдиред рдЕрдХреНрд╖рдо рдСрдЯреЛ рдЬрд╝реВрдоред рдХреЛрдб рддрдп рд╣реИред рдирд┐рд╢реНрдЪрд┐рдд рдкреНрд░рд╛рдердорд┐рдХ рдкреВрд░реНрд╡рд╛рдЧреНрд░рд╣ред IOS 6.0.1, Android 2.3.7 рдкрд░ рдкрд░реАрдХреНрд╖рдг рдХрд┐рдпрд╛ рдЧрдпрд╛ред рдЬреЛрдбрд╝рд╛ рдЧрдпрд╛ рдбреЗрдореЛред

Source: https://habr.com/ru/post/In162841/


All Articles