首页 > 解决方案 > 使用 JS 在 CSS 中删除加载栏作为背景图像

问题描述

我有一个定制的加载栏。我通过将他隐藏在照片背后来使用它,因此每当照片更改时,背景就会出现,并且当照片加载时,加载栏会隐藏在照片后面。最近我不得不使用传输图像,现在图像后面可以看到加载栏。

照片的 id 是“生成的”,这是 css。

#generated {
    border: 1px solid #021a40;
    cursor: pointer;
    background: url(../images/loading400px.gif) 50% no-repeat;
}

我有一个改变这个图像的 JS 函数。

function AsynchronouslyDownloadImage(address, img) {
    img.src = "static/images/shader.svg";
    let downloadingImage = new Image();
    downloadingImage.onload = function () {
        // This img.style.background is what I thought will make my background disappear but this isn't working
        img.style.background = "";
        img.src = this.src;
    };

    downloadingImage.src = imagesEndpoint;
    }

根据长评论,我认为 ïmg.style.background 会起作用。img 是对他的 id=生成的元素的引用。

标签: javascripthtmlcss

解决方案


而不是 img.style.background = ""; 试试这个: img.style.background = "transparent"; 或者你可以试试这个: img.style.display = "none";


推荐阅读