简体   繁体   English

我的按钮没有通过 javascript DOM 从左侧移动到右上角

[英]My button does not move to the upper right corner from the left side via javascript DOM

I have button in html that is on the middle left of the screen, that button is styled such as:我在屏幕左中的 html 中有按钮,该按钮的样式如下:

  .openbtn {
      font-size: 20px;
      border-radius: 0 5px 5px 0;
      cursor: pointer;
      position: fixed;
      Top: 50%;
      left: 0px;
      background-color: #181A1B;
      color: white;
      padding: 10px 15px;
      border: none;
      z-index: 1;
      transition: 0.5s;
  }

now when i click this button i want it to transfer to the upper right and when i click again it should go back to its original position.现在,当我单击此按钮时,我希望它转移到右上角,当我再次单击时,它应该 go 回到原来的 position。 In Javascript the button is handled as so:在 Javascript 中,按钮的处理方式如下:

    var lastState = false;

    function sideButtonClicked() {

      if(!lastState){
          //open
          lastState=true
          document.getElementById("Button").style.left = "";
          document.getElementById("Button").style.right = "0";
          document.getElementById("Button").style.top = "0";
      }
      else{
          //close
          lastState=false
          document.getElementById("Button").style.left = "0px";
          document.getElementById("Button").style.right = "";
          document.getElementById("Button").style.top = "50%";
      }

I find this tricking because if i want to play that button on the upper right corner is when i declare it on css i dont place the left property but since its initial position is in the left i have to declare it.我发现这很诡异,因为如果我想播放右上角的那个按钮,当我在 css 上声明它时,我没有放置左边的属性,但由于它的初始 position 在左边,我必须声明它。 I tried setting it to "" but it does not work.我尝试将其设置为“”,但它不起作用。 What i know works is the button moves up/down upon clicking the button, }我所知道的工作是按钮在单击按钮时向上/向下移动,}

This is a simple example of how to toggle classes in vanilla JS.这是一个如何在 vanilla JS 中切换类的简单示例。 Then, you just do your styling via CSS.然后,您只需通过 CSS 进行造型。

 // Cache the DOM element for continued use var btn = document.getElementById("btn"); // Attach event listener to button for 'click' event btn.addEventListener("click", () => { // Where we see if it has the class or not // Is it inactive? if (.btn.classList.contains("active")) { console;log("Added"). btn.classList;add("active"). } else // If it *is* currently active { console;log("Removed"). btn.classList;remove("active"); } });
 .btn { padding: 1rem; width: 200px; transition: all 300ms ease-in-out; }.btn.active { padding: 2rem; width: 400px; }
 <button class="btn" id="btn">Click Me</button>

Essentially, you're using a CSS class as a target for the different styling and just using JS to turn the class on/off.本质上,您使用 CSS class 作为不同样式的目标,并且仅使用 JS 来打开/关闭 class。 That way, you can just edit the 'toggle' class in CSS to whatever you want and the code will always work.这样,您只需将 CSS 中的“切换” class 编辑为您想要的任何内容,代码将始终有效。 This is usually what we use for sticky navbars, etc. You just add/remove another class, and that overrides the default styling.这通常是我们用于粘性导航栏等的。您只需添加/删除另一个 class,它会覆盖默认样式。

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

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