我在YouTube上发现了一个弹出式图库教程,我想首先反转屏幕上显示的内容.

从现在起,当用户在他们的浏览器中打开网站时,他们会看到一个图片库预览,可以点击一张图片来查看完整的图片.我想改变这一点,让第一张照片显示完整的图像.

我只是try 重新排序div,因为我不知道从哪里开始实现我的目标.有谁能帮忙吗?先谢谢你.

const images = [...document.querySelectorAll('.image')];
const popup = document.querySelector('.popup');
const closeBtn = document.querySelector('.close-btn');
const imageName = document.querySelector('.image-name');
const largeImage = document.querySelector('.large-image');
const imageIndex = document.querySelector('.index');
const leftArrow = document.querySelector('.left-arrow');
const rightArrow = document.querySelector('.right-arrow');

let index = 0;

images.forEach((item, i) => {
  item.addEventListener('click', () => {
    updateImage(i);
    popup.classList.toggle('active');
  })
})

const updateImage = (i) => {
  let path = `img/img${i+1}.png`;
  largeImage.src = path;
  imageName.innerHTML = path;
  imageIndex.innerHTML = `0${i+1}`;
  index = i;
}

closeBtn.addEventListener('click', () => {
  popup.classList.toggle('active');
})

leftArrow.addEventListener('click', () => {
  if (index > 0) {
    updateImage(index - 1);
  }
})

rightArrow.addEventListener('click', () => {
  if (index < images.length - 1) {
    updateImage(index + 1);
  }
})
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

*:focus {
  outline: none;
}

body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ff7a2d;
  font-family: 'roboto', sans-serif;
}

.gallery {
  width: 80%;
  height: 90vh;
  max-width: 1600px;
  max-height: 800px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.gallery-image {
  width: 30%;
  height: calc(50% - 20px);
  min-width: 300px;
  min-height: 200px;
  margin: 10px;
  overflow: hidden;
}

.image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: 1s;
}

.popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  width: 80%;
  max-width: 1600px;
  height: 90vh;
  max-height: 800px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 5;
  overflow: hidden;
  transition: 1s;
  opacity: 0;
}

.popup.active {
  transform: translate(-50%, -50%) scale(1);
  opacity: 1;
}

.popup.active .close-btn,
.popup.active .image-name,
.popup.active .index,
.popup.active .large-image,
.popup.active .arrow-btn {
  opacity: 1;
  transition: opacity .5s;
  transition-delay: 1s;
}

.top-bar {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background: #000;
  color: #fff;
  text-align: center;
  line-height: 50px;
  font-weight: 300;
}

.image-name {
  opacity: 0;
}

.close-btn {
  opacity: 0;
  position: absolute;
  top: 15px;
  right: 20px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #f00;
  cursor: pointer;
}

.arrow-btn {
  opacity: 0;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  padding: 10px;
  border-radius: 50%;
  border: none;
  background: none;
  cursor: pointer;
}

.left-arrow {
  left: 10px;
}

.right-arrow {
  right: 10px;
  transform: translateY(-50%) rotate(180deg);
}

.arrow-btn:hover {
  background: rgba(0, 0, 0, 0.5);
}

.index {
  position: absolute;
  bottom: 10px;
  right: 10px;
  font-size: 80px;
  font-weight: 100;
  color: rgba(255, 255, 255, 0.4);
  opacity: 0;
}

.large-image {
  margin-top: 5%;
  width: 80%;
  height: 80%;
  object-fit: contain;
  opacity: 0;
}
<div class="popup">
  <div class="top-bar">
    <p class="image-name">img1.png</p>
    <span class="close-btn"></span>
  </div>
  <button class="arrow-btn left-arrow"><img src="img/arrow.png" alt=""></button>
  <button class="arrow-btn right-arrow"><img src="img/arrow.png" alt=""></button>
  <img src="img/img1.png" class="large-image" alt="">
  <h1 class="index">01</h1>
</div>
<div class="gallery">
  <div class="gallery-image">
    <img src="img/img1.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img2.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img3.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img4.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img5.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img6.png" alt="" class="image">
  </div>
</div>

推荐答案

要完成所需操作,您可以提取点击处理程序中的逻辑,并在页面加载时在第0个图像上调用它:

const setPopupImage = index => {
  updateImage(index);
  popup.classList.toggle('active');
}
setPopupImage(0);

下面是一个完整的工作示例:

const images = [...document.querySelectorAll('.image')];
const popup = document.querySelector('.popup');
const closeBtn = document.querySelector('.close-btn');
const imageName = document.querySelector('.image-name');
const largeImage = document.querySelector('.large-image');
const imageIndex = document.querySelector('.index');
const leftArrow = document.querySelector('.left-arrow');
const rightArrow = document.querySelector('.right-arrow');

let index = 0;

images.forEach((item, i) => {
  item.addEventListener('click', () => setPopupImage(i));
})

const updateImage = (i) => {
  let path = `img/img${i+1}.png`;
  largeImage.src = path;
  imageName.innerHTML = path;
  imageIndex.innerHTML = `0${i+1}`;
  index = i;
}

closeBtn.addEventListener('click', () => {
  popup.classList.toggle('active');
})

leftArrow.addEventListener('click', () => {
  if (index > 0) {
    updateImage(index - 1);
  }
})

rightArrow.addEventListener('click', () => {
  if (index < images.length - 1) {
    updateImage(index + 1);
  }
})

const setPopupImage = index => {
  updateImage(index);
  popup.classList.toggle('active');
}
setPopupImage(0);
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

*:focus {
  outline: none;
}

body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ff7a2d;
  font-family: 'roboto', sans-serif;
}

.gallery {
  width: 80%;
  height: 90vh;
  max-width: 1600px;
  max-height: 800px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.gallery-image {
  width: 30%;
  height: calc(50% - 20px);
  min-width: 300px;
  min-height: 200px;
  margin: 10px;
  overflow: hidden;
}

.image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: 1s;
}

.popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  width: 80%;
  max-width: 1600px;
  height: 90vh;
  max-height: 800px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 5;
  overflow: hidden;
  transition: 1s;
  opacity: 0;
}

.popup.active {
  transform: translate(-50%, -50%) scale(1);
  opacity: 1;
}

.popup.active .close-btn,
.popup.active .image-name,
.popup.active .index,
.popup.active .large-image,
.popup.active .arrow-btn {
  opacity: 1;
  transition: opacity .5s;
  transition-delay: 1s;
}

.top-bar {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background: #000;
  color: #fff;
  text-align: center;
  line-height: 50px;
  font-weight: 300;
}

.image-name {
  opacity: 0;
}

.close-btn {
  opacity: 0;
  position: absolute;
  top: 15px;
  right: 20px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #f00;
  cursor: pointer;
}

.arrow-btn {
  opacity: 0;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  padding: 10px;
  border-radius: 50%;
  border: none;
  background: none;
  cursor: pointer;
}

.left-arrow {
  left: 10px;
}

.right-arrow {
  right: 10px;
  transform: translateY(-50%) rotate(180deg);
}

.arrow-btn:hover {
  background: rgba(0, 0, 0, 0.5);
}

.index {
  position: absolute;
  bottom: 10px;
  right: 10px;
  font-size: 80px;
  font-weight: 100;
  color: rgba(255, 255, 255, 0.4);
  opacity: 0;
}

.large-image {
  margin-top: 5%;
  width: 80%;
  height: 80%;
  object-fit: contain;
  opacity: 0;
}
<div class="popup">
  <div class="top-bar">
    <p class="image-name">img1.png</p>
    <span class="close-btn"></span>
  </div>
  <button class="arrow-btn left-arrow"><img src="img/arrow.png" alt=""></button>
  <button class="arrow-btn right-arrow"><img src="img/arrow.png" alt=""></button>
  <img src="img/img1.png" class="large-image" alt="">
  <h1 class="index">01</h1>
</div>
<div class="gallery">
  <div class="gallery-image">
    <img src="img/img1.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img2.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img3.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img4.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img5.png" alt="" class="image">
  </div>
  <div class="gallery-image">
    <img src="img/img6.png" alt="" class="image">
  </div>
</div>

Javascript相关问答推荐

功能和普通对象之间的原型污染

如何在JavaScript中通过一次单击即可举办多个活动

JS、C++和C#给出不同的Base 64 Guid编码结果

Vue:ref不会创建react 性属性

防止用户在selectizeInput中取消 Select 选项

Plotly热图:在矩形上zoom 后将zoom 区域居中

如何在不创建新键的情况下动态更改 map 中的项目?

WebRTC关闭navigator. getUserMedia正确

使用Promise.All并发解决时,每个promise 的线性时间增加?

当输入字段无效时,我的应用程序不会返回错误

映射类型定义,其中值对应于键

根据一个条件,如何从处理过的数组中移除一项并将其移动到另一个数组?

打字脚本中方括号符号属性访问和拾取实用程序的区别

如何根据输入数量正确显示alert ?

Django导入问题,无法导入我的应用程序,但我已在设置中安装了它

是否设置以JavaScript为背景的画布元素?

如何正确地在ComponentWillUnmount中卸载状态以避免内存泄漏?

在Press Reaction本机和EXPO av上播放单个文件

如何在加载页面时使锚定标记成为焦点

输入数据覆盖JSON文件