Skip to content

Instantly share code, notes, and snippets.

@Postnov
Last active December 22, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Postnov/b079ece946c4156720ef1ac28575eebc to your computer and use it in GitHub Desktop.
Save Postnov/b079ece946c4156720ef1ac28575eebc to your computer and use it in GitHub Desktop.
Код добавляющий класс, если выпадающее меню вылезает за границы экрана
/* .nav__sublist — класс списка, который появляется при наведении */
// Jquery
$('.nav__sublist').each(function(i, item) {
var offset = $(item).offset().left +
$(item).outerWidth();
if ($(window).width() < offset) {
$(item).addClass('position-right');
}
});
// Native javascript — не работает, найти проблему и исправить
var subMenuItems = document.querySelectorAll('.nav__sublist');
subMenuItems.forEach(function(item) {
setTimeout(function() {
var offset = item.offsetWidth + item.offsetLeft;
if (window.innerWidth < offset) {
item.classList.add('position-right');
}
}, 10);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment