[英]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:有几点需要注意:
moved
contains the CSS rules for moving the header / navigation to the right (150px). 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
等),但为了说明起见,我决定使用最简单的方法。nav {
width: 100%;
background-color: #ccc;
transition: 0.5s; /* this part here */
}
...
.moved {
margin-left: 150px;
transition: 0.5s; /* this part here */
}
moveHeader
function), if and only if the window width is less than 789px.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.