css 如何为照片库设置不同颜色的背景?

jv2fixgn  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(133)

我目前正在进行一个照片库项目,并希望为每个图像设置不同的颜色背景。我一共有六张图片,截至目前,所有图片的背景都是粉红色的。我尝试在CSS中添加另一个.popup类(包含我的图像的类),并使用新的背景色,但没有成功。我还尝试在JavaScript中使用getElementsByClassName来更改用户单击左箭头或右箭头时的颜色,这确实将颜色从粉红色更改为另一种颜色。但是,它不允许我为每个背景设置颜色。有人能帮忙吗?先谢谢你了。

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; // will track our current image;

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);
    }
})
const setPopupImage = index => {
  
  popup.classList.toggle('active');
}
setPopupImage(0);
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

*:focus{
  outline: none;
}

body{
  width: 100vw;
  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: 100vw;
  height: 100vh;
  background:  pink;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 5;
  overflow: hidden;
  transition: 1s;
  opacity: 0;
}
.bk{
  width: 100vw;
  height: 100vh;
  background: brown;
}

.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: 10px;
  background: turquoise;
  color: #fff;
  text-align: center;
  line-height: 50px;
  font-weight: 300;
}

.image-name{
  opacity: 0;
}

  

.close{
  position: absolute;
  top: 35px;
  right: 70px;
  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{
  width: 90%;
  height: 90%;
  object-fit: contain;
  opacity: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Popup</title>

    <link rel="stylesheet" href="style.css">

</head>
<body>
    <div class="popup">
       
        <div class="top-bar">
            <p class="image-name">img1.png</p>
            <span class="close-btn">
                <img src="Graphics/gallery_icon.svg" width="50px" class="close">
            </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 class="bk"
            ></div>
        </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>

    <script src="index.js"></script>
    
</body>
</html>
anauzrmj

anauzrmj1#

你必须在CSS文件中使用选择器:nth-child(n)Here's a link更好的解释。

.gallery-image:nth-child(1) {
  background: red;
} 

.gallery-image:nth-child(2) {
  background: green;
}

.gallery-image:nth-child(3) {
  background: blue;
}

如果这不是你要找的,告诉我。我很乐意帮忙

7cjasjjr

7cjasjjr2#

当你点击一张照片,它在一个弹出式模式中打开时,模式的背景每次都会不同。单击向左或向右箭头时,背景颜色将再次更改。
在你的JS代码中我添加了这一行

randomBackgroundColor(); //<-- NEW LINE

函数randomBackgroundColor()为popup元素的背景设置不同的颜色。
我希望我能帮上忙

示例:

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; // will track our current image;

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

        randomBackgroundColor(); //<-- NEW LINE
    })
})

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);
        randomBackgroundColor(); //<-- NEW LINE
    }
})

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

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

setPopupImage(0);


function randomBackgroundColor() {
    let red = Math.floor(Math.random() * 256);
    let green = Math.floor(Math.random() * 256);
    let blue = Math.floor(Math.random() * 256);

    let color = 'rgb(' + red + ', ' + green + ', ' + blue + ')';

    popup.style.backgroundColor = color;
}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

*:focus {
    outline: none;
}

body {
    width: 100vw;
    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: 100vw;
    height: 100vh;
    background: pink;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 5;
    overflow: hidden;
    transition: 1s;
    opacity: 0;
}

.bk {
    width: 100vw;
    height: 100vh;
    background: brown;
}

.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: 10px;
    background: turquoise;
    color: #fff;
    text-align: center;
    line-height: 50px;
    font-weight: 300;
}

.image-name {
    opacity: 0;
}

.close {
    position: absolute;
    top: 35px;
    right: 70px;
    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 {
    width: 90%;
    height: 90%;
    object-fit: contain;
    opacity: 0;
}
<div class="popup">

    <div class="top-bar">
        <p class="image-name">img1.png</p>
        <span class="close-btn">
            <img src="Graphics/gallery_icon.svg" width="50px" class="close">
        </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 class="bk"></div>
    </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>

相关问题