Sticky 粘住
使用说明 PLUGIN
比可视区域低
如果粘住区域高度小于可视区域,而且高度小于父层,当父层进入不可视区域,粘住区域会黏住可视区域顶部。演示效果中注意观察右侧。
-
<script src='https://src.axui.cn/v1.0/src/plugins/hc-sticky/hc-sticky.min.js'></script> <script type="text/javascript"> var Sticky = new hcSticky('.right', { stickTo: 'main' }); </script>
-
<style> header,footer{ height:60px; background-color: rgba(0,0,0,.04); z-index: 2; } main{ width:80%; margin:14px auto; } .left{ width:calc(100% - 214px); background-color: rgba(0,0,0,.04); float: left; } .right{ width:200px; background-color: rgba(0,0,0,.04); float: right; } section{ height:100px; background-color: rgba(0,0,0,.04); margin:10px; } </style>
-
<header></header> <main> <div class="left"> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <div class="right"> <section></section> <section></section> </div> <div class="ax-clear"></div> </main> <footer></footer>
比可视区域高
如果粘住区域高度大于可视区域,而且高度小于父层,粘住区域会随父层滚动直到粘住区域底部,而后会粘住可视区域底部,直到父层全部显示完毕。演示效果中注意观察右侧。
-
<script src='https://src.axui.cn/v1.0/src/plugins/hc-sticky/hc-sticky.min.js'></script> <script type="text/javascript"> var Sticky = new hcSticky('.right', { stickTo: 'main' }); </script>
-
<style> header,footer{ height:60px; background-color: rgba(0,0,0,.04); z-index: 2; } main{ width:80%; margin:14px auto; } .left{ width:calc(100% - 214px); background-color: rgba(0,0,0,.04); float: left; } .right{ width:200px; background-color: rgba(0,0,0,.04); float: right; } section{ height:100px; background-color: rgba(0,0,0,.04); margin:10px; } </style>
-
<header></header> <main> <div class="left"> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <div class="right"> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <div class="ax-clear"></div> </main> <footer></footer>
外部固定顶部
-
<script src='https://src.axui.cn/v1.0/src/plugins/sticky/jquery.sticky.min.js'></script> <script type="text/javascript"> $(window).load(function(){ $("nav").sticky({ topSpacing: 0 }); }); </script>
-
<style> header,footer{ height:60px; background-color: rgba(0,0,0,.04); z-index: 2; } header{ margin-bottom:14px; } nav{ margin-bottom:14px; height:60px; background-color: #ccc; } main{ width:80%; margin:14px auto; } .left{ width:calc(100% - 214px); background-color: rgba(0,0,0,.04); float: left; } .right{ width:200px; background-color: rgba(0,0,0,.04); float: right; } section{ height:100px; background-color: rgba(0,0,0,.04); margin:10px; } </style>
-
<header></header> <nav></nav> <main> <div class="left"> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <div class="right"> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <div class="ax-clear"></div> </main> <footer></footer>
内部固定顶部
与上例类似,注意使用widthFromWrapper:false
属性,该属性默认是true
,也就是宽度跟随父层的宽度。请观摩演示效果。
-
<script src='https://src.axui.cn/v1.0/src/plugins/sticky/jquery.sticky.min.js'></script> <script type="text/javascript"> $(window).load(function(){ $(".sticky").sticky({ topSpacing: 0,widthFromWrapper:false }); }); </script>
-
<style> header,footer{ height:60px; background-color: rgba(0,0,0,.04); z-index: 2; } main{ width:80%; margin:14px auto; } .left{ width:calc(100% - 214px); background-color: rgba(0,0,0,.04); float: left; } .right{ width:200px; background-color: rgba(0,0,0,.04); float: right; } section{ height:100px; background-color: rgba(0,0,0,.04); margin:10px; } .sticky{ background-color: #ccc; } </style>
-
<header></header> <main> <div class="left"> <section></section> <section></section> <section class="sticky"></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <div class="right"> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <div class="ax-clear"></div> </main> <footer></footer>