如何使用flexBox制作的圣杯布局中的侧边杆变得粘稠?即.当最后一个元素到达时,html的旁边和nav部分应该停止向下滚动.我尝试了多种方式,但取得了一些成功.
@L_502_0@:
<body> <header>header</header> <div id='main'> <article>This area has lot of content </article>` <nav> This nav should not scroll</nav> <aside>This aside too</div></aside> </div> <footer>footer</footer> </body>
CSS:
body { /*font: 24px Helvetica;*/ background: #999999; } #main { min-height: 800px; margin: 0px; padding: 0px; display: -webkit-flex; display: flex; -webkit-flex-flow: row; flex-flow: row; } #main > article { margin: 4px; padding: 5px; border: 1px solid #cccc33; border-radius: 7pt; background: #dddd88; -webkit-flex: 3 1 60%; flex: 3 1 60%; -webkit-order: 2; order: 2; } #main > nav { margin: 4px; padding: 5px; border: 1px solid #8888bb; border-radius: 7pt; background: #ccccff; -webkit-flex: 1 6 20%; flex: 1 6 20%; -webkit-order: 1; order: 1; } #main > aside { margin: 4px; padding: 5px; border: 1px solid #8888bb; border-radius: 7pt; background: #ccccff; -webkit-flex: 1 6 20%; flex: 1 6 20%; -webkit-order: 3; order: 3; } header,footer { display: block; margin: 4px; padding: 5px; min-height: 100px; border: 1px solid #eebb55; border-radius: 7pt; background: #ffeebb; }
解决方法
我已经从代码笔拿起代码,并根据需要用一些侧边栏的一些逻辑重写了JS代码.希望它是有用的:)
var stickArea = $('aside ul'); var footArea = $('footer'); margin = 10; offtop = stickArea.offset().top - margin; offbtm = footArea.offset().top - (margin * 3 + stickArea.height()); $(window).scroll(function() { topScr = $(window).scrollTop(); if (topScr > offtop && stickArea.hasClass('natural')) { stickArea.removeClass('natural').addClass('fixed').css('top',margin); } if (offtop > topScr && stickArea.hasClass('fixed')) { stickArea.removeClass('fixed').addClass('natural').css('top','auto'); } if (topScr > offbtm && stickArea.hasClass('fixed')) { stickArea.removeClass('fixed').addClass('bottom').css('top',offbtm); } if (offbtm > topScr && stickArea.hasClass('bottom')) { stickArea.removeClass('bottom').addClass('fixed').css('top',margin); } });
* { Box-sizing: border-Box; } body { min-height: 100vh; display: -webkit-Box; display: -ms-flexBox; display: flex; -webkit-Box-orient: vertical; -webkit-Box-direction: normal; -ms-flex-direction: column; flex-direction: column; } .master { -webkit-Box-flex: 1; -ms-flex-positive: 1; flex-grow: 1; display: -webkit-Box; display: -ms-flexBox; display: flex; -webkit-Box-pack: center; -ms-flex-pack: center; justify-content: center; } .master .inner-w { display: -webkit-Box; display: -ms-flexBox; display: flex; -webkit-Box-orient: vertical; -webkit-Box-direction: normal; -ms-flex-direction: column; flex-direction: column; -ms-flex-preferred-size: 960px; flex-basis: 960px; } .master main { display: -webkit-Box; display: -ms-flexBox; display: flex; -webkit-Box-orient: vertical; -webkit-Box-direction: normal; -ms-flex-direction: column; flex-direction: column; -webkit-Box-flex: 1; -ms-flex-positive: 1; flex-grow: 1; } @media (min-width: 900px) { .master main { -webkit-Box-orient: horizontal; -webkit-Box-direction: normal; -ms-flex-direction: row; flex-direction: row; -webkit-Box-pack: justify; -ms-flex-pack: justify; justify-content: space-between; } } .master .content { -ms-flex-preferred-size: 70%; flex-basis: 70%; min-height: 2000px; } .master aside { -ms-flex-preferred-size: 350px; flex-basis: 350px; -webkit-Box-flex: 0; -ms-flex-positive: 0; flex-grow: 0; -ms-flex-negative: 0; flex-shrink: 0; } .master aside .widget-list { width: 330px; } body.sticky-sidebar .master aside .widget-list { position: fixed; top: 10px; } body { margin: 0; background: #add8e6; padding: 10px; } .master { background: #90ee90; } .master .inner-w { padding: 10px; } .master header,.master .content,.master aside,.master footer { padding: 10px; } .master header { background: #ffc0cb; min-height: 120px; } .master .content { background: #f5deb3; } .master aside { background: #ffd700; min-height: 400px; } .master aside .widget-list { background: #d3d3d3; padding: 10px; } .master aside .widget-list .widget { background: rgba(0,0.1); padding: 10px; } .master aside .widget-list .widget:not(:first-of-type) { margin-top: 10px; } .master footer { background: #808080; min-height: 500px; } ul { margin: 0; padding: 0; list-style: none; } /** additional Css For Sidebar Logic **/ .master aside .widget-list.fixed { position: fixed; } .master aside .widget-list.bottom { position: absolute; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='master'> <div class='inner-w'> <header> <h1>Attempts at a sticky sidebar for squishy and more-complex flex-Box layouts - sorta ~ eh?</h1> </header> <main> <div class='content'> content </div> <aside class=""> <ul class='widget-list natural'> <li class='widget'> widget 1 </li> <li class='widget'> widget 2 </li> <li class='widget'> widget 3 </li> </ul> </aside> </main> <footer> footer </footer> </div> </div>