我在html和css方面有問題。我需要制作一個圖形,其中紅色的球沿對角線上下移動,它必須沿著藍線的X位置移動(這個元素只向右或向左移動)。問題是滾動條1和2共享紅球的左邊位置...因此,它結束了錯誤,他們回到各自滾動條的百分比位置。 我怎么讓他們兩個分享同一個紅球的左邊位置?
document.addEventListener("DOMContentLoaded", function () {
const scrollBar1 = document.getElementById("scroll-bar1");
const scrollBar2 = document.getElementById("scroll-bar2");
const scrollBar3 = document.getElementById("scroll-bar3");
const scrollBar4 = document.getElementById("scroll-bar4");
const scrollBar5 = document.getElementById("scroll-bar5");
const scrollBar6 = document.getElementById("scroll-bar6");
const redBall = document.getElementById("red-ball");
const blueLine = document.getElementById("blue-line");
let leftBall
scrollBar1.addEventListener("input", function() {
// Verifica se o valor da scroll bar 1 foi alterado
if (scrollBar1.value !== scrollBar1.previousValue) {
const xPercentage = (scrollBar1.value - 100) / (200 - 100);
const xPosition = xPercentage * 100;
leftBall = xPosition
redBall.style.left = `${leftBall}%`;
// Atualiza a posi??o da bola vermelha na vertical (scroll bar 1)
const yPercentage = (scrollBar1.value - 100) / (200 - 100);
const yPosition = yPercentage * 100;
redBall.style.top = `${yPosition}%`;
console.log(redBall.style.left)
}
});
scrollBar2.addEventListener("input", function() {
// Verifica se o valor da scroll bar 1 foi alterado
if (scrollBar2.value !== scrollBar2.previousValue) {
const blueLinePercentage = (scrollBar2.value - 400) / (0 - 400);
const blueLinePosition = blueLinePercentage * 100;
//leftBall = blueLinePosition
blueLine.style.left = `${80 - blueLinePosition}%`;
redBall.style.left = `${100-blueLinePosition}%`;
console.log(redBall.style.left)
}
});
});
.container {
text-align: center;
}
#chart {
position: relative;
width: 450px;
height: 450px;
margin: 0 auto;
background-color: #f2f2f2;
border: 1px solid #ccc;
}
#red-ball {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
transform: rotate(45deg);
}
#black-line {
position: absolute;
top: 50%;
left: 30%;
transform: translateY(-50%, -50%);
width: 40%;
height: 2px;
background-color: black;
transform: rotate(45deg);
}
#blue-line {
position: absolute;
top: 50%;
left: 30%;
transform: translateY(-50%, -50%);
width: 40%;
height: 2px;
background-color: blue;
transform: rotate(45deg);
}
#x-axis {
position: absolute;
bottom: -20px;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
}
#y-axis {
position: absolute;
top: 0;
left: -30px;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.axis-label {
font-size: 12px;
}
.scroll-bar {
margin: 20px auto;
width: 400px;
display: flex;
align-items: center;
}
.scroll-bar input[type="range"] {
flex-grow: 1;
}
.scroll-bar-value {
margin-left: 10px;
font-size: 12px;
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gráfico de Demanda por Carne Moída</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<h1>Gráfico de Demanda por Carne Moída</h1>
<div id="chart">
<div id="red-ball"></div>
<div id="black-line"></div>
<div id="blue-line"></div>
<div id="x-axis">
<span class="axis-label">6,292</span>
<span class="axis-label">10,292</span>
<span class="axis-label">14,292</span>
</div>
<div id="y-axis">
<span class="axis-label">90</span>
<span class="axis-label">140</span>
<span class="axis-label">190</span>
</div>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar1" min="100" max="200" value="150">
<span id="scroll-bar-value1" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar2" min="0" max="400" value="200">
<span id="scroll-bar-value2" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar3" min="100" max="300" value="200">
<span id="scroll-bar-value3" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar4" min="100" max="400" value="180">
<span id="scroll-bar-value4" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar5" min="10" max="200" value="30">
<span id="scroll-bar-value5" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar6" min="0" max="200" value="100">
<span id="scroll-bar-value6" class="scroll-bar-value"></span>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
在同一個回調中管理所有變量讓我對這個問題更加清楚。
這并不容易,但我想我做到了:
document.addEventListener("DOMContentLoaded", function () {
const scrollBar1 = document.getElementById("scroll-bar1");
const scrollBar2 = document.getElementById("scroll-bar2");
const scrollBar3 = document.getElementById("scroll-bar3");
const scrollBar4 = document.getElementById("scroll-bar4");
const scrollBar5 = document.getElementById("scroll-bar5");
const scrollBar6 = document.getElementById("scroll-bar6");
const redBall = document.getElementById("red-ball");
const blueLine = document.getElementById("blue-line");
scrollBar1.addEventListener("input", updatePos);
scrollBar2.addEventListener("input", updatePos);
function updatePos(){
const xPercentage = (scrollBar1.value - 100) / (200 - 100);
const xPosition = xPercentage * 100;
let leftBall = xPosition+20;
// Atualiza a posi??o da bola vermelha na vertical (scroll bar 1)
const yPercentage = (scrollBar1.value - 100) / (200 - 100);
const yPosition = yPercentage * 100;
const blueLinePercentage = (scrollBar2.value - 400) / (0 - 400);
const blueLinePosition = blueLinePercentage * 100;
leftBall -= blueLinePosition-30;
redBall.style.top = `${yPosition}%`;
redBall.style.left = `${leftBall}%`;
blueLine.style.left = `${80 - blueLinePosition}%`;
}
});
.container {
text-align: center;
}
#chart {
position: relative;
width: 450px;
height: 450px;
margin: 0 auto;
background-color: #f2f2f2;
border: 1px solid #ccc;
}
#red-ball {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
transform: rotate(45deg);
}
#black-line {
position: absolute;
top: 50%;
left: 30%;
transform: translateY(-50%, -50%);
width: 40%;
height: 2px;
background-color: black;
transform: rotate(45deg);
}
#blue-line {
position: absolute;
top: 50%;
left: 30%;
transform: translateY(-50%, -50%);
width: 40%;
height: 2px;
background-color: blue;
transform: rotate(45deg);
}
#x-axis {
position: absolute;
bottom: -20px;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
}
#y-axis {
position: absolute;
top: 0;
left: -30px;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.axis-label {
font-size: 12px;
}
.scroll-bar {
margin: 20px auto;
width: 400px;
display: flex;
align-items: center;
}
.scroll-bar input[type="range"] {
flex-grow: 1;
}
.scroll-bar-value {
margin-left: 10px;
font-size: 12px;
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gráfico de Demanda por Carne Moída</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<h1>Gráfico de Demanda por Carne Moída</h1>
<div id="chart">
<div id="red-ball"></div>
<div id="black-line"></div>
<div id="blue-line"></div>
<div id="x-axis">
<span class="axis-label">6,292</span>
<span class="axis-label">10,292</span>
<span class="axis-label">14,292</span>
</div>
<div id="y-axis">
<span class="axis-label">90</span>
<span class="axis-label">140</span>
<span class="axis-label">190</span>
</div>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar1" min="100" max="200" value="150">
<span id="scroll-bar-value1" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar2" min="0" max="400" value="200">
<span id="scroll-bar-value2" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar3" min="100" max="300" value="200">
<span id="scroll-bar-value3" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar4" min="100" max="400" value="180">
<span id="scroll-bar-value4" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar5" min="10" max="200" value="30">
<span id="scroll-bar-value5" class="scroll-bar-value"></span>
</div>
<div class="scroll-bar">
<input type="range" id="scroll-bar6" min="0" max="200" value="100">
<span id="scroll-bar-value6" class="scroll-bar-value"></span>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
上一篇c 數組序列化json
下一篇mvvm設計模式 vue