.search { /* Container just in case we want more than just the search input to come along */ position: absolute; top: 155px; left: 20px; right: 20px; input { width: 265px; transition: width 0.2s; -webkit-appearance: none; /* Autoprefixer doesn't catch this */ } }
.top { height: 250px; /* Space in here for search */ padding-top: 40px; position: relative; }
.search { /* Container just in case we want more than just the search input to come along */ ... .fix-search & { position: fixed; top: 10px; input { width: 250px; } } }
2 状态切换
状态切换可以用 JS 代码来实现。这里给出了利用 jQuery 的一个案例
1 2 3 4 5 6 7 8 9
var wrap = $("#wrap");
wrap.on("scroll", function (e) { if (this.scrollTop > 147) { wrap.addClass("fix-search"); } else { wrap.removeClass("fix-search"); } });
我实际用的是下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
let rc = document.getElementsByClassName("post-toc"); if (rc.length == 0) { return; } window.onscroll = function () { // 185 是 toc 部分两个状态的 top 位置差 // 64 是导航栏的高度 if (document.scrollingElement.scrollTop > 185 + 64) { for (xx of rc) { xx.classList.add("fixed"); } } else { for (xx of rc) { xx.classList.remove("fixed"); } } };