2016-08-05 46 views
-2

因此,我目前正试图弄清楚如何使用jQuery提供的.show()和.hide()功能来显示和隐藏div类或p类包含一些文本,我希望能够在div-parent内部单击以显示上下文。不要有按钮,但我们假设我将鼠标悬停在包含简短文本和徽标的div类上,背景会发生变化,我希望能够点击此类并在父类中显示隐藏的内容。没有按钮的jQuery显示和隐藏元素

我会尝试用下面的图片ilustrate此:

enter image description here

所以以后我在div级点击这应该扩展现有的类,并显示被隐藏的内容:

enter image description here

我希望有人能让我走上正确的轨道,并帮助我解决这个问题。 干杯

+3

后一些代码的方法的另一种方式。你尝试过什么吗?什么不行? – Christophvh

+1

你能发表一些你写的代码吗? – evolutionxbox

+1

请在OP中发布所有相关代码而不仅仅是图片 – guradio

回答

1

你可以解决您的关注纯悬停上的CSS。检查这是你在找什么!

.content:hover, .content2:hover, .content3:hover { 
    height: 200px; 
} 

或者你可以使用jQuery!

注:此代码是保留transitiondiv与动态高度膨胀。

更新

jQuery.fn.animateAuto = function(prop, speed, callback){ 
 
    var elem, height; 
 
    return this.each(function(i, el){ 
 
     el = jQuery(el), elem = el.clone().css({"height":"auto"}).appendTo("body"); 
 
     height = elem.css("height"), 
 
     elem.remove(); 
 
     if(prop === "height") 
 
      el.animate({"height":height}, speed, callback); 
 
    }); 
 
} 
 

 
$("document").ready(function(){ 
 
    $(".content, .content2, .content3").hover(function(){ 
 
    var h = $(this).css("height"); 
 
    if(h == '50px'){ 
 
     $(this).animateAuto('height', 20); 
 
    } 
 
    else{ 
 
     $(this).css("height", '50px'); 
 
    } 
 
    }); 
 
});
.content, .content2, .content3 { 
 
    padding: 5px; 
 
    height: 50px; 
 
    width: 25%; 
 
    margin:0 2%; 
 
    float: left; 
 
    position: relative; 
 
    transition: height 0.5s; 
 
    -webkit-transition: height 0.5s; 
 
    text-align: center; 
 
    overflow: hidden; 
 
    background:#666; 
 
    border-radius:5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="content"> 
 
    <h2>Div 1</h2> 
 
    <p>This is an example! This is an example!</p> 
 
</div> 
 

 
<div class="content2"> 
 
    <h2>Div 2</h2> 
 
    <p>This is an example! This is an example! This is an example! This is an example! This is an example2!</p> 
 
</div> 
 

 
<div class="content3"> 
 
    <h2>Div 3</h2> 
 
    <p>This is an example! This is an example!</p> 
 
</div>

+1

是的,纯CSS将是最好的选择。 – Shamppi

+0

谢谢你的回答!所以我认为这会做到这一点,但是有没有一种方法可以根据内容的长度来扩展div高度?让我们假设div1的内容是div2的两倍,但我不希望两者的高​​度相同。我仍然希望淡化效果在那里,试图将高度设置为相对或自动,但是这似乎消除了过渡? – magnusnn

+0

我已经更新了我的答案,希望能帮到你! –

0

当主容器元素被点击找到所有与hidden-text类单击元素的儿童,并给他们。

$(".hidden-text").hide(); 
 
$(".container").click(function() { 
 
    $(".container").children(".hidden-text").hide(); 
 
    $(this).children(".hidden-text").show(); 
 
})
.container{ 
 
    width:100px; 
 
    display:inline-block; 
 
    vertical-align:top; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container">This is a div<div class="hidden-text">This is a bunch of hidden text in the div inside of another div.</div></div> 
 
<div class="container">This is a div<div class="hidden-text">This is a bunch of hidden text in the div inside of another div.</div></div> 
 
<div class="container">This is a div<div class="hidden-text">This is a bunch of hidden text in the div inside of another div.</div></div>

+0

谢谢您的回答,并对迟到的回复感到抱歉!所以我试过了你的解决方案,但是我怎么只做一个div块来一次显示呢?基本上,如果打开的div容器失去焦点,我希望它被隐藏。例如,如果我显示第一个并将其中一个打开,然后单击第二个,我希望第一个再次褪色并隐藏。如果我点击“隐藏文本”或打开的“容器本身内的某处,应该再次隐藏它。 – magnusnn

+0

我更新了我的答案,这是你想要的吗? – Wowsk

+0

嗯,是的,其中一些,但是如果我在空白页上点击外部,我想隐藏每个打开的div块。有没有一个更简单的方法做到这一点比我想出的: $(document).ready(function(){ $(“body”)。click(function(e){if($(e.target ).attr( '类')=== “容器”){ 通; } 其他{ “容器 ” $()儿童(“ 隐藏的文本”),隐藏();} }); }); – magnusnn

0

请不要使用参考纯CSS

<div class="box-content"> 
    <h2>Div 1</h2> 
    <p>This is an example! This is an example! This is an example! This is an example! This is an example! This is an example! </p> 
</div> 

<div class="box-content"> 
    <h2>Div 2</h2> 
    <p>This is an example! This is an example! This is an example! This is an example! This is an example! This is an example! </p> 
</div> 

<div class="box-content"> 
    <h2>Div 3</h2> 
    <p>This is an example! This is an example! This is an example! This is an example! This is an example! This is an example! </p> 
</div> 


.box-content { padding: 5px; height: auto; width: 25%; margin: 0 2%; float: left; background: #666; border-radius: 5px; } 
.box-content p { display: none; } 
.box-content:hover p { display: block; color: #fff; }