µ± position:sticky Åöµ½ bootstrap ¸¡¶¯²¼¾ÖʱºòµÄ²È¿Ó¼ÇÔØ
position:sticky
Õâ¸öÊôÐÔ£¬ÎÒ¾ÍÒâʶµ½´ËÇ°µÄ²»ÉÙ js ³¡¾°¿ÉÒÔÓÃÕâ¸ö css ÊôÐÔÈ¥¸Äд¡£±ÈÈç ÍøÕ¾ ÓÒ²àµÄ²»ÉÙ¹ã¸æ£¬¹ö¶¯ÉÏÈ¥ºóÐèÒª fixed£¬ÍêÈ«¾ÍÊÇ sticky µÄÀûÓð¡¡£½ñÌìҪ˵µÄÊÇ ÎÄÕÂÏêÇéÒ³ ÓÒ²àµÄÃûÄ¿À¸£¬µ±Ò³ÃæÏ»¬µÄʱºò£¬ËüÒ²»áÀι̵½Ò³Ã涥²¿£¬´ËÇ°ÊÇÓà js È¥¼àÌý scroll ʼþ£¬È»ºó°´ÕÕλÖÃÍ£Ö¹Íƶϣ¬toggle fixed µÄ·½°¸£¬³öÓÚһЩԵÓÉ£¬¾öÒé¶ÔËüÓà sticky È¥ÖØд¡£
¼¸Ï¾ÍдÍêÁË£¬È¥³ý¹ö¶¯Ê¼þ¼àÌý£¬È»ºó½«²Ëµ¥ÔªËØ .post-nav
¼ÓÉÏ position:sticky; top:0
Ñùʽ£¬µ«ÊÇ£¬²»ÆðЧ£¡
wtf! °Ù˼²»µÃÆä½â£¬ÎÒ¿ª¶ËËÑË÷ÔµÓÉ¡£ÔÚ so Ëѵ½ÁË Õâ¸ö£¬Ëµµ½´ó¸ÅÊÇÔªËصĸ¸¼¶ÔªËØÓÐ¶Ô overflow ÊôÐÔÍ£Ö¹´¦Ö㬱ȷ½¼ÓÁËʲô overflow:hidden
ɶµÄ£¬µ«ÊÇ¿´ÁËÏ£¬²¢Ã»ÓÐÕâÖÖ×´¿ö¡£
È»ºóÎÒ²ÂÏë»á²»»áÊÇ bootstrap ¹æ»®µÄÎÊÌ⣨ÊÂʵÉÏȷʵÓйØϵ£©£¬Ð´Ï demo£º
<!DOCTYPE html> <html> <head> <title></title> <link href="//cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"> <style> body {font-size: 50px; font-weight: 900;} .main {height: 2000px; background: #eee} .menu {height: 200px; background: yellow} .ad {height: 200px; background: red; position: sticky; top: 0px;} .guess {height: 200px; background: blue;} </style> </head> <body> <p class="container"> <p class="row"> <p class="col-md-8 main">content</p> <p class="col-md-4"> <p class="menu">menu</p> <p class="ad">ad</p> <p class="guess">others</p> </p> </p> </p> </body> </html>
µ«ÊÇûÎÊÌ⣬ºöÈ»Ïëµ½ÍøÕ¾ÓÃµÄ bootstrap °æ±¾ÊÇ 3.x£¬È»ºó¸Ä³É 3.3.7 µÄ°æ±¾£¬ÕâʱºòÎÊÌâ¾Í³öÀ´ÁË¡£
ÕâʱºòÎÊÌâ¾Í±ÈÁ¦ºÃ¶¨Î»ÁË£¬4.x ÓõÄÊÇ flex ¹æ»®£¬¶ø 3.x »¹ÊÇ float ¸¡¶¯¹æ»®£¬ÎÊÌâÓ¦µ±ÊdzöÔÚÕâÀïÁË¡£
ÖÕ¾¿´úÂ루²ÎÕÕ Õâ¸ö issue)£º
<!DOCTYPE html> <html> <head> <title></title> <link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <style> body {font-size: 50px; font-weight: 900;} .main {height: 2000px; background: #eee} .side {height: 2000px;} .menu {height: 200px; background: yellow} .ad {height: 200px; background: red; position: sticky; top: 0px;} .guess {height: 200px; background: blue;} </style> </head> <body> <p class="container"> <p class="row"> <p class="col-md-8 main">content</p> <p class="col-md-4 side"> <p class="menu">menu</p> <p class="ad">ad</p> <p class="guess">others</p> </p> </p> </p> </body> </html>
¶ÔÓ¦µ½¿ª¶ËµÄÎÊÌâÉÏ£¬ÓÉÓÚ menu ÊÇÊôÓÚ .col-md-3
ÔªËصģ¬ËùÒÔÓÒ²àµÄ .col-md-3
ÐèÒªºÍ×ó±ßµÄ .col-md-9
±£³Ö¸ß¶ÈÒ»Ö¼´¿É£¬¼ÓÉÏÕâÐдúÂ룺
$('.side').height($('.main').height())
ÓÉÓÚ×ó±ßµÄÄÚÈÝÇøÓòÓÐͼƬµÄÑÓ³Ù¼ÓÔØ£¬ËùÒÔÕâÐдúÂëÐèÒªÁ¬ÐøÊ©ÐУº
$(window).scroll(function() { $('.side').height($('.main').height()) // other code // ... })
×ܽ᣺ÒÔÉϾÍÊDZ¾ÆªÎĵÄÈ«²¿ÄÚÈÝ£¬µ«Ô¸ÄܶԴó¼ÒµÄѧϰÓÐËù°ïæ¡£¸ü¶àÏà¹Ø½Ì³ÌÇë°Ý·Ã HTMLÊÓƵ½Ì³Ì£¡
Ïà¹ØÒý¼ö£º
php¹«ÒæÅàѵÊÓƵ½Ì³Ì
ÒÔÉϾÍÊǵ± position:sticky Åöµ½ bootstrap ¸¡¶¯¹æ»®Ê±ºòµÄ²È¿Ó¼ÇÔصľßÌåÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢°Ù·Ö°ÙÔ´ÂëÍøÆäËüÏà¹ØÎÄÕ£¡
¸ÐлÄúµÄÖ§³Ö£¬ÎÒ»á¼ÌÐøŬÁ¦µÄ!
´ò¿ªÖ§¸¶±¦É¨Ò»É¨£¬¼´¿É½øÐÐɨÂë´òÉÍŶ
°Ù·Ö°ÙÔ´ÂëÍø ½¨Òé´òÉÍ1¡«10Ôª£¬ÍÁºÀËæÒ⣬¸ÐлÄúµÄÔĶÁ£¡