繁体   English   中英

Vanilla Javascript 在移动视图中更改图像

[英]Vanilla Javascript change image when in mobile view

在移动设备中如何将徽标更改为白色? 但是我已经有了下面的滚动代码,我正在使用香草 javascript。 我有深色和浅色的标志。 它在滚动时工作但不知道如何为宽度移动添加脚本

 addEventListener('scroll', (event) => { });

onscroll = (event) => {
 
  const logoDark = document.querySelector('.nav-logo');
  const logoLight = document.querySelector('.nav-logo-light');

  logoDark.classList.add("show");
  logoLight.classList.add("hide");


  if (window.scrollY > 100) {
    logoDark.classList.add("hide");
    logoLight.classList.add("show");

    for (let i = 0; i < navItems.length; i++) {
      navItems[i].classList.add('light-text');
    }
  }

  else if (window.scrollY < 100) {
    logoDark.classList.remove('hide');
    logoLight.classList.remove("show");
  }
};

您可以在 javascript 中使用media-query也可以在 CSS 中使用

这是一个片段:

function scrollFunction(isMobile) {
  if (!isMobile) { 
   // If this is a desktop screen add your logic for desktop
  } else {
   // else if this is a mobile screen add logic for mobile
  }
}

var matchMedia =  window.matchMedia("(max-width: 400px)") // or whatever you consider mobile width for your use case.
scrollFunction(matchMedia.matches) // Call listener function at run time
matchMedia.addListener(myFunction) // Attach listener function on state changes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM