使用transform

时间:2019-05-19 12:27:17

标签: javascript html css slider transform

我正在使用HTML的滑动表单。基本上,我希望容器(f1 | f2)中只有一个是可见的。但是我不知道如何隐藏另一个div,使其在页面上不可见。

https://jsfiddle.net/Lqu67yrw/

我已经尝试使用不起作用的overflow: hidden;,因为在转换之后,“新”溢出仍然可见,但是不再溢出的旧仍然不可见。

index.html

<div class="container">
      <div class="slider" id="slide">
        <form class="f1">
          <h1>Lorem ipsum</h1>
          <input type="text" placeholder="F1" />
          <input id="SBB" type="button" value="Show other Panel" />
        </form>
        <form class="f2">
          <h1>dolor sit</h1>
          <input type="password" placeholder="F2" />
          <input id="FBB" type="button" value="Go back" />
        </form>
      </div>
    </div>

style.scss

.container {
  width: 33%;
  height: 10%;

  padding: 1rem;

  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%);

  background-color: #fff;
  border: 2px solid #444;
  box-shadow: 0 10px 10px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.slider {
  display: flex;
  width: 200%;

  transform: translateX(15%);
  transition: transform 1s ease-in-out;
  &.active {
    transform: translateX(-35%);
    transition: transform 1s ease-in-out;
  }
}

.f1 {
  width: 100%;
}

.f2 {
  width: 100%;
  margin-left: 3rem;
}

core.js

const secondBoxBtn = document.getElementById("SBB");
const firstBoxBtn = document.getElementById("FBB");
const container = document.getElementById("slide");

secondBoxBtn.addEventListener("click", () => {
  container.classList.add("active");
});
firstBoxBtn.addEventListener("click", () => {
  container.classList.remove("active");
});

我希望f1 / f2从框的边界滑入框的中心。另一个滑出盒子。

2 个答案:

答案 0 :(得分:1)

css进行了一些更改,并且可以正常工作,请检查链接

const secondBoxBtn = document.getElementById("SBB");
const firstBoxBtn = document.getElementById("FBB");
const container = document.getElementById("slide");

secondBoxBtn.addEventListener("click", () => {
  container.classList.add("active");
});
firstBoxBtn.addEventListener("click", () => {
  container.classList.remove("active");
});
* {
  margin: 0;
  padding: 0;
  font-family: "Fira Sans", sans-serif;
}

body {
  background-color: #f4f4f4;
}

.container {
  width: 500px;
  
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%);
  background-color: #fff;
  border: 2px solid #444;
  box-shadow: 0 10px 10px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  overflow: hidden;
}

.slider {
  display: block;
  width: 1000px;
  transform: translateX(0%);
  transition: transform 1s ease-in-out;
   white-space: nowrap;
  padding: 1rem;
      

  &.active {
    transform: translateX(-50%);
    transition: transform 1s ease-in-out;
  }
}

.f1 {
  width: 50%;
  display:inline-block
}

.f2 {
  width: 50%;
  display:inline-block
}
<div class="container">
      <div class="slider" id="slide">
        <form class="f1">
          <h1>Lorem ipsum</h1>
          <input type="text" placeholder="F1" />
          <input id="SBB" type="button" value="Show other Panel" />
        </form>
        <form class="f2">
          <h1>dolor sit</h1>
          <input type="password" placeholder="F2" />
          <input id="FBB" type="button" value="Go back" />
        </form>
      </div>
    </div>

https://codepen.io/Aditya055/pen/pmrRoq

答案 1 :(得分:0)

您可以通过添加display: none样式来禁用每种形式。

例如:https://jsfiddle.net/sgcy0mbo/5/

HTML

<div class="container">
      <div class="slider" id="slide">
        <form class="f1" id="f1">
          <h1>Lorem ipsum</h1>
          <input type="text" placeholder="F1" />
          <input id="SBB" type="button" value="Show other Panel" />
        </form>
        <form class="f2 hide" id="f2">
          <h1>dolor sit</h1>
          <input type="password" placeholder="F2" />
          <input id="FBB" type="button" value="Go back" />
        </form>
      </div>
    </div>

JS

const secondBoxBtn = document.getElementById("SBB");
const firstBoxBtn = document.getElementById("FBB");
const container = document.getElementById("slide");
const f1 = document.getElementById("f2");
const f2 = document.getElementById("f1");
secondBoxBtn.addEventListener("click", () => {
    f1.classList.remove("hide");
    f2.classList.add("hide");
  container.classList.add("active");
});
firstBoxBtn.addEventListener("click", () => {
    f2.classList.remove("hide");
    f1.classList.add("hide");   
  container.classList.remove("active");
});

CSS

* {
  margin: 0;
  padding: 0;
  font-family: "Fira Sans", sans-serif;
}

body {
  background-color: #f4f4f4;
}

.container {
  width: 33%;
  height: 10%;
  padding: 1rem;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%);
  background-color: #fff;
  border: 2px solid #444;
  box-shadow: 0 10px 10px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.slider {
  display: flex;
  width: 200%;
  transform: translateX(15%);
  transition: transform 1s ease-in-out;

  &.active {
    transform: translateX(-35%);
    transition: transform 1s ease-in-out;
  }
}

.f1 {
  width: 100%;
}

.hide {
  display: none
}

.f2 {
  width: 100%;
  margin-left: 3rem;
}