简体   繁体   English

Javascript slideUp / slideDown /一个按钮

[英]Javascript slideUp / slideDown / one button

I am having trouble of solving a problem. 我无法解决问题。 I have 2 buttons, the one is for slideUp and the other is for slideDown. 我有2个按钮,一个用于slideUp,另一个用于slideDown。 What i would like to do is to have one button that toggle instead of having 2. 我想做的是有一个按钮切换而不是2。

Thanks. 谢谢。

I have this code in html: 我在html中有这个代码:

<div id = "box">Show / Hide</div>
  <button onclick="slideUp('box');">Slide Up</button>
  <button onclick="slideDown('box');">Slide Down</button>

and i have this code in css: 我在css中有这个代码:

body{
  background:grey;
}

#box{
  width:400px;
  height:400px;
  background:orange;
  margin:0 auto;
  margin-top:3%;
  overflow:hidden;
}

and my javascript code is this: 我的javascript代码是这样的:

function slideUp(el) {
  var elem = document.getElementById(el);
  elem.style.transition = "all 2s ease-in-out";
  elem.style.height = "0px";
}
function slideDown(el) {
  var elem = document.getElementById(el);
  elem.style.transition = "all 2s ease-in-out";
  elem.style.height = "400px";
}

You can create CSS class with the height:0 rule, and toggle that class with JS : 您可以使用height:0规则创建CSS class ,并使用JS 切换该类:

 var elem = document.getElementById("box"); function slide() { elem.classList.toggle('hide'); } 
 .box { width: 400px; height: 400px; overflow: hidden; background: orange; margin: 0 auto; transition: all 0.4s ease-in-out; } .hide { height: 0; } 
 <button onclick="slide();">Slide Toggle</button> <div id="box" class="box">Show / Hide</div> 

Put the css properties into a class, add the class to your element, and then toggle the class that affect the height on/off of the element using classList.toggle() 将css属性放入类中,将类添加到元素中,然后使用classList.toggle()切换影响元素高度开/关的类

 function slideToggle(el) { var elem = document.getElementById(el); elem.classList.toggle("open"); } 
 body{ background:grey; } #box{ width:400px; background:orange; margin:0 auto; margin-top:3%; overflow:hidden; } .slider { transition:all 2s ease-in-out; height:0px; } .slider.open { height:400px; } 
 <div id = "box" class="slider">Show / Hide</div> <button onclick="slideToggle('box');">Slide</button> 

You can create class .slideup 您可以创建类.slideup

#box.slideUp {
    height:0;
}

And toggle this class on #box: 并在#box上切换此课程:

function toggle(){
    document.getElementById('box').classList.toggle('slideUp')
}

Example here 这里的例子

Best way is to toggle a class on the box when the button is clicked. 最好的方法是在单击按钮时在框上切换类。 That class will set the height on the box to 0, and so the transition will take effect and smooth the change. 该类将框上的height设置为0,因此转换将生效并平滑更改。

JS JS

document.querySelector('.js-button').addEventListener('click', function (event) {
  document.querySelector('.box').classList.toggle('box-closed')
});

CSS CSS

.box-closed {
  height: 0px;
}

Demo 演示

I like to track state with a simple boolean, a bit like so : 我喜欢用一个简单的布尔跟踪状态,有点像这样:

var _bool = false;
var _box = document.getElementById('box');
var _clik = document.getElementById('clik');

_clik.addEventListener('click', function(){
  if (_bool){
      _box.style.height = '100px';
      _bool = false;
   } else {
      _box.style.height = '10px';
      _bool = true;
   }

});

Here's a Fiddle 这是一个小提琴

Importantly, we're not storing state on the DOM, and so once your app gets complicated, it will stay performant. 重要的是,我们不会在DOM上存储状态,因此一旦您的应用程序变得复杂,它将保持高效。 You, obviously, can use these two states in whatever way you like (animation, css transition, etc); 显然,你可以用你喜欢的任何方式使用这两种状态(动画,css过渡等);

Apply transition to #box via css stylesheet; 通过css stylesheet将转换应用到#box;

 #box {
  -webkit-transition: all 2s ease-in-out;
  -ms-transition: all 2s ease-in-out;
  transition: all 2s ease-in-out;
  width:400px;
  height:400px;
  background:orange;
  margin:0 auto;
  margin-top:3%;
  overflow:hidden;
}

I am trying to get more familiar with modules today, so this is a modular approach - solution 我今天想要更熟悉模块,所以这是一种模块化方法 - 解决方案

  var Dropdown = function(el) {

      var is_open = false;
      var elem = document.querySelector(el);  
      // in case you cannot set transition via stylesheet 
      elem.style.transition = "all 2s ease-in-out";

      function slideUp() {
        elem.style.height = "0px";     
        is_open = false;
      }
      function slideDown() {
        elem.style.height = "400px";
        is_open = true;
      }
      function init() {
        if(is_open) {
          slideUp();
        }else{
          slideDown();
        }
      }
      return {
        init : init
      }
  };

  myDropdown = Dropdown('#box');
  document.querySelector('#your-button').addEventListener('click',  myDropdown.init);

Make it shorter: 缩短时间:

<button onclick="slide()"></button>

<div id="box"></div>

JS: slide(){document.getElementById("box").classList.toggle('hide');} JS: slide(){document.getElementById("box").classList.toggle('hide');}

#box {
  overflow: hidden;
  transition: all .5s ease-in-out;
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -ms-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
}
.hide {
  height: 0px !important;
}

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

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