[英]Trying to display content on click using css (active) and javascript. How can I keep the content displaying?
I feel like this is incredibly simple but I'm just not quite getting it. 我觉得这非常简单,但我还不太了解。
What I'm trying to do: When you click on a circle image, text will show up below it and will stay there until another circle image is clicked on. 我正在尝试做的事情:当您单击一个圆形图像时,文本将显示在其下方,并将停留在该位置,直到单击另一个圆形图像为止。
This is what I have so far... https://codepen.io/elamen/pen/wYGwGw 这就是我到目前为止所拥有的... https://codepen.io/elamen/pen/wYGwGw
<div class="boxes">
<img src="http://www.conejousd.org/portals/0/Images/EnrollCVUSD/2017/school-logos/school-sm-aspen.png" style="width: 150px;">
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
</div>
<div class="boxes">
<img src="http://www.conejousd.org/portals/0/Images/EnrollCVUSD/2017/school-logos/school-sm-aspen.png" style="width: 150px;">
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
</div>
<div class="boxes">
<img src="http://www.conejousd.org/portals/0/Images/EnrollCVUSD/2017/school-logos/school-sm-aspen.png" style="width: 150px;">
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
</div>
.boxes {
opacity: 0.5;
float: left;
width: 200px;
height: 130px;
overflow: hidden;
}
.boxes:active {
opacity: 1;
height: auto;
}
$('.boxes > a').click(function() {
$('.boxes').removeClass('hovered');
$(this).parent().toggleClass('hovered');
});
I'm unsure why you used boxes:active in your CSS as the active box, so if you change that to '.hovered', it works as expected. 我不确定为什么您在CSS中使用box:active作为活动框,因此如果将其更改为“ .hovered”,它会按预期工作。 Also, I don't quite understand how your jQuery is working in the first place since the event listener is pointing to an 'a' element rather than an 'img'.
另外,由于事件监听器指向的是'a'元素而不是'img',因此我不太了解jQuery的工作方式。
In any case, here is a working pen for your reference. 无论如何,这是一支工作笔供您参考。
https://codepen.io/chrisaronchick/pen/MPyWgQ https://codepen.io/chrisaronchick/pen/MPyWgQ
Here is the CSS: 这是CSS:
.boxes {
opacity: 0.5;
float: left;
width: 200px;
height: 130px;
overflow: hidden;
}
.hovered {
opacity: 1;
height: auto;
}
Here is the JavaScript: 这是JavaScript:
$('.boxes > img').click(function() {
$('.boxes').removeClass('hovered');
$(this).parent().toggleClass('hovered');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.