BLOGTOP

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » BLOGTOP » Уголок wap мастера » JavaScript » Как создать фиксированный/липкий заголовок при прокрутке с помощью CSS


Как создать фиксированный/липкий заголовок при прокрутке с помощью CSS

Сообщений 1 страница 1 из 1

1

[html]<div class="header" id="myHeader">
  <h2>Липкий заголовок</h2>
</div>
<style>
/* Style the header */
.header {
  padding: 10px 16px;
  background: #555;
  color: #f1f1f1;
}

/* Page content */
.content {
  padding: 16px;
}

/* The sticky class is added to the header with JS when it reaches its scroll position */
.sticky {
  position: fixed;
  top: 0;
  width: 100%
}

/* Add some top padding to the page content to prevent sudden quick movement (as the header gets a new position at the top of the page (position:fixed and top:0) */
.sticky + .content {
  padding-top: 102px;
}
</style>
<script>
// When the user scrolls the page, execute myFunction
window.onscroll = function() {myFunction()};

// Get the header
var header = document.getElementById("myHeader");

// Get the offset position of the navbar
var sticky = header.offsetTop;

// Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
  if (window.pageYOffset >= sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}
</script>
[/html]

Шаг 1) добавить HTML:

Код:
<div class="header" id="myHeader">
  <h2>My Header</h2>
</div>

Шаг 2) добавить CSS:

Код:
Как сделать - фиксированный/липкий заголовок
Узнайте, как создать фиксированный/липкий заголовок при прокрутке с помощью CSS и JavaScript.


Как создать фиксированный заголовок при прокрутке
Шаг 1) добавить HTML:
Пример
<div class="header" id="myHeader">
  <h2>My Header</h2>
</div>
Шаг 2) добавить CSS:
Пример
/* Style the header */
.header {
  padding: 10px 16px;
  background: #555;
  color: #f1f1f1;
}

/* Page content */
.content {
  padding: 16px;
}

/* The sticky class is added to the header with JS when it reaches its scroll position */
.sticky {
  position: fixed;
  top: 0;
  width: 100%
}

/* Add some top padding to the page content to prevent sudden quick movement (as the header gets a new position at the top of the page (position:fixed and top:0) */
.sticky + .content {
  padding-top: 102px;
}

Шаг 3) добавить JavaScript:

Код:
// When the user scrolls the page, execute myFunction
window.onscroll = function() {myFunction()};

// Get the header
var header = document.getElementById("myHeader");

// Get the offset position of the navbar
var sticky = header.offsetTop;

// Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
  if (window.pageYOffset >= sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}
Подпись автора

вопрос по ◆ВТ◆ в личку

вопрос по ◆ВТ◆ в тему

0

Быстрый ответ

Напишите ваше сообщение и нажмите «Отправить»


упс таблица

Откуда гость?
ВНИМАНИЕ! В подписи запрещены ссылки на конкурирующие сайты или форумы!


Вы здесь » BLOGTOP » Уголок wap мастера » JavaScript » Как создать фиксированный/липкий заголовок при прокрутке с помощью CSS