[英]How to get dimmer on specific card to toggle rather than all my card gallery using Semantic UI and JQuery
I have a gallery of avatars I want to show my user, which are shown in the card format of Semantic-UI. 我有一个要显示给用户的化身画廊,这些化身以Semantic-UI的卡格式显示。 I want the user to click on one of the images which then triggers Semantic's dimmer to appear in that specific image's place.
我希望用户单击其中一张图像,然后触发语义调光器出现在该特定图像的位置。
What I currently get is that all the images get the dimmer to appear on them instead of the specific one I want. 我目前得到的是所有图像都经过调光器显示,而不是我想要的特定图像。 Here is the code that puts the dimmer on all images:
这是将调光器放置在所有图像上的代码:
$(".selectavatar img").hover(function() { $('.selectavatar img').removeClass('selectedImage'); $(this).toggleClass("selectedImage"); }); $(".selectavatar img").click(function() { $(".ui.dimmer").dimmer("toggle"); });
<!--gallery goes here--> <div class="ui three column grid selectavatar"> <div class="five wide column"> <div class="ui segment"> <form action="/account/<%=currentUser._id%>?_method=PUT" method="POST"> <input name="user[image]" type"submit" value = "image1.jpg" class="hidden" /> <img name="user[image]" src = "image1.jpg" width=300> <div class="ui dimmer"> <div class="content"> <div class="center"> <button class="ui button blue" type="Submit"> <i class="user icon"></i> Select Avatar </button> </div> </div> </div> </form> </div> </div> <div class="five wide column"> <div class="ui segment"> <form action="/account/<%=currentUser._id%>?_method=PUT" method="POST"> <input name="user[image]" type"submit" value = "image2.png" class="hidden" /> <img name="user[image]" src = "image2.png" width=300> <div class="ui dimmer"> <div class="content"> <div class="center"> <button class="ui button blue" type="Submit"> <i class="user icon"></i> Select Avatar </button> </div> </div> </div> </form> </div> </div>
The way I envisioned it was that I'll add the class .selectedImage
on the image the user wants, then I would be able to toggle the dimmer if selectedImage
exists. 我设想的方式是,我将在用户想要的图像上添加类
.selectedImage
,然后如果selectedImage
存在,我将能够切换调光器。 But when I do use that as so: 但是当我这样使用时:
$(".selectavatar img.selectedImage").click(function() {
$(".ui.dimmer").dimmer("toggle");
});
then nothing appears, no dimmer whatsoever! 那么什么也没出现,没有任何调光器!
What am I doing wrong? 我究竟做错了什么?
As a side note, I need to figure out how to generate a gallery from a folder of images using EJS and NodeJS, instead of having to hard code each image. 附带说明一下,我需要弄清楚如何使用EJS和NodeJS从图像文件夹生成图库,而不是必须对每个图像进行硬编码。 I guess the right thing to do would be a seperate question for that?
我猜想正确的事将是一个单独的问题?
Your code will dim all .ui.dimmer divs when you click an image: 单击图像时,您的代码将使所有.ui.dimmer div变暗:
$(".selectavatar img").click(function() {
$(".ui.dimmer").dimmer("toggle");
});
This code will dim only the .ui.dimmer div which is located in the same parent container as the clicked image: 此代码只会使与单击的图像位于同一父容器中的.ui.dimmer div变暗:
$(".selectavatar img").click(function() {
$(this).parent().find(".ui.dimmer").dimmer("toggle");
});
Part 2 第2部分
Your code will set a click handler on all img.selectedImage which exist when you set the event handlers. 您的代码将在设置事件处理程序时存在的所有img.selectedImage上设置一个单击处理程序。 Unfortunately no images have the selectedImage class at that point so it doesn't work.
不幸的是,那时没有图像具有selectedImage类,因此它不起作用。
$(".selectavatar img.selectedImage").click(function() {
//code
});
This code will do the same, but it only requires .selectavatar to exist when the event handler is set up: 此代码将执行相同的操作,但是在设置事件处理程序时,仅要求.selectavatar存在:
$(".selectavatar").on("click", "img.selectedImage", function() {
//code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.