首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >活动图像标签图片库

问活动图像标签图片库
EN

Stack Overflow用户
提问于 2022-04-22 15:20:21
回答 2查看 73关注 0票数 1

我有一个标签库(从W3Schools),你可以选择一个图像显示在下面。我在想,在我选择之后,如何为所选的图像创建一个活动类。就像在navbar上看到它一样(这个例子显示页面是打开的)。(主页pages about contact)我只需要给点击的图像任何类(不透明度或颜色),我尝试了很多东西,但在JavaScript中还是新的,无法实现我想要的:

​

代码语言:javascript
复制
function myFunction(imgs) {
  var expandImg = document.getElementById("expandedImg");
  var imgText = document.getElementById("imgtext");
  expandImg.src = imgs.src;
  imgText.innerHTML = imgs.alt;
  expandImg.parentElement.style.display = "block";
}
代码语言:javascript
复制
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial;
}

/* The grid: Four equal columns that floats next to each other */
.column {
  float: left;
  width: 25%;
  padding: 10px;
}

/* Style the images inside the grid */
.column img {
  opacity: 0.8; 
  cursor: pointer; 
}

.column img:hover {
  opacity: 1;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* The expanding image container */
.container {
  position: relative;
  display: none;
}

/* Expanding image text */
#imgtext {
  position: absolute;
  bottom: 15px;
  left: 15px;
  color: white;
  font-size: 20px;
}

/* Closable button inside the expanded image */
.closebtn {
  position: absolute;
  top: 10px;
  right: 15px;
  color: white;
  font-size: 35px;
  cursor: pointer;
}
代码语言:javascript
复制
<div style="text-align:center">
  <h2>Tabbed Image Gallery</h2>
  <p>Click on the images below:</p>
</div>

<!-- The four columns -->
<div class="row">
  <div class="column">
    <img src="https://picsum.photos/200" alt="Nature" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/222" alt="Snow" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/201" alt="Mountains" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/209" alt="Lights" style="width:100%" onclick="myFunction(this);">
  </div>
</div>

<div class="container">
  <span onclick="this.parentElement.style.display='none'" class="closebtn">&times;</span>
  <img id="expandedImg" style="width:100%">
  <div id="imgtext"></div>
</div>

​

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-22 16:00:10

您可以使用添加所需类的event listener来完成这一任务,并将它们移除。您可以使用event.target访问触发侦听器的元素,并相应地添加/删除类:

代码语言:javascript
复制
// get the images that we want to listen for clicks from
let img = document.querySelectorAll(".column img");

// add an event listener to each image
img.forEach(function(elem) {
  // when one of the images is clicked, add the "your-class" class to it and remove the class from any other elements
  elem.addEventListener("click", function(event) {
    // check if any other elements have the class and remove it if some do
    if (document.querySelectorAll(".your-class").length != 0) {
      document.querySelector(".your-class").classList.remove("your-class");
      // if there are no other elements with "your-class" do nothing
    } else {}
    // add "your-class" to the element clicked
    event.target.classList.add("your-class")
  });
});

在下面的代码片段中,我通过拥有一个类来演示这一点,该类在单击活动图像边框时生成青色:

​

代码语言:javascript
复制
// get the images that we want to listen for clicks from
let img = document.querySelectorAll(".column img");

// add an event listener to each image
img.forEach(function(elem) {
  // when one of the images is clicked, add the border-cyan class to it and remove the class from any other elements
  elem.addEventListener("click", function(event) {
    // check if any other elements have the class and remove it if some do
    if (document.querySelectorAll(".border-cyan").length != 0) {
      document.querySelector(".border-cyan").classList.remove("border-cyan");
      // if there are no other elements with the border cyan class do nothing
    } else {}
    // add the border cyan class to the element clicked
    event.target.classList.add("border-cyan")
  });
});

function myFunction(imgs) {
  var expandImg = document.getElementById("expandedImg");
  var imgText = document.getElementById("imgtext");
  expandImg.src = imgs.src;
  imgText.innerHTML = imgs.alt;
  expandImg.parentElement.style.display = "block";
}
代码语言:javascript
复制
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial;
}


/* The grid: Four equal columns that floats next to each other */

.column {
  float: left;
  width: 25%;
  padding: 10px;
}


/* Style the images inside the grid */

.column img {
  opacity: 0.8;
  cursor: pointer;
}

.column img:hover {
  opacity: 1;
}


/* Clear floats after the columns */

.row:after {
  content: "";
  display: table;
  clear: both;
}


/* The expanding image container */

.container {
  position: relative;
  display: none;
}


/* Expanding image text */

#imgtext {
  position: absolute;
  bottom: 15px;
  left: 15px;
  color: white;
  font-size: 20px;
}


/* Closable button inside the expanded image */

.closebtn {
  position: absolute;
  top: 10px;
  right: 15px;
  color: white;
  font-size: 35px;
  cursor: pointer;
}

.border-cyan {
  border: 3px solid cyan;
}
代码语言:javascript
复制
<div style="text-align:center">
  <h2>Tabbed Image Gallery</h2>
  <p>Click on the images below:</p>
</div>

<!-- The four columns -->
<div class="row">
  <div class="column">
    <img src="https://picsum.photos/200" alt="Nature" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/222" alt="Snow" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/201" alt="Mountains" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/209" alt="Lights" style="width:100%" onclick="myFunction(this);">
  </div>
</div>

<div class="container">
  <span onclick="this.parentElement.style.display='none'" class="closebtn">&times;</span>
  <img id="expandedImg" style="width:100%">
  <div id="imgtext"></div>
</div>

​

票数 2
EN

Stack Overflow用户

发布于 2022-04-22 16:01:20

  1. ,首先您需要找到所有图像并从它们中删除类
  2. ,然后添加用于单击的图像

的添加类。

​

代码语言:javascript
复制
function myFunction(img) {
  var expandImg = document.getElementById("expandedImg");
  var imgText = document.getElementById("imgtext");
  expandImg.src = img.src;
  imgText.innerHTML = img.alt;
  expandImg.parentElement.style.display = "block";
  img.closest('.row').querySelectorAll('img').forEach(img => img.classList.remove('yourClassName'))
  img.classList.add('yourClassName')
}
代码语言:javascript
复制
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial;
}

/* The grid: Four equal columns that floats next to each other */
.column {
  float: left;
  width: 25%;
  padding: 10px;
}

/* Style the images inside the grid */
.column img {
  opacity: 0.8; 
  cursor: pointer; 
}

.column img:hover {
  opacity: 1;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* The expanding image container */
.container {
  position: relative;
  display: none;
}

/* Expanding image text */
#imgtext {
  position: absolute;
  bottom: 15px;
  left: 15px;
  color: white;
  font-size: 20px;
}

/* Closable button inside the expanded image */
.closebtn {
  position: absolute;
  top: 10px;
  right: 15px;
  color: white;
  font-size: 35px;
  cursor: pointer;
}

.yourClassName {
border: 1px solid red;
}
代码语言:javascript
复制
<div style="text-align:center">
  <h2>Tabbed Image Gallery</h2>
  <p>Click on the images below:</p>
</div>

<!-- The four columns -->
<div class="row">
  <div class="column">
    <img src="https://picsum.photos/200" alt="Nature" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/222" alt="Snow" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/201" alt="Mountains" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="https://picsum.photos/209" alt="Lights" style="width:100%" onclick="myFunction(this);">
  </div>
</div>

<div class="container">
  <span onclick="this.parentElement.style.display='none'" class="closebtn">&times;</span>
  <img id="expandedImg" style="width:100%">
  <div id="imgtext"></div>
</div>

​

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71971093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档