1 回答
TA贡献1770条经验 获得超3个赞
您必须添加和删除 css。
样本:
var dotContainer = document.getElementById("n788"); //THIS TO CONTAIN ALL ID VALUES FROM THE JSON DB
var dataRequest = new XMLHttpRequest();
dataRequest.open(
"get",
"https://raw.githubusercontent.com/sicronerver/sicronerve/master/dbn.json"
);
dataRequest.onload = function() {
var ourData = JSON.parse(dataRequest.responseText);
//setInterval(function(){
renderdata(ourData);
//}, 1000);
};
dataRequest.send();
function renderdata(dataobjectarray) {
document.querySelectorAll("div a").forEach(x => {
x.className = "dot";
});
var texString = "";
for (i = 0; i < dataobjectarray.length; i++) {
texString += dataobjectarray[i].id + ",";
}
dotContainer.insertAdjacentText("afterEnd", texString);
}
演示:
window.onload = () => {
function fetchData() {
fetch(
"https://raw.githubusercontent.com/sicronerver/sicronerve/master/dbn.json"
)
.then(res => res.json())
.then(renderdata);
}
function renderdata(data) {
document.querySelectorAll("div a").forEach(x => {
x.classList.remove("hide");
});
data.forEach(item => {
const elm = document.getElementById(item.id);
elm.classList.add("hide");
});
}
function start() {
setInterval(fetchData, 2000);
}
start();
};
.hide {
visibility: hidden;
opacity: 0;
}
.grid {
display: grid;
grid-template-columns: repeat(16, 1fr);
grid-template-rows: repeat(9, 1fr);
}
.dot {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #777777;
}
.dot:hover {
background-color: rgb(60, 255, 0);
opacity: 50%;
}
.dot:active {
background-color: #ff0000;
opacity: 50%;
}
#n786 {
position: relative;
grid-column: 3 / span 2;
grid-row: 6 / span 2;
}
#n787 {
position: relative;
grid-column: 6 / span 2;
grid-row: 6 / span 2;
}
#n788 {
position: relative;
grid-column: 9 / span 2;
grid-row: 6 / span 2;
}
#n789 {
position: relative;
grid-column: 12 / span 2;
grid-row: 6 / span 2;
}
#n790 {
position: relative;
grid-column: 15 / span 2;
grid-row: 6 / span 2;
}
<div class="grid">
<a id="n786" class="dot" href="#"></a>
<a id="n787" class="dot" href="#"></a>
<a id="n788" class="dot" href="#"></a>
<a id="n789" class="dot" href="#"></a>
<a id="n790" class="dot" href="#"></a>
</div>
- 1 回答
- 0 关注
- 83 浏览
添加回答
举报