简体   繁体   English

单击其他图像时隐藏 div

[英]Hide div when I click on a different image

I developed an image gallery.我开发了一个图片库。 Each image has its name as its original state.每个图像都有其名称作为其原始状态。 When I click on an image, the div with the name disappears and a new div with actions is displayed.当我单击图像时,带有名称的 div 消失并显示一个带有动作的新 div。

Is there a way to click on a different image, the previous image goes back to its original state?有没有办法点击不同的图像,之前的图像会恢复到原来的状态? (presents the a div with the name again) (再次显示带有名称的 div)

At this moment, when I click on the image I can hide the div with the name and present the action div, however when I click on another image, I cannot bring the previous one back to its original state :(此时,当我单击图像时,我可以隐藏带有名称的 div 并显示动作 div,但是当我单击另一张图像时,我无法将前一张恢复到原始状态:(

DEMO 演示

code代码

<ul class="mdc-image-list my-image-list" style="padding-left: 10px;padding-right: 10px;">
    <li class="mdc-image-list__item" *ngFor="let product of Images; let  j = index;">
        <div class="mdc-image-list__image-aspect-container">
            <img [src]="product.image" class="mdc-image-list__image"
                (click)="eventB($event,j)">
          </div>
            <div class="mdc-image-list--with-text-protection">
                <div class="mdc-image-list__supporting mdc-image-list__supporting{{j}}">
                    <span class="mdc-image-list__label divT{{j}}">{{product.Name}}</span>
                    <span class="mdc-image-list__label ImageButtonsG divB{{j}}">
                <a >ADD </a>
                <a>Save</a>
                <a>delete</a>
                <a>neu</a>
              </span>
                </div>
                <a class="EyeG eyeB{{j}}" (click)="Produto(product.id)"></a>
            </div>
    </li>
</ul>


      eventB(e, j) {
    $(".divT" + j).css({ "display": "none" });
    $(".mdc-image-list__supporting" + j).css({ "background": "none" });
    $(".divB" + j).css({ "display": "block" });
    $(".eyeB" + j).css({ "display": "block" });
  }

Problem问题

I clicked on two images, only the last image clicked should have the stock div and not both (the first clicked should be in the original state (div with the name))我点击了两张图片,只有点击的最后一张图片应该有股票 div 而不是两者(第一次点击应该处于原始状态(带有名称的 div))

图片

You will need to remove the styling you added when you invoke eventB(e, j) .您需要删除在调用eventB(e, j)时添加的样式。 Remove that style from all images and then add it to the image you clicked on.从所有图像中删除该样式,然后将其添加到您单击的图像中。

In other words:换句话说:

eventB(e, j) {
  this.Images.forEach((img, index) => {
    $(".divT" + index).css({ "display": "block" });
    $(".mdc-image-list__supporting" + index).css({ "background": "rgba(0,0,0,0.6)" });
    $(".divB" + index).css({ "display": "none" });
    $(".eyeB" + index).css({ "display": "none" });
  });

  $(".divT" + j).css({ "display": "none" });
  $(".mdc-image-list__supporting" + j).css({ "background": "none" });
  $(".divB" + j).css({ "display": "block" });
  $(".eyeB" + j).css({ "display": "block" });

A better way would be to add a selected field to each image in the Images array:更好的方法是向Images数组中的每个图像添加一个selected字段:

{    
  ID:1,
  Name : "Name",
  Cat: "words",
  image: 'https://material-components-web.appspot.com/images/photos/3x2/16.jpg',
  selected: false,
}

and use that flag to toggle classes that control the display or hiding.并使用该标志来切换控制显示或隐藏的类。 then you will only set that flag to false in the loop I showed above and then set it to true for the clicked image, so your event handler function will become:那么您只需在我上面显示的循环中将该标志设置为 false,然后将其设置为单击图像的 true,因此您的事件处理函数将变为:

eventB(e, j) {
  this.Images.forEach((img, index) => {
    img.selected = index == j;
  });
}

In your html change this line:在您的 html 中更改此行:

<div class="mdc-image-list--with-text-protection">

To this:对此:

<div class="mdc-image-list--with-text-protection" [ngClass]="{'selected': product.selected}">

And add this to your CSS:并将其添加到您的 CSS:

.selected {
  .mdc-image-list__supporting{
    background: none;
  }

  .EyeG,
  .mdc-image-list__label,
  ImageButtonsG {
    display: block;
  }
}

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

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