<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style> body { margin: 0; font-size: 28px; } .header { background-color: #f1f1f1; padding: 30px; text-align: center; } .topnav { overflow: hidden; background-color: #333; } .topnav a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } .topnav a:hover { background-color: #ddd; color: black; } .topnav a.active { background-color: #4CAF50; color: white; } .topnav .icon { display: none; } .content { padding: 16px; height: 1500px; } .sticky { position: fixed; top: 0; width: 100%; } .sticky + .content { padding-top: 64px; } @media screen and (max-width: 600px) { .topnav a { padding: 14px 16px; } .topnav a:not(:first-child) { display: none; } .topnav a.icon { float: right; display: block; } } @media screen and (max-width: 600px) { .topnav.responsive {position: absulute;} .topnav.responsive .icon { position: absolute; right: 0; top: 0; } .topnav.responsive a { float: none; display: block; text-align: left; } } </style> </head> <body> <div class="header"> <h2>Scroll Down</h2> <p>Scroll down to see the sticky effect.</p> </div> <div class="topnav" id="navbar"> <a class="active" href="javascript:void(0)">Home</a> <a href="javascript:void(0)">News</a> <a href="javascript:void(0)">Contact</a> <a href="javascript:void(0);" class="icon" onclick="myFunction2()"><i class="fa fa-bars"></i></a> </div> <div class="content"> <h3>Sticky Navigation Example</h3> </div> <script> window.onscroll = function() {myFunction()}; var navbar = document.getElementById("navbar"); var sticky = navbar.offsetTop; function myFunction() { if (window.pageYOffset >= sticky) { navbar.classList.add("sticky") } else { navbar.classList.remove("sticky"); } } function myFunction2() { //var navbar = document.getElementById("navbar"); if (navbar.className === "topnav") { navbar.className += " responsive"; } else { navbar.className = "topnav"; } } </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style> body { margin: 0; font-size: 28px; } .header { background-color: #f1f1f1; padding: 30px; text-align: center; } .topnav { overflow: hidden; background-color: #333; width: 100%; } .topnav a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } .topnav a:hover { background-color: #ddd; color: black; } .topnav a.active { background-color: #4CAF50; color: white; } .topnav .icon { display: none; } .content { padding: 16px; height: 1500px; } .sticky { padding-top: 64px; } @media screen and (max-width: 600px) { .topnav a { padding: 14px 16px; } .topnav a:not(:nth-child(-n+2) ) { display: none; } .topnav a.icon { float: right; display: block; } } @media screen and (max-width: 600px) { .topnav.responsive {position: absulute;} .topnav.responsive .icon { position: absolute; right: 0; /*top: 0;*/ } .topnav.responsive a { float: none; display: block; text-align: left; } } </style> </head> <body> <div class="header"> <h2>Scroll Down</h2> <p>Scroll down to see the sticky effect.</p> </div> <div class="topnav" id="navbar"> <a href="javascript:void(0);" class="icon" onclick="myFunction2()"> <i class="fa fa-bars"></i> </a> <a class="active" href="javascript:void(0)">Home</a> <a href="javascript:void(0)">News</a> <a href="javascript:void(0)">Contact</a> </div> <div class="content" id="content"><h3>Sticky Navigation Example</h3></div> <script> window.onscroll = function() {myFunction()}; var navbar = document.getElementById("navbar"); var sticky = navbar.offsetTop; function myFunction() { var content = document.getElementById("content"); if (window.pageYOffset >= sticky) { navbar.style.position = 'fixed'; navbar.style.top = '0'; content.classList.add("sticky"); } else { navbar.style.position = ''; content.classList.remove("sticky");; } } function myFunction2() { if (navbar.className === "topnav") { navbar.className += " responsive"; } else { navbar.className = "topnav"; } } </script> </body> </html>
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง