简体   繁体   English

转换DIV元素,同时还原另一个元素上的过渡-使用Javascript

[英]Transition a DIV element while reverting transition on a another element - Using Javascript

I have php generating several DIV tags populated with information for a database. 我有PHP生成几个DIV标签,其中填充了数据库信息。 A DIV height of 400px will display all information. 400像素的DIV高度将显示所有信息。 However, I render the DIVs to height=50PX on the page with CSS. 但是,我使用CSS在页面上将DIV渲染为height = 50PX。

example: 例:

<div style="height: 50px">Info</div>
<div style="height: 50px">Info</div>
<div style="height: 50px">Info</div>
<div style="height: 50px">Info</div>

etc 等等

I need some help with javascript method and functions, that will run when I click on the DIV element, the function should change the style.property.height to 400px and simultaneously reduce any other div that was expanded to 400px back to 50px. 我需要一些有关javascript方法和函数的帮助,这些函数将在我单击DIV元素时运行,该函数应将style.property.height更改为400px,同时将其他任何已扩展到400px的div减小为50px。 When the DIV is action ie it's at 400px, it should NOT change no matter how many time it's clicked on, but once click out of the DIV or another div it should revert to 50px 当DIV为动作时,即为400px时,无论单击多少次,都不应更改,但一旦单击DIV或另一个div,它应恢复为50px

You can give all the div s the same class to select them by (with document.querySelectorAll ) to change their styles. 您可以给所有div相同的类,以选择它们(通过document.querySelectorAll )以更改其样式。 You can loop through all of the div s with Array.prototype.forEach to change their heights back to 50px . 您可以使用Array.prototype.forEach遍历所有div将其高度更改回50px JSFiddle: http://jsfiddle.net/s5dLpjkg/embedded/result JSFiddle: http : //jsfiddle.net/s5dLpjkg/embedded/result

 var divs = document.querySelectorAll(".myClass"); var input = document.getElementById("num"); document.getElementById("transitionBtn").addEventListener("click", function(e){ var num = input.value; var div = divs[num-1]; [].slice.call(divs).forEach(function(el){ if(el.style.height!=="50px"&&el!==div){ el.style.height = "50px"; } }); div.style.height="200px"; }); 
 .myClass{ background-color: dodgerblue; border: 1px solid black; transition: height 2s; } 
 <div style="height: 50px" class="myClass">Info #1</div> <div style="height: 50px" class="myClass">Info #2</div> <div style="height: 50px" class="myClass">Info #3</div> <div style="height: 50px" class="myClass">Info #4</div> <input type="number" id="num" placeholder="Div number" min="1" max="4" style="width: 30%" value="1"> <p/> <button id="transitionBtn">Run Transition</button> 

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

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