20/05/2024 | 19:02

Tinh dầu bôi trơn toàn thân

Certainly! Here's a basic example of how you can create a sticky header for a website using HTML, CSS, and JavaScript:

HTML:

```html

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Sticky Header</title>

  <link rel="stylesheet" href="styles.css">

</head>

<body>

<header class="sticky-header">

  <h1>My Sticky Header</h1>

</header>

<main>

  <!-- Your website content goes here -->

</main>

<script src="script.js"></script>

</body>

</html>

```

CSS (styles.css):

```css

body {

  margin: 0;

  font-family: Arial, sans-serif;

}

.sticky-header {

  background-color: #333;

  color: #fff;

  padding: 20px;

  text-align: center;

  position: fixed;

  top: 0;

  width: 100%;

  z-index: 1000; /* To make sure the header stays on top */

}

/* Placeholder styles for main content */

main {

  padding: 20px;

  margin-top: 80px; /* Adjust according to header height */

}

```

JavaScript (script.js):

```javascript

window.onscroll = function() {stickyHeader()};

var header = document.querySelector('.sticky-header');

var sticky = header.offsetTop;

function stickyHeader() {

  if (window.pageYOffset > sticky) {

    header.classList.add('sticky');

  } else {

    header.classList.remove('sticky');

  }

}

```

In this example:

- We create a basic HTML structure with a `<header>` for the sticky header and a `<main>` for the website content.

- CSS is used to style the header and create a basic layout. The `.sticky-header` class has `position: fixed` to make it stick to the top of the viewport.

- JavaScript is used to add a `sticky` class to the header when the user scrolls past a certain point (`offsetTop` of the header), and remove it when they scroll back up. This `sticky` class can be used to apply additional styling if needed.

Make sure to adjust styles and JavaScript logic according to your specific design and requirements.

4.9/5 (62 votes)

 
 
 
nNgọc Ngân
Giá rẻ, sản phẩm chất lượng, mình rất thích!
Reply
Cẩm TúAdmin

Dạ, cảm ơn Chị đã phải hồi, hệ thống luôn lựa chọn những sản phẩm tốt nhất để giúp khách hàng mua được hàng tốt.

tThanh Hải
Có ship hàng toàn quốc không nhỉ?
Reply
Cẩm TúAdmin

Có nhé, Anh yên tâm đặt hàng online ạ, nhận được hàng mới thanh toán tiền. Ngoài ra được đổi/trả hàng miễn phí trong 7 - 15 ngày

tTrâm Anh
Chất lượng tốt, sẽ giới thiệu cho bạn bè.
Reply
Cẩm TúAdmin

Thanks Chị, website bên em luôn cập nhật những sản phẩm bán chạy nhất đề xuất cho người mua nên rất uy tín.

mMỹ Duyên
Tuyệt vời, mới đặt hàng hôm qua mà hôm nay đã nhận được hàng.
Reply
Cẩm TúAdmin

Dạ, tùy vào từng khu vực xa hay gần, nhưng thường không quá 2 ngày là nhận được hàng

pPhương Linh
Có nhiều sản phẩm để lựa chọn, giá tốt!
Reply
Cẩm TúAdmin

Cảm ơn bạn, đừng quên thường xuyên ghé thăm website để xem những sản phẩm mới nhất nhé.