Cách THỰC HIỆN - Menu Điều hướng trên Điện thoại di động
Tìm hiểu cách tạo menu điều hướng trên cùng cho điện thoại thông minh / máy tính bảng bằng CSS và JavaScript.
Thanh điều hướng di động
Dọc ( khuyến nghị ):
Nằm ngang:
Tạo menu điều hướng trên thiết bị di động
Bước 1) Thêm HTML:
Thí dụ
<!-- Load an icon library to show a hamburger menu (bars) on small screens -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Top Navigation Menu -->
<div class="topnav">
<a href="#home"
class="active">Logo</a>
<!-- Navigation links (hidden by default)
-->
<div id="myLinks">
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
<!-- "Hamburger menu" / "Bar icon" to toggle the navigation
links -->
<a href="#;" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
Bước 2) Thêm CSS:
Thí dụ
/* Style the navigation menu */
.topnav {
overflow: hidden;
background-color: #333;
position: relative;
}
/* Hide the
links inside the navigation menu (except for logo/home) */
.topnav #myLinks {
display: none;
}
/* Style navigation menu links */
.topnav a {
color: white;
padding: 14px 16px;
text-decoration: none;
font-size:
17px;
display: block;
}
/* Style the hamburger menu */
.topnav a.icon {
background: black;
display: block;
position: absolute;
right: 0;
top: 0;
}
/* Add a grey background color on
mouse-over */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Style the
active link (or home/logo) */
.active {
background-color: #04AA6D;
color: white;
}
Bước 3) Thêm JavaScript:
Thí dụ
/* Toggle between showing and hiding the navigation menu links when
the user clicks on the hamburger menu / bar icon */
function myFunction() {
var x =
document.getElementById("myLinks");
if (x.style.display === "block")
{
x.style.display = "none";
} else {
x.style.display = "block";
}
}
Mẹo: Để tạo thanh điều hướng đáp ứng, hoạt động trên tất cả các thiết bị, hãy đọc Hướng dẫn Cách thực hiện - Điều hướng hàng đầu thích ứng của chúng tôi .
Mẹo: Đi tới Hướng dẫn CSS Navbar của chúng tôi để tìm hiểu thêm về các thanh điều hướng.