uliyanov_maks
@uliyanov_maks
Новичок

Как объединить два javaскрипта?

Привет!
Можно ли как-то объединить эти скрипты в один?

Подправил.

const targetNavbarDesktop = document.querySelector('.targetNavbarDesktop');
const fixedNavbarDesktop = document.querySelector('.fixedNavbar');
const obFixedNavbarDesktop = new IntersectionObserver(obCallbackDesktop);

function obCallbackDesktop(payload) {
	if (document.documentElement.clientWidth > 992) {
		if (payload[0].boundingClientRect.y < 0) {
			document.querySelector('class1').style.marginBottom = fixedNavbarDesktop.offsetHeight + 'px';
			fixedNavbarDesktop.classList.add('class2');
		} else {
			fixedNavbarDesktop.classList.remove('class2');
			document.querySelector('class1').style.marginBottom = '0';
		}
	}
}
obFixedNavbarDesktop.observe(targetNavbarDesktop);


И

const targetNavbarMobile = document.querySelector('.targetNavbarMobile');
const fixedNavbarMobile = document.querySelector('.fixedNavbar');
const obFixedNavbarMobile = new IntersectionObserver(obCallbackMobile);

function obCallbackMobile(payload) {
	if (document.documentElement.clientWidth < 992) {
		if (payload[0].boundingClientRect.y < 0) {
			document.querySelector('class1').style.marginBottom = fixedNavbarMobile.offsetHeight + 'px';
			fixedNavbarMobile.classList.add('class3');
		} else {
			document.querySelector('class1').style.marginBottom = '0';
			fixedNavbarMobile.classList.remove('class3');
		}
	}
}
obFixedNavbarMobile.observe(targetNavbarMobile);
  • Вопрос задан
  • 224 просмотра
Пригласить эксперта
Ответы на вопрос 1
Lapita12
@Lapita12
Тесты, тесты?
const targetNavbarDesktop = document.querySelector('.targetNavbarDesktop');
const fixedNavbarDesktop = document.querySelector('.fixedNavbarDesktop');
const obFixedNavbarDesktop = new IntersectionObserver(obCallbackDesktop);

function obCallbackDesktop(payload) {
  if (document.documentElement.clientWidth > 992) {
    if (payload[0].boundingClientRect.y < 0) {
      document.querySelector('.class1').style.marginBottom = fixedNavbarDesktop.offsetHeight + 'px';
      fixedNavbarDesktop.classList.add('class2');
    } else {
      fixedNavbarDesktop.classList.remove('class2');
      document.querySelector('.class1').style.marginBottom = '0';
    }
  }
}
obFixedNavbarDesktop.observe(targetNavbarDesktop);

const targetNavbarMobile = document.querySelector('.targetNavbarMobile');
const fixedNavbarMobile = document.querySelector('.fixedNavbarMobile');
const obFixedNavbarMobile = new IntersectionObserver(obCallbackMobile);

function obCallbackMobile(payload) {
  if (document.documentElement.clientWidth < 992) {
    if (payload[0].boundingClientRect.y < 0) {
      document.querySelector('.class1').style.marginBottom = fixedNavbarMobile.offsetHeight + 'px';
      fixedNavbarMobile.classList.add('class3');
    } else {
      document.querySelector('.class1').style.marginBottom = '0';
      fixedNavbarMobile.classList.remove('class3');
    }
  }
}
obFixedNavbarMobile.observe(targetNavbarMobile);
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы