简体   繁体   English

单击显示隐藏的Div

[英]Reveal Hidden Divs on click

I'm looking to reveal a div when I click on a corresponding div, much like when you click on an image in Google's image search results. 当我点击相应的div时,我想要显示div,就像点击Google图片搜索结果中的图片一样。 The row of images split, and a new area is revealed. 分割图像行,并显示新区域。 I got pretty far with my minimal knowledge of javascript here: http://codepen.io/anon/pen/fKEgw 我对javascript的最小知识非常了解: http//codepen.io/anon/pen/fKEgw

The basics of it works like this: 它的基础知识是这样的:

document.getElementById("a-section").addEventListener("click", function () {
document.getElementById("a-reveal").style.height = "300px";
document.getElementById("b-reveal").style.height = "0px";
document.getElementById("c-reveal").style.height = "0px";
document.getElementById("d-reveal").style.height = "0px";
document.getElementById("e-reveal").style.height = "0px";
document.getElementById("f-reveal").style.height = "0px";
document.getElementById("g-reveal").style.height = "0px";
document.getElementById("h-reveal").style.height = "0px";
});

How can I get a similar effect without being forced to set a height value? 如何在不强制设置高度值的情况下获得类似效果? Is there a better way to code this type of thing so I'm not making duplicate divs for the mobile view? 有没有更好的方法来编写这种类型的东西,所以我不是为移动视图制作重复的div? SHould I use something other than just javascript? 我应该使用除了javascript以外的东西吗?

Thank you very much for your help! 非常感谢您的帮助!

If you use jquery, you can simply do, for example 例如,如果使用jquery,则可以执行此操作

$("#a-reveal").show();

and

$("#b-reveal").hide();

rather than changing the heights. 而不是改变高度。 In plain javascript this is basically setting the 'display' css attribute to 'none' to hide it and then back (default is 'block'). 在普通的javascript中,这基本上是将'display'css属性设置为'none'来隐藏它然后返回(默认为'block')。

edit: 编辑:

Also, if you modify the html so that the divs are like this: 此外,如果您修改html,以便div是这样的:

<div id="a-section" class="section" reveal="a-reveal">
....

<div id="a-reveal" class="reveal">

where reveal contains the ID of the associated reveal div, you could replace all the separate event handler attaching code with the following: 如果reveal包含关联的揭示div的ID,您可以使用以下内容替换所有单独的事件处理程序附加代码:

//attaches event to every div with class "section"
$(".section").click(function(){
    $(".reveal").hide();
    var thisRevealID = $(this).attr("reveal");
    $("#"+thisRevealID).show();
});

There may be a cleaner way to get the appropriate reveal div than to resort to adding the reveal attribute, but the idea is to make it so you can determine what you need based on the clicked div without the code knowing exactly which div was clicked. 可能有一种更简洁的方法来获取相应的揭示div而不是诉诸添加揭示属性,但想法是这样做,这样你就可以根据点击的div确定你需要什么,而代码不知道确切地点击了哪个div。

I agree that if the project is large you might be better off with JQuery, see thogue's excellent answer. 我同意,如果项目很大,你可能会更好地使用JQuery,请参阅thogue的优秀答案。 If you want to stay with plain Javascript you can certainly clean it up a little bit: 如果你想使用普通的Javascript,你当然可以清理一下:

var reveal_func = function(section){
   var arr_a = new array("a-reveal","b-reveal","c-reveal");
   for (var i=0; i<var_a.length; i++){
      if (arr_a[i] === section){
          document.getElementById(arr_a[i]).style.height = "300px";
      }else{
         document.getElementById(arr_a[i]).style.height = "0px";
      }
   }

};

and then 接着

document.getElementById("a-section").addEventListener("click", reveal_func("a-reveal"))
document.getElementById("b-section").addEventListener("click", reveal_func("b-reveal"))

as needed. 如所须。

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

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