Dẫn nhập
Ở bài trước, chúng ta đã áp dụng jquery để code phần STICK NAVIGATION VÀ HIỆU ỨNG SCROLL CHO WEB LANDING
PAGE .
Trong bài này, Kteam sẽ tiếp tục hướng dẫn bạn sử dụng jquery để code
Mobile navigation .
Nội dung
Để đọc hiểu bài này tốt nhất, bạn nên xem qua các nội dung:
Trong bài này, Kteam sẽ cùng bạn tìm hiểu nội dung:
Code Stick Navigation Hiệu ứng Scroll
Code Mobile navigation
Truy cập Fontawesome.com > tìm bar và chọn mẫu bạn thích
Index.html
<!DOCTYPE html>
<html >
<head >
<title > Selftaught</title >
<link rel ="stylesheet" href ="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity ="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz"
crossorigin ="anonymous" >
<link href ="https://fonts.googleapis.com/css?family=Lato:300,400,700,900" rel ="stylesheet" >
<link rel ="stylesheet" href ="vendors/css/grid.css" >
<link rel ="stylesheet" type ="text/css" href ="resources/css/styles.css" />
<link rel ="stylesheet" href ="resources/css/queries.css" >
</head >
<body >
<header id ="home" >
<nav >
<a href ="#home" > <img class ="logo" src ="./resources/img/logo.png" alt ="logo" > </a >
<ul class ="main-nav" >
<li > <a href ="#testimonials" > How Clients Think</a > </li >
<li > <a href ="#about" > About</a > </li >
<li > <a href ="#products" > Products</a > </li >
<li > <a href ="#contact" > Contact</a > </li >
</ul >
<i class ="fas fa-bars mobile-nav-icon" > </i >
</nav >
<div class ="clearfix" > </div >
<div class ="row" >
<div class ="heading-main-box" >
<h1 >
We will study <br > English together.<br >
So join with us.
</h1 >
<a href ="#about" class ="btn" > Tell me more</a >
<a href ="#products" class ="btn" > Join us</a >
</div >
</div >
</header >
<section class ="about-section" id ="about" >
<div class ="row" >
<h2 > About Us</h2 >
<p class ="p-long" >
We are people that are studying English by ourselves, we believe that you don’t need to pay so much
money for being good at English, we’re here to help you
</p >
</div >
<div class ="row" >
<div class ="col span-1-of-4 about-picture" >
<img src ="./resources/img/activities1.jpeg" alt ="activities1" >
<p class ="picture-title" >
Team Working
</p >
</div >
<div class ="col span-1-of-4 about-picture" >
<img src ="./resources/img/activities2.jpeg" alt ="activities2" >
<p class ="picture-title" >
Funny class
</p >
</div >
<div class ="col span-1-of-4 about-picture" >
<img src ="./resources/img/activities3.jpeg" alt ="activities3" >
<p class ="picture-title" >
Be confident
</p >
</div >
<div class ="col span-1-of-4 about-picture" >
<img src ="./resources/img/activities4.jpeg" alt ="activities4" >
<p class ="picture-title" >
All our friends
</p >
</div >
</div >
</section >
<section class ="products-section" id ="products" >
<h2 > Products</h2 >
<div class ="row" >
<div class ="col span-1-of-3" >
<div class ="product-box" >
<h3 > Pronunciation class</h3 >
<p class ="product-price" > 19$ <span > / month</span > </p >
<ul >
<li > <i class ="fas fa-check small-icon" > </i > Native voices</li >
<li > <i class ="fas fa-check small-icon" > </i > Master IPA</li >
</ul >
<a href ="#" class ="btn" > JOIN US</a >
</div >
</div >
<div class ="col span-1-of-3" >
<div class ="product-box" >
<h3 > Speaking class</h3 >
<p class ="product-price" > 25$ <span > / month</span > </p >
<ul >
<li > <i class ="fas fa-check small-icon" > </i > Native speaking</li >
<li > <i class ="fas fa-check small-icon" > </i > Speaing naturally</li >
</ul >
<a href ="#" class ="btn" > JOIN US</a >
</div >
</div >
<div class ="col span-1-of-3" >
<div class ="product-box" >
<h3 > Listening class</h3 >
<p class ="product-price" > 25$ <span > / month</span > </p >
<ul >
<li > <i class ="fas fa-check small-icon" > </i > Native speech</li >
<li > <i class ="fas fa-check small-icon" > </i > Hearing English clearly</li >
</ul >
<a href ="#" class ="btn" > JOIN US</a >
</div >
</div >
</div >
</section >
<section class ="testimonials-section" id ="testimonials" >
<h2 > Testimonials</h2 >
<div class ="row" >
<div class ="col span-1-of-3" >
<blockquote >
The course is very helpful, I can speak English fluently and I think The SF English became my second family, I love everyone very much. Thank you!
<cite >
<img src ="resources/img/person5.jpeg" alt ="person1" > Nguyen Chem Gio
</cite >
</blockquote >
</div >
<div class ="col span-1-of-3" >
<blockquote >
I believe that who came to This English centre would love this place as I did, when the course finished, I feel sad because studying in that place I always felt funny.
<cite >
<img src ="resources/img/person2.jpeg" alt ="person1" > Tran Vo Van
</cite >
</blockquote >
</div >
<div class ="col span-1-of-3" >
<blockquote >
Trust me, you can master English just by joining this class, I did, so I believe that you can too. Hope that one day, you can come and join in this class.
<cite >
<img src ="resources/img/person4.jpeg" alt ="person1" > Le Du Do
</cite >
</blockquote >
</div >
</div >
</section >
<section class ="sponsors-section" id ="sponsors" >
<h2 > Sponsors</h2 >
<div class ="row" >
<ul class ="sponsors-showcase clearfix" >
<li >
<figure >
<img class ="logo-sponsor" src ="resources/img/barcelona_logo.png" alt ="barcelona" >
<figcaption >
Barcelona
</figcaption >
</figure >
</li >
<li >
<figure >
<img class ="logo-sponsor" src ="resources/img/mctcity_logo.png" alt ="manchester city" >
<figcaption >
Manchester City
</figcaption >
</figure >
</li >
<li >
<figure >
<img class ="logo-sponsor" src ="resources/img/tottenham_logo.png" alt ="tottenham hotspur" >
<figcaption >
Tottenham Hotspur
</figcaption >
</figure >
</li >
</ul >
<ul class ="sponsors-showcase clearfix" >
<li >
<figure >
<img class ="logo-sponsor" src="resources /img /real_logo.png " alt ="real marid" >
<figcaption >
Real Marid
</figcaption >
</figure >
</li >
<li >
<figure >
<img class ="logo-sponsor" src ="resources/img/juventus_logo.png" alt ="Juventus" >
<figcaption >
Juventus
</figcaption >
</figure >
</li >
<li >
<figure >
<img class ="logo-sponsor" src ="resources/img/mctunited_logo.png" alt ="manchester united" >
<figcaption >
Manchester United
</figcaption >
</figure >
</li >
</ul >
</div >
</section >
<section class ="contact-section" id ="contact" >
<div class ="row" >
<div class ="col span-1-of-3" >
<ul class ="information" >
<li > <i class ="fas fa-map-marker-alt small-icon" > </i > Address: 21A, SelfTaught street, Ha Noi</li >
<li > <i class ="fas fa-envelope small-icon" > </i > Email: selftaughtteam@edu.com</li >
<li > <i class ="fas fa-phone small-icon" > </i > SĐT: (+084 )099-923-232-320</li >
</ul >
<ul class ="social-icons" >
<li > <i class ="fab fa-facebook" > </i > </li >
<li > <i class ="fab fa-twitter-square" > </i > </li >
<li > <i class ="fab fa-instagram" > </i > </li >
<li > <i class ="fab fa-google-plus-square" > </i > </li >
</ul >
</div >
<div class ="col span-2-of-3" >
<form >
<div class ="row" >
<div class ="col span-1-of-3" >
<label > Name</label >
</div >
<div class ="col span-2-of-3" >
<input type ="text" placeholder ="Your Name" >
</div >
</div >
<div class ="row" >
<div class ="col span-1-of-3" >
<label > Email</label >
</div >
<div class ="col span-2-of-3" >
<input type ="email" placeholder ="Your Email" >
</div >
</div >
<div class ="row" >
<div class ="col span-1-of-3" >
<label > Message</label >
</div >
<div class ="col span-2-of-3" >
<textarea placeholder ="Your Message" > </textarea >
</div >
</div >
<div class ="row" >
<input type ="submit" class ="btn" value ="Send It" >
</div >
</form >
</div >
</div >
</section >
<footer >
<p >
Copyright © 2018 by SELFTAUGHT TEAM
</p >
</footer >
<script
src ="https://code.jquery.com/jquery-3.3.1.js"
integrity ="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin ="anonymous" > </script >
<script src ="vendors/js/jquery.waypoints.min.js" > </script >
<script src ="resources/js/scripts.js" > </script >
</body >
</html >
Style.css
*{
padding: 0 px;
margin: 0 px;
box-sizing: border-box;
}
html{
font-family: 'Lato' , sans-serif;
font-size: 20 px;
color: #5a5a5a;
background-color: #ffffff;
text-rendering: optimizeLegibility;
}
body{
overflow-x: hidden;
}
.btn{
text-decoration: none;
color: white;
background-color: #e67e22;
padding: 15 px 30 px;
border-radius: 20 px;
margin: 0 5 px;
width: 190 px;
display: inline-block;
text-align: center;
transition: all .3 s;
}
.btn:hover,
.btn:active{
background-color: #d35400;
}
.clearfix::after{
content: " " ;
display: table;
clear: both;
}
.row{
max-width: 1140 px;
margin: 0 auto;
}
h1, h2, h3{
text-transform: uppercase;
letter-spacing: 1 px;
}
h1{
font-size: 300 %;
font-weight: 400 ;
word-spacing: 5 px;
color: white;
line-height: 135 %;
margin-bottom: 50 px;
}
h2{
color: #e67e22;
font-size: 180 %;
font-weight: 700 ;
text-align: center;
}
h2:after{
content: " " ;
display: block;
width: 150 px;
height: 3 px;
background-color: rgba(230 , 126 , 34 ,0.5 );
margin: 0 auto;
margin-top: 5 px;
}
h3{
padding-top: 30 px;
padding-bottom: 10 px;
text-align: center;
background-color: #265077;
color: white;
border-top-right-radius: 15 px;
border-top-left-radius: 15 px;
margin-bottom: 15 px;
}
.p-long {
text-align: center;
color: #265077;
font-size: 22 px;
margin-top: 40 px;
margin-bottom: 40 px;
width: 70 %;
margin-left: 15 %;
line-height: 140 %;
}
section{
padding: 50 px;
}
.small-icon{
color: #305679;
font-size: 130 %;
margin-right: 10 px;
}
header{
background-image: linear-gradient(45 deg,rgba(2 ,33 ,64 , 0.8 ) 55 %,rgba(45 ,95 ,93 ,0.85 )), url("../img/background4.jpeg" );
background-size: cover;
background-
height: 100 vh;
background-attachment: fixed ;
}
.logo{
width: 350 px;
float : left;
margin-left: 70 px;
margin-top: -50 px;
margin-bottom: -100 px;
}
.main-nav{
float : right;
list-style: none;
margin-right: 50 px;
margin-top: 55 px;
}
.main-nav li{
display: inline-block;
margin-right: 30 px;
}
.main-nav li a{
text-decoration: none;
color: white;
font-size: 110 %;
font-weight: 700 ;
transition: border-bottom 0.5 s;
padding: 3 px 0 ;
}
.main-nav li a:hover,
.main-nav li a:active{
border-bottom: 2 px solid #cccccc;
}
.heading-main-box{
top: 50 %;
left: 50 %;
transform: translate(-45 %, -55 %);
width: 1140 px;
}
.sticky{
background-color: rgba(2 ,33 ,64 ,.9 );
top: 0 ;
left: 0 ;
width: 100 %;
padding-bottom: 10 px;
}
.sticky .logo{
width: 250 px;
margin-top: -40 px;
margin-bottom: -70 px;
}
.sticky .main-nav{
margin-top: 30 px;
}
.mobile-nav-icon{
font-size: 200 %;
color: #6b95bd;
top: 55 px;
right: 100 px;
display: none;
}
.sticky .mobile-nav-icon{
top: 30 px;
}
.about-picture{
margin-top: 50 px;
}
.about-picture img{
width: 90 %;
border-radius: 20 px;
border: 4 px solid #c2e1fd;
box-shadow: 0 px 5 px 15 px 5 px #265077 ;
transition: all .7 ;
}
.picture-title{
top: 50 %;
left: 50 %;
color: #c2e1fd;
transform: translate(-65 %,100 %);
opacity: 0 ;
transition: all .7 s;
}
.about-picture:hover img,
.about-picture:active img{
filter: brightness(55 %) blur(2 px);
}
.about-picture:hover .picture-title,
.about-picture:active .picture-title{
transform: translate(-65 %, -50 %);
opacity: 1 ;
}
.products-section{
background-color: #f4f4f4;
}
.products-section h2{
margin-bottom: 60 px;
}
.product-box{
background-color: #eeefff;
box-shadow: 3 px 5 px 10 px #e0e0e0;
width: 90 %;
height: 350 px;
}
.product-price{
color: #265077;
font-size: 250 %;
font-weight: 300 ;
text-align: center;
}
.product-price span {
font-size: 80 %;
}
.product-box ul li{
margin-left: 20 %;
list-style: none;
margin-top: 20 px;
margin-bottom: 20 px;
}
.product-box .btn{
margin-left: 20 %;
}
.testimonials-section{
background-image:linear-gradient(45 deg,rgba(2 ,33 ,64 , 0.8 ) 55 %,rgba(45 ,95 ,93 ,0.85 )), url('../img/background2.jpeg' );
background-
background-size: cover;
background-attachment: fixed ;
}
.testimonials-section h2{
margin-bottom: 70 px;
}
blockquote{
width: 95 %;
font-style: italic;
line-height: 140 %;
color: white;
}
blockquote:before{
content: '\201C' ;
display: block;
font-size: 400 %;
top: -10 px;
left: -10 px;
}
cite{
display: block;
color: #6b95bd;
margin-top: 25 px;
}
cite img{
height: 50 px;
border-radius: 50 %;
border: 2 px solid #265077;
vertical-align: middle;
margin-right: 5 px;
}
.sponsors-section{
background-color: #f4f4f4;
}
.sponsors-section h2{
margin-bottom: 50 px;
}
.sponsors-showcase{
margin-left: 8 %;
}
.sponsors-showcase li{
list-style: none;
float : left;
width: 30 %;
text-align: center;
margin-bottom: 20 px;
}
.logo-sponsor{
width: 140 px;
cursor: pointer;
transition: all .3 s;
}
.sponsors-showcase li .logo-sponsor:hover,
.sponsors-showcase li .logo-sponsor:active{
transform: scale(1.2 );
}
figcaption{
color: #265077;
font-weight: 900 ;
font-size: 120 %;
cursor: pointer;
}
.contact-section{
background-image: linear-gradient(45 deg,rgba(2 ,33 ,64 , 0.8 ) 55 %,rgba(45 ,95 ,93 ,0.85 )),url('../img/background5.jpeg' );
background-
background-size: cover;
background-attachment: fixed ;
}
footer{
text-align: center;
background-color: #494949;
color: white;
padding: 20 px;
}
.information{
list-style: none;
color: white;
line-height: 200 %;
margin-top: 70 px;
word-spacing: 1 px;
font-size: 95 %;
}
.information .small-icon{
color: #427db4
}
.information .small-icon:hover,
.information .small-icon:active{
transform: scale(1.3 );
}
.social-icons{
margin-top: 50 px;
list-style: none;
}
.social-icons li{
display: inline-block;
font-size: 250 %;
margin-left: 20 px;
color: white;
}
.social-icons li i{
transition: all .3 s;
cursor: pointer;
}
.social-icons li i:hover,
.social-icons li i:active{
transform: scale(1.3 );
color: #e67e22;
}
form{
background-color: rgba(0 , 0 , 0 , .4 );
margin-left: 40 %;
width: 68 %;
padding: 30 px;
margin-bottom: 30 px;
}
form .row{
margin-bottom: 20 px;
}
label{
font-size: 150 %;
color: white;
}
input, textarea{
outline: none;
padding: 10 px;
background-color: rgba(48 , 47 , 47 , 0.3 );
border: 1 px solid rgba(70 , 68 , 68 , 0.7 );
margin-left: 30 px;
cursor: pointer;
border-radius: 5 px;
}
textarea{
resize: none;
height: 100 px;
}
input{
height: 50 px;
}
input[type=text], input[type=email], textarea{
width: 80 %;
font-size: 100 %;
transition: all .3 s;
}
input[type=text]:focus, input[type=email]:focus, textarea:focus{
color: white;
background-color: rgba(48 , 47 , 47 , 0.8 )
}
form .row:last-child{
text-align: center;
}
input[type=submit]{
font-weight: 700 ;
font-size: 100 %;
border-radius: 5 px;
width: 60 %;
}
Queries.css
@media only screen and (max-width:1200 px) {
html {
font-size : 16 px ;
}
.heading-main-box {
transform : translate( -35 %,-50 %)
}
form {
margin-left : 30 % ;
}
}
@media only screen and (max-width:1023 px) {
.logo {
width : 300 px ;
margin-left : 40 px ;
}
.main-nav {
margin-top : 40 px ;
margin-right : 15 px ;
}
.main-nav li {
margin-right : 10 px ;
}
.heading-main-box {
transform : translate(-20 %,-50 %)
}
.products-section .btn {
width : 80 % ;
margin-left : 10 % ;
}
.product-box {
width : 95 % ;
}
h3 {
font-size : 90 % ;
}
.product-box ul li {
margin-left : 5 % ;
}
.product-box .small-icon {
margin-right : 5 px ;
}
.social-icons li {
margin-left : 10 px ;
}
.information i {
display : block ;
}
.information li {
font-size : 14 px ;
}
}
@media only screen and (max-width: 767 px) {
.col {
width : 100 % ;
}
.main-nav {
display : none ;
}
.mobile-nav-icon {
display : block ;
}
.logo {
width : 350 px ;
margin-left : 12 % ;
}
h1 {
word-spacing : 2 px ;
font-size : 250 % ;
}
.heading-main-box {
transform : translate(-17 %,-50 %) ;
}
.heading-main-box .btn {
width : 15 % ;
}
.about-section .row .p-long {
font-size : 20 px ;
}
.about-picture {
text-align : center ;
}
.about-picture img {
width : 70 %
}
.picture-title {
font-size : 25 px ;
}
.product-box {
width : 70 % ;
margin-left : 20 % ;
}
.product-box ul li {
margin-left : 25 % ;
}
.testimonials-section .row .col {
margin-bottom : 50 px ;
}
figcaption {
visibility : hidden ;
}
form {
width : 80 % ;
margin-left : 10 % ;
}
label {
margin-left :35 % ;
}
.information {
margin-top : 20 px ;
text-align : center ;
}
.information li {
font-size : 20 px ;
}
.social-icons {
text-align : center ;
}
.social-icons li {
font-size : 60 px ;
margin-bottom : 20 px ;
}
}
@media only screen and (max-width: 570 px) {
.mobile-nav-icon {
right : 45 px ;
}
}
@media only screen and (max-width: 480 px) {
html {
font-size : 12 px ;
}
h1 {
font-size : 200 % ;
}
.logo {
width : 220 px ;
margin-top : 10 % ;
margin-left : 20 % ;
}
.heading-main-box {
transform : translate(-10 %, -50 %) ;
}
.heading-main-box .btn {
width : 13 % ;
}
.heading-main-box .btn {
display : block ;
margin-bottom : 20 px ;
margin-left : 2 % ;
}
.information li {
font-size : 16 px ;
}
.social-icons li {
font-size : 40 px ;
}
form {
width : 90 % ;
margin-left : 5 % ;
}
.mobile-nav-icon {
top : 100 px ;
}
.sticky .logo {
width : 200 px ;
margin-top : -25 px ;
}
}
Scripts.js
$(document).ready(
function () {
$('.about-section' ).waypoint(
function (direction) {
if (direction == "down" ){
$('nav' ).addClass('sticky' );
}else {
$('nav' ).removeClass('sticky' );
}
},{
offset: '600px'
}
)
$('a' ).click(function (event) {
$('html, body' ).animate({
scrollTop: $( $.attr(this , 'href' ) ).offset().top
}, 700 );
event.preventDefault();
});
$('.mobile-nav-icon' ).click(
function () {
$('.main-nav' ).slideToggle(200 );
if ($('.mobile-nav-icon' ).hasClass('fa-bars' )){
$('.mobile-nav-icon' ).addClass('fa-times' );
$('.mobile-nav-icon' ).removeClass('fa-bars' )
}
else {
$('.mobile-nav-icon' ).addClass('fa-bars' );
$('.mobile-nav-icon' ).removeClass('fa-times' )
}
}
)
}
)
Kết luận
Trong bài học này chúng ta đã hoàn thiện phần mobile navigation, cũng như hoàn thiện website của chúng ta
Ở bài học sau Kteam sẽ hướng dẫn bạn cách TỐI ƯU HÓA WEBSITE
Cảm ơn các bạn đã theo dõi bài viết. Hãy để lại bình luận và góp ý của mình để giúp phát triển bài viết tốt hơn. Đừng quên:
"Luyện tập – Thử thách – Không ngại khó" .
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Code Mobile navigation dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Project
Nếu việc thực hành theo hướng dẫn không diễn ra suôn sẻ như mong muốn. Bạn cũng có thể tải xuống PROJECT THAM KHẢO ở link bên dưới!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
Ad hướng dẫn em code đoạn click vô mobile-nav-icon để chuyển đổi qua lại giữa fa-bars và fa-times với...em code mà click vô nó không đổi...em code theo code của ad dẫn rồi ạ:
$('.mobile-nav-icon').click(
function(){
$('.main-nav').slideToggle(200);
//của main-nav
if ($('.mobile-nav-icon').hasClass(fa-bars)){
$('.mobile-nav-icon').addClass(fa-times);
$('.mobile-nav-icon').removeClass(fa-bars);
}
else{
$('.mobile-nav-icon').removeClass(fa-times);
$('.mobile-nav-icon').addClass(fa-bars);
}
}
)