简体   繁体   English

当屏幕宽度大于特定宽度时如何恢复事件

[英]How I can restore an event when screen width is greater than specific width

When the screen size is less than 987px, I want to add an event on Menu icon that will move the header 150px into the right and when I click again, it will push the header -150 into the left.当屏幕尺寸小于 987px 时,我想在菜单图标上添加一个事件,将 header 向右移动 150px,当我再次单击时,它会将 header -150 推向左侧。 The problem is when the screen width is greater than 789, the header has been moved accordingly which I don't want.问题是当屏幕宽度大于 789 时,header 已相应移动,这是我不想要的。 How can I restore the header automatically into it's actual position again as it were before whenever screen width is greater than 789?当屏幕宽度大于 789 时,如何将 header 自动恢复为实际的 position?

you can do css example你可以做 css 的例子

@media only screen and (max-width:987px) {
.menu{
margin-left:150px;
}
}

If I understand your question correctly, you want a conditional behaviour of your button clicks - the clicks should work on resolutions with less than 789px width (the question is not specific on this, but I'm assuming you mean the width).如果我正确理解你的问题,你想要你的按钮点击的条件行为 - 点击应该适用于宽度小于 789px 的分辨率(这个问题没有具体说明,但我假设你的意思是宽度)。

To achieve this, you can do something like in the code below (the appropriate fiddle can be found here - in it, you can experiment with the width of the document, which is something you can not do here on SO).为此,您可以在下面的代码中执行类似的操作(可以在此处找到适当的小提琴 - 在其中,您可以试验文档的宽度,这是您在 SO 上无法执行的操作)。

 const nav = document.getElementsByTagName("nav")[0]; const btn = document.getElementById("clicker"); btn.addEventListener("click",moveHeader); function moveHeader() { let wd = window.innerWidth; console.log(wd); if(wd < 789) { nav.classList.toggle("moved"); } else { nav.classList.remove("moved"); } } window.addEventListener("resize", function() { if(this.innerWidth > 789) { nav.classList.remove("moved"); } });
 #controller { display: inline-block; width: 100px; position: relative; border-right: 2px solid black; } button { margin: 15px auto; } #content { display: inline-block; overflow: hidden; } #content div, nav { display: block; } nav { width: 100%; background-color: #ccc; } nav a { padding: 15px; } #content div { margin-top: 15px; margin-left: 15px; }.moved { margin-left: 150px; }
 <div id="controller"> <button id="clicker"> Click me </button> </div> <div id="content"> <nav> <a href="#">Home</a> <a href="#">Portfolio</a> <a href="#">Contact</a> </nav> <div> Lorem ipsum sit dolor amet etc </div> </div>

There are a couple of things to note:有几点需要注意:

  • the class moved contains the CSS rules for moving the header / navigation to the right (150px). class moved包含将 header / 导航向右移动 (150px) 的 CSS 规则。 There are other ways to achieve this ( position: absolute etc), but I decide on the simplest one for the sake of illustration.还有其他方法可以实现此目的( position: absolute等),但为了说明起见,我决定使用最简单的方法。
  • if you want to have a fairly smooth animation of the header going back and forth, you could change these rules (or adapt them for your particular case):如果你想让 header 的 animation 来回相当顺畅,你可以更改这些规则(或根据你的特定情况调整它们):
nav {
  width: 100%;
  background-color: #ccc;
  transition: 0.5s; /* this part here */
}

...

.moved {
  margin-left: 150px;
  transition: 0.5s; /* this part here */
}
  • the same class is toggled on your button click ( moveHeader function), if and only if the window width is less than 789px.当且仅当 window 宽度小于 789px 时,相同的 class 在您的按钮点击( moveHeader函数)上切换。 The reason for using the toggle - your users might keep clicking on the button, and it will keep sending the header / navigation away, and pulling it back.使用toggle的原因——你的用户可能会一直点击按钮,它会一直发送 header / 导航,然后再拉回来。 Since I don't know the layout of your page, this solution will let you things like open and close the navigation, send it offscreen, and bring it back, etc因为我不知道你的页面布局,这个解决方案会让你打开和关闭导航,将它发送到屏幕外,然后把它带回来等等
  • the window has an event listener attached to it - your users might change the width of their browsers, they might stack windows on their desktops, they might rotate their tablets and phones, etc, and by doing so, they might change the screen width. window 有一个附加的事件侦听器 - 您的用户可能会更改浏览器的宽度,他们可能会在桌面上堆叠 windows,他们可能会旋转他们的平板电脑和手机等,并且通过这样做,他们可能会更改屏幕宽度。 If that happens, you need to be able to go back to the original functionality for < 789px width.如果发生这种情况,您需要能够 go 恢复 < 789px 宽度的原始功能。 Of course, this code covers only the width change.当然,这段代码只涉及宽度变化。 For handling a potential change in screen orientation, please see this SO answer - be sure to check the canIUse screenshot attached to it.要处理屏幕方向的潜在变化,请参阅此 SO 答案- 请务必检查附带的 canIUse 屏幕截图。

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

相关问题 如果屏幕宽度大于,如何在调整大小和加载时停止单击事件? - How to stop click event on resize & load if screen width is greater than? 如果宽度大于屏幕宽度,如何隐藏动态内容? - How to hide dynamic content if it's width is greater than screen width? 当屏幕宽度小于 810px 时,如何移除 relax class? - How can i remove the rellax class when the screen width is smaller than 810px? 当 div 宽度大于 80% 时添加类 - Addclass when div width is greater than 80% 如何检查元素宽度是否大于100% - How to check if element width is greater than 100% 如果 window 宽度小于给定值,我如何使用 JavaScript 隐藏 Html 中的文本,并在大于该值时显示它 - How can i hide a text in Html using JavaScript if the window width is lower than a given value and show it if it's greater than the value 当屏幕宽度大于 1024px 时删除类(如果存在) - Remove class (if present) when screen width is greater than 1024px 当屏幕宽度小于屏幕宽度时,如何删除Javascript中的clone()规则 - How to remove clone() rule in Javascript when screen width is smaller than 如何使HTML表格具有自动宽度但大于屏幕宽度? - How can I make an HTML table auto width but larger than the screen? 仅在屏幕调整为特定宽度时,如何使用JQuery调用函数? - How to Call a Function with JQuery Only When Screen Resizes to a Specific Width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM