运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * {box-sizing: border-box;} body { margin: 0; font-family: Arial, Helvetica, sans-serif; } #navbar { overflow: hidden; background-color: #f1f1f1; padding: 90px 10px; transition: 0.4s; position: fixed; width: 100%; top: 0; z-index: 99; } #navbar a { float: left; color: black; text-align: center; padding: 12px; text-decoration: none; font-size: 18px; line-height: 25px; border-radius: 4px; } #navbar #logo { font-size: 35px; font-weight: bold; transition: 0.4s; } #navbar a:hover { background-color: #ddd; color: black; } #navbar a.active { background-color: dodgerblue; color: white; } #navbar-right { float: right; } @media screen and (max-width: 580px) { #navbar { padding: 20px 10px !important; } #navbar a { float: none; display: block; text-align: left; } #navbar-right { float: none; } } </style> </head> <body> <div id="navbar"> <a href="#default" id="logo">CompanyLogo</a> <div id="navbar-right"> <a class="active" href="#home">Home</a> <a href="#contact">Contact</a> <a href="#about">About</a> </div> </div> <div style="margin-top:210px;padding:15px 15px 2500px;font-size:30px"> <p><b>This example demonstrates how to shrink a navigation bar when the user starts to scroll the page.</b></p> <p>Scroll down this frame to see the effect!</p> <p>Scroll to the top to remove the effect.</p> <p><b>Note:</b> We have also made the navbar responsive, resize the browser window to see the effect.</p> <p>Lorem ipsum dolor dummy text to enable scrolling, sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <script> // When the user scrolls down 80px from the top of the document, resize the navbar's padding and the logo's font size window.onscroll = function() {scrollFunction()}; function scrollFunction() { if (document.body.scrollTop > 80 || document.documentElement.scrollTop > 80) { document.getElementById("navbar").style.padding = "30px 10px"; document.getElementById("logo").style.fontSize = "25px"; } else { document.getElementById("navbar").style.padding = "80px 10px"; document.getElementById("logo").style.fontSize = "35px"; } } </script> </body> </html>