日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

当 position:sticky 遇到 bootstrap 浮动布局时候的踩坑记录

發(fā)布時(shí)間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 当 position:sticky 遇到 bootstrap 浮动布局时候的踩坑记录 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

當(dāng)?shù)谝淮谓佑|到 position:sticky 這個(gè)屬性,我就意識(shí)到之前的不少 js 場(chǎng)景可以用這個(gè) css 屬性去改寫(xiě)。譬如 網(wǎng)站 右側(cè)的不少?gòu)V告,滾動(dòng)上去后需要 fixed,完全就是 sticky 的應(yīng)用啊。

今天要說(shuō)的是 文章詳情頁(yè) 右側(cè)的目錄欄,當(dāng)頁(yè)面下滑的時(shí)候,它也會(huì)固定到頁(yè)面頂部,之前是用 js 去監(jiān)聽(tīng) scroll 事件,然后根據(jù)位置進(jìn)行判斷,toggle fixed 的方案,出于一些原因,決定對(duì)它用 sticky 去重寫(xiě)。

幾下就寫(xiě)完了,去掉滾動(dòng)事件監(jiān)聽(tīng),然后將菜單元素 .post-nav 加上 position:sticky; top:0 樣式,但是,不起效!

wtf! 百思不得其解,我開(kāi)始搜索原因。在 so 搜到了 這個(gè),說(shuō)到可能是元素的父級(jí)元素有對(duì) overflow 屬性進(jìn)行處理,比如加了什么 overflow:hidden 啥的,但是看了下,并沒(méi)有這種情況。

然后我猜想會(huì)不會(huì)是 bootstrap 布局的問(wèn)題(事實(shí)上確實(shí)有關(guān)系),寫(xiě)下 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> <div class="container"><div class="row"><div class="col-md-8 main">content</div><div class="col-md-4"><div class="menu">menu</div><div class="ad">ad</div><div class="guess">others</div></div></div> </div> </body> </html>

但是沒(méi)問(wèn)題,突然想到網(wǎng)站用的 bootstrap 版本是 3.x,然后改成 3.3.7 的版本,這時(shí)候問(wèn)題就出來(lái)了。

這時(shí)候問(wèn)題就比較好定位了,4.x 用的是 flex 布局,而 3.x 還是 float 浮動(dòng)布局,問(wèn)題應(yīng)該是出在這里了。

最終代碼(參考 這個(gè) 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> <div class="container"><div class="row"><div class="col-md-8 main">content</div><div class="col-md-4 side"><div class="menu">menu</div><div class="ad">ad</div><div class="guess">others</div></div></div> </div> </body> </html>

對(duì)應(yīng)到開(kāi)始的問(wèn)題上,因?yàn)?menu 是屬于 .col-md-3 元素的,所以右邊的 .col-md-3 需要和左邊的 .col-md-9 保持高度一致即可,加上這行代碼:

$('.side').height($('.main').height())

因?yàn)樽筮叺膬?nèi)容區(qū)域有圖片的延遲加載,所以這行代碼需要持續(xù)執(zhí)行:

$(window).scroll(function() { $('.side').height($('.main').height())// other code // ...})

總結(jié)

以上是生活随笔為你收集整理的当 position:sticky 遇到 bootstrap 浮动布局时候的踩坑记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。