html - How to make a sticky navigation bar ? -
this code , hope guys me .
im trying create navigate bar stick on top of page , moves when user scroll down .
<!doctype html> <html> <title>new technology planet</title> <head> <style> header { background-image : url("pic15.jpg"); color:white; text-align:center; padding:150px; } ul{ list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; border-right:1px solid #bbb; } li.logo{ border-right: none; font-family:impact, charcoal, sans-serif; color:white; font-size:40px; margin-top: 15px; padding-left:30px; } li:last-child { border-right: none; } li { display: block; color: white; text-align: center; padding: 25px 30px; text-decoration: none; font-size:20px; } li a:hover:not(.active) { background-color: #111; } .active { background-color: #4caf50; } #section { width:350px; float:center; padding:10px; } #footer { background-image : url("pic17.jpg"); color:white; clear:both; text-align:center; padding:100px; } #header{ font-family: "comic sans ms", cursive, sans-serif; } </style> </head> <body> <header> <h1>new technology planet</h1> <p><h3>the website still under construction laith sj</h3></p> <p>teamspeak3 & websites hosting</p> <p>our goal give best can do!</p> </header> <!-- navagator start --> <nav role='navigation'> <ul> <li class="logo">newtecplanet</li> <ul style="float:right;list-style-type:none;"> <li><a class="active" href="#home">home</a></li> <li><a href="#news">news|updates</a></li> <li><a href="#contact">products</a></li> <li><a href="#login">signup | login</a></li> <li><a href="#about">about us</a></li> <li><a href="#about">contact us</a></li> </ul> </ul> </nav> <!-- navagattor end code --> <!-- --> <div id="section"> <h2>test</h2> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> </div> <!-- last part of page --> <div id="footer"> new technology planet </div> </body> </html> guys please feel free edit post if think there viewers , , hope if can edit code can works .
if need example im asking let me know , dont want post other websites dont break rules .
thank
create class sticky , give position:fixed. on scroll check window's & nav bar position assign class nav stick @ top of screen. see example below:
var topnav = document.getelementbyid("topnav"), stop = topnav.offsettop, docbody = document.documentelement || document.body.parentnode || document.body, hasoffset = window.pageyoffset !== undefined, scrolltop; window.onscroll = function(e) { scrolltop = hasoffset ? window.pageyoffset : docbody.scrolltop; if (scrolltop >= stop) { topnav.classname = 'sticky'; } else { topnav.classname = ''; } } header { background-image: url("pic15.jpg"); color: #5a4a4a; text-align: center; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; border-right: 1px solid #bbb; } li.logo { border-right: none; font-family: impact, charcoal, sans-serif; color: white; font-size: 40px; margin-top: 15px; padding-left: 30px; } li:last-child { border-right: none; } li { display: block; color: white; text-align: center; padding: 25px 30px; text-decoration: none; font-size: 20px; } li a:hover:not(.active) { background-color: #111; } .active { background-color: #4caf50; } #section { width: 350px; float: center; padding: 10px; } #footer { background-image: url("pic17.jpg"); color: white; clear: both; text-align: center; padding: 100px; } #header { font-family: "comic sans ms", cursive, sans-serif; } .sticky { position: fixed; top: 0; left: 0; width: 100%; } <header> <h1>new technology planet</h1> <p> <h3>the website still under construction laith sj</h3> </p> <p>teamspeak3 & websites hosting</p> <p>our goal give best can do!</p> </header> <!-- navagator start --> <nav role='navigation' id="topnav" class=""> <ul> <li class="logo">newtecplanet</li> <ul style="float:right;list-style-type:none;"> <li><a class="active" href="#home">home</a> </li> <li><a href="#news">news|updates</a> </li> <li><a href="#contact">products</a> </li> <li><a href="#login">signup | login</a> </li> <li><a href="#about">about us</a> </li> <li><a href="#about">contact us</a> </li> </ul> </ul> </nav> <!-- navagattor end code --> <!-- --> <div id="section"> <h2>test</h2> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p> </div> <!-- last part of page --> <div id="footer"> new technology planet </div>
Comments
Post a Comment