javascript - 使用 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=生成的元素的引用。
解决方案
而不是
img.style.background = "";
试试这个:
img.style.background = "transparent";
或者你可以试试这个:
img.style.display = "none";
推荐阅读
- android - Push Kit 何时会包含 Android 12 所需的 android:exported 属性?
- ruby-on-rails - 想确定我们在 ROR 中是否有内存泄漏
- mxgraph - mxgraph 中的多个布局
- php - 为什么我的 PHP 页面通过重定向它的页面提交表单?
- android - 是否可以在 Android 中为应用内购买编写单元测试?
- javascript - 如何更改所选时间的颜色时间选择器?
- javascript - 将 JSON 从视图发送到控制器 ASP.NET MVC 会给出空值
- validation - 我不想在两个字段更新时触发验证规则
- java - 如何在 Jersey Web 应用程序中获取文件资源并执行自定义 JSON Marshaller?
- javascript - 如何使用函数返回星期几?