【问题标题】:element.style.backgroundImage = `url(${url})`; not workingelement.style.backgroundImage = `url(${url})`;不工作
【发布时间】:2021-05-22 23:29:25
【问题描述】:

所有。我遇到了一个关于如何动态更改 div 的背景图像的问题。当我单击下一步按钮时,我想更改背景图像;但是,语法 'disk.style.backgroundImage = "url(xxx)" 不起作用;我该如何解决?下面是对应的代码,希望大家帮我解决一下。

HTML 代码:

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

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,这部分我想动态改变背景图片。


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();
});

【问题讨论】:

  • What Do You Mean “It Doesn’t Work”? 使用browser console (dev tools)(点击F12)并阅读任何错误。开发工具提供了一个 Inspector / Elements 和一个 Network 选项卡。请确认:资源是否找到(例如 HTTP 200 响应)?如果不是,则请求哪个实际 URL?确保您的背景位于 正高和正宽可见元素上。
  • 点击下一个按钮后,div 不会更改其背景图像,并且保持不变。并且浏览器控制台没有错误,所有图片都存储在本地机器中。图片没有 HTTP 响应。
  • 即使在本地,“网络”选项卡中也始终有一个日志条目。元素 Inspector 揭示了什么?
  • 我不知道为什么,所有其他图像都有HTTP响应;但是一旦我单击按钮,我想要更改的图像就没有 HTTP 响应代码。并且原图保持不变

标签: javascript html css html5-audio


【解决方案1】:

这个 sn-p 证明使用按钮单击循环浏览背景图像没有问题。这意味着您的问题不完整,问题出在其他地方。
除非您提供更多详细信息,否则我们将无法回答您的问题。

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>

【讨论】:

  • asker 的问题是他们没有定义const disk = document.getElementById("disk");
  • @VC.One 我认为它没有包含在帖子中,但它已在其他地方定义,因为他的评论说“浏览器控制台中没有错误”。但你可能是对的! ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
相关资源
最近更新 更多