NAVBAR无法按预期运行 - 可能是由于不正确的jQuery(窗口)。

发布于 2025-02-03 00:46:49 字数 658 浏览 2 评论 0原文

我创建了一个纳维尔。在移动视图上,当我单击汉堡菜单时,我设置了一个控制台。它响应一次。当我调整窗口大小并再次单击时,它会响应几次。再次,它响应了10次以上的响应。这使纳维栏的设置打破了。下面,我尝试包括我认为要成为问题并删除所有样式功能的一般思想/逻辑。 console.log是否由于$(window).on(“ ressize”,handleresize);而越来越多?我尝试添加$(window).off(“ resize”,“ handleresize);无用。我该如何解决?

$(document).ready(function () {

// ...

function isMobileSize() {
  if (window.innerWidth < 990) {
    return true;
  }
  return false;
};

function handleResize() {
  if (isMobileSize()) {
    // show mobile dropdown
  } else if (!isMobileSize()) {
    // don't show mobile dropdown
  }
};

handleResize();
$(window).on("resize", handleResize);
});

I've created a navbar. On mobile view, I set a console.log for when I click the hamburger menu. It responds once. When I resize the window and click again, it responds several times. Again, and it responds 10+ times, etc. This breaks the navbar with how I have it set up. Below, I've tried including the general idea/logic behind what I believe is to be the issue and removing all of the styling functions and such. Does console.log fire more and more due to $(window).on("resize", handleResize);? I tried adding $(window).off("resize", handleResize); to no avail. How can I fix this?

$(document).ready(function () {

// ...

function isMobileSize() {
  if (window.innerWidth < 990) {
    return true;
  }
  return false;
};

function handleResize() {
  if (isMobileSize()) {
    // show mobile dropdown
  } else if (!isMobileSize()) {
    // don't show mobile dropdown
  }
};

handleResize();
$(window).on("resize", handleResize);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩于岁月 2025-02-10 00:46:49

您是否考虑过简化生活并为此使用CSS?
https://codepen.io/panchroma/penchroma/pen/pen/pen/pen/xwzzozozozy

>

在文档的头部添加一个视口元素

<meta name="viewport" content="width=device-width, initial-scale=1">  

,并在移动下拉列表中添加一类,以便您可以使用CSS

<div class="mobile-dropdown">
  <ul>
    <li><a href="#">mobile link</a></li>
    <li><a href="#">mobile link</a></li>
    <li><a href="#">mobile link</a></li>
    <li><a href="#">mobile link</a></li>
  </ul>
</div>

css 对目标进行定位。

@media (min-width: 990px) {
  .mobile-dropdown {
    display: none;
  }
}

Have you considered simplifying your life and using CSS for this?
https://codepen.io/panchroma/pen/XWZZozy

HTML

Add a viewport metatag in the head of your doc

<meta name="viewport" content="width=device-width, initial-scale=1">  

And add a class to your mobile dropdown so you can target it with your CSS

<div class="mobile-dropdown">
  <ul>
    <li><a href="#">mobile link</a></li>
    <li><a href="#">mobile link</a></li>
    <li><a href="#">mobile link</a></li>
    <li><a href="#">mobile link</a></li>
  </ul>
</div>

CSS

@media (min-width: 990px) {
  .mobile-dropdown {
    display: none;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文