javascript - 在循环中使用 .getAttribute 添加样式
问题描述
第一次在这里发帖。希望有人能帮忙?!
我正在尝试将样式添加到我通过在 HTML 中添加数据属性然后使用 JS 在轮播中循环遍历每个图像来创建的视差轮播中。我能够将“数据背景”添加到内联样式中,但“数据位置”不是。
预期产出
<div class="parallax-container parallax-scale"
data-background="//localhost:3000/wp-content/uploads/2020/03/animage.jpg"
data-position="50% 50%" data-swiper-parallax="1130.25"
style="transform: translate3d(0px, 0px, 0px); background-image: url("//localhost:3000/wp-content/uploads/2020/03/animage.jpg"); background-position: 50% 50%;">
var list = document.getElementsByClassName('parallax-container');
for (var i = 0; i < list.length; i++) {
var src = list[i].getAttribute('data-background');
list[i].style.backgroundImage = "url('" + src + "')";
var pos = list[i].getAttribute('data-position');
list[i].style.backgroundPosition = pos;
console.log(list[i])
}
<div class="parallax-container parallax-scale" data-background="//localhost:3000/wp-content/uploads/2020/03/animage.jpg" data-position="50% 50%">
解决方案
推荐阅读
- python - 比较两个列表并在发生更改时打印出来
- angularjs - 如何将 ng-if 逻辑写入控制器
- asp.net - Asp.Net RDLC ReportViewer 内存问题
- javascript - 使用 PHP 和 AJAX 在数据库中增加值
- grafana - 如何使用 Grafana 正确监控 AWS 上的 ELB 延迟?
- javascript - 在 UI 中更改时更改 reducer 上的复选框数组的状态
- css - 在 prestashop 中缩小 CSS 和 JS
- javascript - firebase auth 是否可以同时更新用户和自定义声明?
- sql - 如何计算表中的实例数(Countifs)
- javascript - 未捕获的错误:#90211 ExtensionName 错误 - 使用 FusionCharts 的 Node.JS、Heroku、JavaScript 应用程序