【问题标题】:Active image tab image gallary活动图像选项卡图库
【发布时间】:2023-02-11 01:04:59
【问题描述】:

我有一个标签库(来自 W3Schools),您可以在其中选择要显示的图像。我想知道如何在选择后为所选图像设置一个活动类。就像您在导航栏上看到它一样(此示例显示页面已打开)。 (家页数关于联系方式) 我只需要给点击的图像任何类(不透明度或颜色)我尝试了很多东西但仍然是 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";
}
* {
  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;
}
<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>

【问题讨论】:

  • @saliem_autoun 如果提供的答案之一满足您要求的行为,请将您的问题标记为已解决。

标签: javascript css


【解决方案1】:

您可以使用 event listener 添加您需要的任何类并删除它们。您可以使用 event.target 访问触发侦听器的元素并相应地添加/删除类:

// 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")
  });
});

在下面的代码 sn-p 中,我通过一个类在单击时将活动图像的边框更改为青色来演示这一点:

// 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";
}
* {
  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;
}
<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>

【讨论】:

  • 非常感谢,很有帮助,特别是你如何解释每一步! <3 <3
  • 没问题,总是乐于助人!
【解决方案2】:
  1. 首先您需要找到所有图像并从中删除类
  2. 之后为点击的图像添加类

    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')
    }
    * {
      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;
    }
    <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>

【讨论】:

  • 非常感谢,简单明了的解决方案,解决了我所需要的! <3<3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多