<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery | Scroll Fix Snippet (sticky nav)</title> <!-- https://codepen.io/tobiasdev/pen/rrqpBg --> <style type="text/css"> /* ===================== ===================== Sticky styles ===================== ===================== */ .sticky { transition: ease .3s; } .sticky-pin { position: fixed; top: 0; } /* ===================== ===================== Optional styles ===================== ===================== */ /* Reset */ * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } :root { font-family: 'Roboto Slab', serif; color: #333333; } html, body { margin: 0; padding: 0; } /* Tuning */ header { /*height: 100vh;*/ /* Uncomment for full height header */ padding: 110px 20px; background: #4527A0; color: #ffffff; text-align: center; } hr { width: 30px; border-color: #EC407A; } nav { width: 100%; padding: 20px 0; background: #7C4DFF; color: #ffffff; } nav p { font-size: 1.2em; text-align: center; } section { width: 80%; max-width: 680px; margin: 100px auto; } h2 { color: #EC407A; } p { line-height: 1.4em; text-align: justify; } .sticky-pin { background: #ffffff; color: #7C4DFF; box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23); } </style> </head> <body> <header> <h1>Just Scroll</h1> <hr> </header> <nav class="sticky"> <p>Awesome sticky element!</p> </nav> <section> <h2>Lorem ipsum dolor sit amet.</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vel vitae quae eos modi, magni nemo vero dolores sequi corporis minima libero, voluptatum tenetur eveniet molestias illo aut officia laborum facere illum. Porro voluptatum tempore commodi provident magni, accusantium. Sequi, quam!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Provident accusamus neque beatae odit, cupiditate totam dolor eos odio cumque ipsa nemo quaerat sapiente minima aliquid ipsum libero, explicabo veritatis! Earum explicabo vel ab architecto, incidunt, dicta, voluptatibus iure, tempora ea perspiciatis provident! Veritatis veniam quis distinctio eligendi sunt debitis nam, dolorem facilis, necessitatibus omnis corporis quisquam ad autem quo suscipit repudiandae explicabo illum reiciendis enim atque excepturi. Aspernatur nam maxime odio laborum officia reprehenderit, et blanditiis praesentium fugit alias, illo quod animi consectetur ducimus sapiente sequi. Culpa quae doloribus dolore inventore assumenda perspiciatis, neque dicta provident, sit, saepe voluptate! Neque modi cum, doloremque molestias libero voluptatibus recusandae optio autem nisi culpa ipsa placeat perferendis sed tempore nulla natus amet quas saepe, dolor vitae rem. Et totam eius perspiciatis minima rem ipsam! Quia doloribus cum soluta quae, alias error ratione molestias.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et molestiae ducimus totam cumque repudiandae facere, ex magni odio pariatur sequi id officiis maiores? In, sunt! Veritatis suscipit tempore cupiditate eligendi saepe illo velit. Deleniti ratione ea sequi mollitia, blanditiis at nisi vel, voluptates voluptate fugiat atque repellendus eum. Fuga velit unde qui eligendi quam ipsa quibusdam a laborum ducimus et!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nihil odio fugiat ratione quasi adipisci, minima nemo provident quia nesciunt aperiam saepe quidem sequi neque voluptas. Ab numquam, obcaecati magnam eos, doloribus laborum. Voluptatum dolorem nostrum tempore libero doloremque mollitia recusandae quis alias possimus quisquam! Velit, ut temporibus voluptatibus amet assumenda. Provident laborum dolor repudiandae modi quam ratione et laudantium ad repellendus, rem eligendi illo expedita. Cumque dolor eos beatae voluptatibus, omnis rem velit. Consequatur deserunt enim provident, dolores blanditiis qui voluptatibus, quisquam pariatur sint non reiciendis sunt quidem sed neque repellendus iure. Ad, iure laudantium laborum at fuga maxime facilis neque. Fugiat non temporibus blanditiis in tenetur vitae totam, a soluta laudantium sapiente nihil nesciunt, ipsum, velit! Adipisci, perferendis.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi eius suscipit commodi, nemo natus dolorem impedit blanditiis. Distinctio, similique, voluptatum!</p> </section> <!-- Google Fonts --> <!-- <link href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400,700" rel="stylesheet"> --> </body> <script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> /* =============================================================== Hi! Welcome to my little playground! My name is Tobias Bogliolo. 'Open source' by default and always 'responsive', I'm a publicist, visual designer and frontend developer based in Barcelona. Here you will find some of my personal experiments. Sometimes usefull, sometimes simply for fun. You are free to use them for whatever you want but I would appreciate an attribution from my work. I hope you enjoy it. =============================================================== */ $(document).ready(function(){ var stickyElement = $(".sticky"), stickyClass = "sticky-pin", stickyPos = stickyElement.offset().top, //Distance from the top of the window. stickyHeight; //Create a negative margin to prevent content 'jumps': stickyElement.after('<div class="jumps-prevent"></div>'); function jumpsPrevent() { stickyHeight = stickyElement.innerHeight(); stickyElement.css({"margin-bottom":"-" + stickyHeight + "px"}); stickyElement.next().css({"padding-top": + stickyHeight + "px"}); }; jumpsPrevent(); //Run. //Function trigger: $(window).resize(function(){ jumpsPrevent(); }); //Sticker function: function stickerFn() { var winTop = $(this).scrollTop(); //Check element position: winTop >= stickyPos ? stickyElement.addClass(stickyClass): stickyElement.removeClass(stickyClass) //Boolean class switcher. }; stickerFn(); //Run. //Function trigger: $(window).scroll(function(){ stickerFn(); }); }); </script> </html> 提示:你可以先修改部分代码再运行。 转载请注明:有爱前端 » 滚动悬停 喜欢 (1)or分享 (0)