EzyPlatform Low Code: Khởi tạo phần đầu trang (header)
Back To Blogs
Để cho đơn giản chúng ta sẽ sử dụng code từ dự án mã nguồn mở có sẵn có tên portfolio-website do bạn pawantech12 Pawan Kumavat tạo ra. Giấy phép của dự án này có ghi chú This project is Free To Use and does not contains any license. vậy nên bạn có thể sử dụng một cách tự do.
Bạn hãy clone dự án này về hoặc mở trực tiếp nó trên trình duyệt.
Cấu trúc một trang web
Như bạn đã biết một trang web thông thường sẽ có 3 phần:
- Phần đầu (header): Phần này sẽ dùng chung cho mọi trang, nó thường sẽ chứa logo, menu điều hướng.
- Phần thân (body): Phần này thì tuỳ mỗi trang sẽ có nội dung khác nhau.
- Phần chân (footer): Phần này cũng sẽ dùng cho mọi trang, nó thường chứa các thông tin của cá nhân, doanh nghiệp, thông tin liên hệ, điều khoản dịch vụ, bảo mật và bản quyền.
Trong bài này chúng ta sẽ tập trung vào việc khởi tạo phần đầu trang nhé.
Khởi tạo style và scripts dùng chung
Trước khi khởi tạo phần đầu trang chúng ta sẽ cần khởi tạo các style và scripts dung chung.
Khởi tạo style
Đầu tiên bạn cần truy cập đến menu item Các phần trang:
Ở đây bạn sẽ tìm thấy phần Styles và Scripts (Các kịch bản). Bây giờ hãy chọn Styles, trình soạn thảo sẽ hiện ra.
Bạn hãy sao chép toàn bộ mã nguồn ở phần đầu trang portfolio có sẵn vào trình soạn thảo.
Tiếp theo bạn sẽ tạo một thẻ style ở ngay sau phần mã nguồn vừa được sao chép ở trình soạn thảo, sau đó sao chép toàn bộ nội dung của tập tin style.css vào trong thẻ style mới được tạo. Ví dụ dưới đây là một phần trong trình soạn thảo của tôi:
Bạn đừng quên nhấn nút lưu lại.
Khởi tạo scripts
Bây giờ quay lại mục các phần trang, bạn hãy lựa chọn mục Scripts(Các kịch bản). Một editor khác sẽ hiện ra, bạn có thể sao chép các đoạn mã sau vào trình soạn thảo:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous" ></script> <!-- AOS library javascript link --> <script src="https://unpkg.com/aos@next/dist/aos.js"></script> <script> // initializing AOS library AOS.init({ duration: 1000, offset: 50, }); </script> <script> // htmlcss progress circular bar let htmlProgress = document.querySelector(".html-css") || {style: {}}, htmlValue = document.querySelector(".html-progress") || {}; let htmlStartValue = 0, htmlEndValue = 90, htmlspeed = 30; let progresshtml = setInterval(() => { htmlStartValue++; htmlValue.textContent = `${htmlStartValue}%`; htmlProgress.style.background = `conic-gradient(#fca61f { htmlStartValue * 3.6 }deg, #ededed 0deg)`; if (htmlStartValue == htmlEndValue) { clearInterval(progresshtml); } }, htmlspeed); // javasript progress circular bar let javascriptProgress = document.querySelector(".javascript") || {style: {}}, javascriptValue = document.querySelector(".javascript-progress") || {}; let javascriptStartValue = 0, javascriptEndValue = 75, jsspeed = 30; let progressjs = setInterval(() => { javascriptStartValue++; javascriptValue.textContent = `${javascriptStartValue}%`; javascriptProgress.style.background = `conic-gradient(#7d2ae8 { javascriptStartValue * 3.6 }deg, #ededed 0deg)`; if (javascriptStartValue == javascriptEndValue) { clearInterval(progressjs); } }, jsspeed); // php progress circular bar let phpProgress = document.querySelector(".php") || {style: {}}, phpValue = document.querySelector(".php-progress") || {}; let phpStartValue = 0, phpEndValue = 80, phpspeed = 30; let progressphp = setInterval(() => { phpStartValue++; phpValue.textContent = `${phpStartValue}%`; phpProgress.style.background = `conic-gradient(#20c997 { phpStartValue * 3.6 }deg, #ededed 0deg)`; if (phpStartValue == phpEndValue) { clearInterval(progressphp); } }, phpspeed); // reactjs progress circular bar let reactProgress = document.querySelector(".reactjs") || {style: {}}, reactValue = document.querySelector(".reactjs-progress") || {}; let reactStartValue = 0, reactEndValue = 30, rjsspeed = 30; let progressreact = setInterval(() => { reactStartValue++; reactValue.textContent = `${reactStartValue}%`; reactProgress.style.background = `conic-gradient(#3f396d { reactStartValue * 3.6 }deg, #ededed 0deg)`; if (reactStartValue == reactEndValue) { clearInterval(progressreact); } }, rjsspeed); // filter using javascript $(document).ready(function () { $(".filter-item").click(function () { const value = $(this).attr("data-filter"); if (value == "all") { $(".post").show("1000"); } else { $(".post") .not("." + value) .hide("1000"); $(".post") .filter("." + value) .show("1000"); } }); }); // javascript for sticky navbar even if u scroll the navbar will be fixed document.addEventListener("DOMContentLoaded", function(){ window.addEventListener('scroll', function() { if (window.scrollY > 50) { document.getElementById('navbar-top').classList.add('fixed-top'); // add padding top to show content behind navbar navbar_height = document.querySelector('.navbar').offsetHeight; document.body.style.paddingTop = navbar_height + 'px'; } else { document.getElementById('navbar-top').classList.remove('fixed-top'); // remove padding top from body document.body.style.paddingTop = '0'; } }); }); // adding funtionality to back to top button //Get the button let mybutton = document.getElementById("btn-back-to-top"); // When the user scrolls down 20px from the top of the document, show the button window.onscroll = function () { scrollFunction(); }; function scrollFunction() { if ( document.body.scrollTop > 20 || document.documentElement.scrollTop > 20 ) { mybutton.style.display = "block"; } else { mybutton.style.display = "none"; } } // When the user clicks on the button, scroll to the top of the document if (mybutton) { mybutton.addEventListener("click",function(){ document.body.scrollTop = 0; document.documentElement.scrollTop = 0; }); } </script>
Bạn đừng quên nhấn nút lưu lại.
Khởi tạo phần đầu trang (header)
Sau khi đã khởi tạo style và scripts dùng chung, bây giờ bạn có thể khởi tạo phần đầu trang. Bạn hãy quay trở lại Các phần trang và lựa chọn Header một trình soạn thảo sẽ hiện ra. Bạn hãy sao chép mã nguồn ở ngay sau thẻ body vào trình soạn thảo:
<!-- navbar starts --> <nav id="navbar-top" class="navbar navbar-expand-lg navbar-light bg-white sticky-top" > <div class="container-fluid px-5"> <a class="navbar-brand fw-bold fs-2" href="#" style="color: #3f396d" >Portfolio</a > <button class="nav-menu d-md-none" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" > <i class="bi bi-menu-button-fill"></i> </button> <div class="collapse navbar-collapse justify-content-center" id="navbarNav" > <ul class="navbar-nav gap-4" style="font-size: 1.1rem"> <li class="nav-item"> <a class="nav-link" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#service">Service</a> </li> <li class="nav-item"> <a class="nav-link" href="#about">About</a> </li> <li class="nav-item"> <a class="nav-link" href="#portfolio">Portfolio</a> </li> <li class="nav-item"> <a class="nav-link" href="#testimonial">Testimonials</a> </li> <li class="nav-item"> <a class="nav-link" href="#blog">Blog</a> </li> </ul> </div> <button class="c-btn ms-4 py-2 px-4 rounded-pill d-none d-md-block">Contact</button> </div> </nav> <!-- navbar ends here -->
Bạn hãy nhấn nút lưu lại, sau đó quay trở lại trình duyệt và truy cập vào web, bạn sẽ thấy giao diện mới thay đổi như sau:
Chúc mừng bạn đã khởi tạo thành công phần header.
Tổng kết
Ngày nay các giao diện đã có sẵn rất nhiều, chúng ta có thể tận dụng để làm Low code, những đoạn mã nào chúng ta thấy khó hiểu hoặc muốn tuỳ chỉnh thêm chúng ta có thể sử dụng AI. Sau đó bạn có thể thông qua trình soạn thảo để cập nhật các phần trang và nhận kết quả tương ứng.