Last active
December 22, 2019 12:12
-
-
Save Postnov/b079ece946c4156720ef1ac28575eebc to your computer and use it in GitHub Desktop.
Код добавляющий класс, если выпадающее меню вылезает за границы экрана
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* .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