console
const parent = document.querySelector('.parent');
const son = document.querySelector('.son');
const btn = document.querySelector('.btn');
const btn1 = document.querySelector('.btn1');
let x = 0;
let y = 0;
btn.onclick = function () {
y += 10;
parent.style.setProperty('--y', y + 'px')
}
btn1.onclick = function () {
x += 10;
son.style.setProperty('--x', x + 'px')
}
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="wrapper">
<div class="car">购物车</div>
<button class="btn">点击移动父元素</button>
<button class="btn btn1">点击移动子元素</button>
<div class="ball parent" style="--left:200px;--top:200px; --y:0px">
<span class="son" style="--x:0px">+</span>
</div>
</div>
<script src="./index.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
text-align: center;
}
html, body {
height: 100%;
}
:root {
--carSize: 70px;
--ballSize: 30px;
--btnSize: 100px;
--color: rgb(91, 167, 229)
}
.wrapper {
width: 375px;
height: calc(100% - 20px);
border: 1px solid;
position: relative;
box-sizing: border-box;
margin: 10px;
}
.car {
width: var(--carSize);
height: var(--carSize);
border-radius: 50%;
background-color: var(--color);
position: fixed;
bottom: 30px;
left: 30px;
font-size: 12px;
line-height: var(--carSize);
color: white;
}
.btn {
width: var(--btnSize);
height: calc(var(--btnSize) / 2);
background-color: var(--color);
border-radius: 3px;
color: white;
outline: none;
border: none;
position: absolute;
right: 20px;
top: 100px;
}
.ball {
left: var(--left);
top: var(--top);
width: var(--ballSize);
height: var(--ballSize);
border: 1px solid;
border-radius: 50%;
position: absolute;
transform: translateY(var(--y));
}
.ball span {
display: block;
width: var(--ballSize);
height: var(--ballSize);
border-radius: 50%;
background-color: var(--color);
font-size: 20px;
line-height: calc(var(--ballSize) - 4px);
font-weight: 700;
color: white;
transform: translateX(var(--x));
}
.btn1{
top: 30px
}