在它的容器的左边有一个按钮,文本在右边!当按钮位于左侧时,单击它将导致按钮移动到容器的右侧。当按钮在右边时,按钮文本向左转!当按钮位于右侧时,单击它将导致按钮移动到容器的左侧。
我试过这样做: html:
<body>
<div class="container">
<button id="flip-flop" onclick="moveRight()">Go Right!</button>
</div>
</body>js文件:
function moveRight(){
const flip_flip_button = document.getElementById("flip-flop")
flip_flip_button.addEventListener("click", function () {
flip_flip_button.style.left = 400 + "px";
});
}css:
.container {
position: fixed;
top: 50%;
left: 50%;
width: 50%;
background-color: gray;
transform: translate(-50%, -50%);
}
#flip-flop{
position: relative;
}这个代码结果,按钮是向右移动的(通过第二次单击?我也不知道为什么),但responsive.How不能,我可以移动右边的按钮,直到容器右边?
发布于 2022-03-26 18:48:03
这里有几个问题。
element.style,因为这会产生内联样式,人们普遍认为这是非常糟糕的实践。相反,在CSS中准备一个包含所需样式的CSS类,然后单击该类。text-align: right。
document
.getElementById('flip-flop')
.addEventListener("click", function() {
this.parentNode.classList.toggle('right');
});.container {
position: fixed;
top: 50%;
left: 50%;
width: 50%;
background-color: gray;
transform: translate(-50%, -50%);
}
#flip-flop {
position: relative;
}
.right {
text-align: right;
}
#flip-flop::after {
content: " right!";
}
.right #flip-flop::after {
content: " left!";
}<div class="container">
<button id="flip-flop">Go</button>
</div>
https://stackoverflow.com/questions/71630784
复制相似问题