简体   繁体   English

element.style.backgroundImage = `url(${url})`; 不工作

[英]element.style.backgroundImage = `url(${url})`; not working

all.全部。 I meet a problem about how dynamically change a background image of a div.我遇到了一个关于如何动态更改 div 的背景图像的问题。 When I click the next button, I want to change the background image;当我单击下一步按钮时,我想更改背景图像; however, the syntax 'disk.style.backgroundImage = "url(xxx)" does not working;但是,语法 'disk.style.backgroundImage = "url(xxx)" 不起作用; how could i fix it?我该如何解决? The below is corresponding codes, hope you guys could help me solve it.下面是对应的代码,希望大家帮我解决。

HTML CODE: HTML 代码:

<div id="disk" class="player--info__left"></div>

CSS CODE CSS 代码

        .player--info__left{
            position: absolute;
            width: 5.5rem;
            height: 5.5rem;
            border: 1px solid #fff;
            border-radius: 50%;
            padding: 2px;
            top: -1.5rem;
            left: -1.5rem;
            margin: 0 4px 2px 2px;
            background-image: url('/images/IMG_4328.JPG');
            background-size: cover;
            object-fit: cover;
            &:hover{
                transform: rotate(360deg);
                transition: all 1s ease-in-out;
            }
            &::after{
                content: '';
                position: absolute;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                border-radius: 50%;
                border: 3px solid rebeccapurple;
            }
        }

JS CODE, in this part I want to change the background image dynamically. JS 代码,在这部分我想动态改变背景图像。


const songList = [
    {
        src: '/music/Gymnopédies.mp3',
        img: '/music-img/Gymnopédies.jpg',
        title: 'Gymnopédies',
        singer: 'Erik Satie',
    },
    {
        src: '/music/Old Town Road.mp3',
        img: '/music-img/Lil Nas X.png',
        title: 'Old Town Road',
        singer: 'Lil Nas X',
    },
    {
        src: '/music/Daddy.mp3',
        img: '/music-img/Daddy.png',
        title: 'Daddy',
        singer: 'Coldplay',
    },
];

let index = 0;
next.addEventListener('click', function () {
    ++index;
    if (index >= songList.length - 1) {
        index = 0;
    }
    // isPlaying = true;
    console.log(disk);
    console.log(diskp);
    disk.style.backgroundImage = `url(${songList[index].img})`;

    playSong();
    playerButton.click();
});

This snippet proves that there is no problem cycling through background images using a button click.此片段证明使用按钮单击循环浏览背景图像没有问题。 Meaning your question is incomplete, the problem lies elsewhere.这意味着您的问题不完整,问题出在其他地方。
We won't be able to answer your question unless you provide more details.除非您提供更多详细信息,否则我们将无法回答您的问题。

 const imageUrls = [ "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/220px-FullMoon2010.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/The_Earth_seen_from_Apollo_17.jpg/220px-The_Earth_seen_from_Apollo_17.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/OSIRIS_Mars_true_color.jpg/220px-OSIRIS_Mars_true_color.jpg", ]; let index = 0; function onButtonClick() { const disk = document.getElementById("disk"); disk.style.backgroundImage = `url(${imageUrls[index]})`; index++; if (index >= imageUrls.length) index = 0; }
 #disk { width: 220px; height: 220px; }
 <button onclick="onButtonClick()">Click me repeatedly to cycle through images</button> <div id="disk" class="player--info__left"></div>

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

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