简体   繁体   English

粘性侧边栏和页脚不起作用

[英]Sticky Sidebar and Footer not working

I am trying to make my side navigation bar sticky so that when you scroll the page content, the sidebar will remain visible (and scrollable) and not scroll with the page content. 我试图使侧面导航栏保持粘性,以便在滚动页面内容时,侧边栏将保持可见(且可滚动),并且不会随页面内容一起滚动。

I am also trying to make my footer sticky only when the page content is smaller then the screen size, otherwise it only pops up after the page content has ended. 我还试图仅在页面内容小于屏幕大小时使页脚变粘,否则仅在页面内容结束后才弹出。 (Like this example: https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ ) (例如这个例子: https : //philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/

I am a novice web developer trying to make a website for a friend as a project. 我是一个新手Web开发人员,试图为一个朋友创建一个网站作为项目。 I am trying to keep the site responsive so that it works well on mobiles and different computer resolutions. 我试图使网站保持响应状态,以便它在手机和不同的计算机分辨率下都能正常工作。

I am using Bootstrap 4.0.0-beta3, font-awesome 4.7.0 and javascript. 我正在使用Bootstrap 4.0.0-beta3,超棒的字体4.7.0和javascript。

UPDATE : I tried to use flexboxes to make the footer sticky, but it didn't work. 更新 :我试图使用flexboxes使页脚变粘,但是没有用。

.wrapper {
    display: flex;
    align-items: stretch;
}

I have placed a copy of most of my code here because it seamed a little big to write out all in this post.: https://jsbin.com/xabisiq/edit?html,css,js,output 我在这里放置了大部分代码的副本,因为在这篇文章中,所有代码都写得有点大。: https ://jsbin.com/xabisiq/edit?html,css,js,output

Thank you in advance! 先感谢您!

Ok let's go step by step. 好吧,让我们一步一步走。

Side bar sticky with position fixed 侧杆发粘,位置固定

When you give something position:fixed you have to understand you are taking the element out of the flow of the normal flow. position:fixed您必须了解自己正在从正常流程的流程中删除该元素。 It will no longer render in its assigned position. 它将不再渲染到其指定位置。 In the case of fixed , it will not move when scrolled and will be on top of other elements. 对于fixed ,滚动时它将不会移动,并且会位于其他元素的顶部。 Kind of like if you took your computer monitor and stuck a yellow sticky note on a corner of it, literally. 就像是拿起计算机显示器并在其一角贴了一个黄色便签一样。 All the elements in your monitor render, and move, your sticky note stays there forever, on top of everyone. 监视器中的所有元素都会将粘滞便笺呈现并移动,并永远固定在每个人的上方。 So how do you manage to view the elements behind it? 那么,您如何查看其背后的元素? Well, you have to give them CSS rules to force them to take into account that space of the fixed element, because on their own they can't see that element any more. 好吧,您必须给他们CSS规则,以迫使他们考虑fixed元素的空间,因为他们自己再也看不到该元素了。 I hope that wasn't too confusing. 我希望不要太困惑。

HTML HTML

Move your sidebar HTML out of that parent div container #main-Body-Head 将侧边栏HTML移出该父div容器#main-Body-Head

<nav class="navbar navbar-expand-md bg-dark navbar-dark fixed-top">...</nav>
<nav id="sidebar">...</nav>
<div class="container" id="main-Body-Head">...</div>

Give appropriate rules: 给出适当的规则:

#sidebar {
 position: fixed;
 left: 0;
 top: 61px; //size of top nav
}

See how I had to hard-code the top part? 看到我必须对top进行硬编码了吗? That's because sidebar doesn`t know nor care about the top nav element, you have to tell it to give it some space or you won't be able to view that element. 那是因为sidebar知道也不关心顶部的nav元素,您必须告诉它留出一些空间,否则您将无法查看该元素。

Now you have to tell the main content of your page to move over and make space for the sidebar with: 现在,您必须使用以下命令告诉页面的主要内容并为sidebar腾出空间:

.wrapper {
    padding-left: 250px; //size of #sidebar
}

Snapshot 快照

Footer with sticky via flex 通过flex粘贴的页脚

For this case I simply added the CSS rules available in that wonderful link. 对于这种情况,我只是在该链接中添加了可用的CSS规则。 Excellent post really! 真的很棒!

body{
   display: flex;
   min-height: 100vh;
   flex-direction: column;
}

#main-Body-Head { flex:1;  }

Snapshot 快照

That's it really. 就是这样。

Problems? 问题?

Well, the fact that as you scroll down you'll see that your side bar will scroll over your footer. 好吧,事实上,当您向下滚动时,您会看到侧边栏将在页脚上滚动。 That will need to be fixed. 这将需要解决。 Also when you git the arrow and your sidebar moves to the right, you need to adjust the body's padding accordingly. 同样,当您按下箭头时,边栏也会向右移动,您需要相应地调整主体的填充。 Hope this helped! 希望这对您有所帮助!

I recommend the best book I ever read on CSS: CSS mastery by Andy Budd. 我推荐我曾经读过的关于CSS的最好的书:Andy Budd的CSS精通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM