css 当我单击按钮时,它会移动,我该怎么弥补?

uplii1fm  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(125)

当我单击暂停/播放按钮时,它会移动。我该怎么弥补?

const btn = document.querySelector(".button");
if (btn === null) {
  throw new Error('could not find button');
}
btn.addEventListener("click", function() {
  btn.classList.toggle("paused");
  return false;
});
:root {
  --pauseButtonhight: 16px;
  --pauseButtonwidth: 13px;
  --pauseButtonborder: 8px;
}

.button {
  border: 0;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  padding: 0;
  height: var(--pauseButtonhight);
  border-color: transparent transparent transparent #7083d8;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
}

.button.paused {
  padding-top: 8px;
  border-style: double;
  border-width: 0px 0 0px var(--pauseButtonwidth);
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}
<div>
  <label for="playspersecond">Updates per second: </label>
  <input type="number" id="playspersecond" value="5" min="0" max="10" />
  <button class="button" id="play"></button>
</div>

当您按下该按钮时,它会上下移动,并同时移动旁边的文本。我想这与我如何在不同的状态下绘制按钮有关,但我是CSS/HTML的新手,所以我不知道该怎么做。这是this pause button的编辑版本
如果你有一个更好/正确的方法来做暂停/播放按钮,那也会起作用。

wljmcqd8

wljmcqd81#

将按钮的垂直对齐设置为middle(或top):

const btn = document.querySelector(".button");
if (btn === null) {
  throw new Error('could not find button');
}
btn.addEventListener("click", function() {
  btn.classList.toggle("paused");
  return false;
});
:root {
  --pauseButtonhight: 16px;
  --pauseButtonwidth: 13px;
  --pauseButtonborder: 8px;
}

.button {
  border: 0;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  padding: 0;
  height: var(--pauseButtonhight);
  border-color: transparent transparent transparent #7083d8;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
  vertical-align: middle;
}

.button.paused {
  padding-top: 8px;
  border-style: double;
  border-width: 0px 0 0px var(--pauseButtonwidth);
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}
<div>
  <label for="playspersecond">Updates per second: </label>
  <input type="number" id="playspersecond" value="5" min="0" max="10" />
  <button class="button" id="play"></button>
</div>
sycxhyv7

sycxhyv72#

vertical-align: middle;添加到.button类。
像这样

const btn = document.querySelector(".button");
if (btn === null) {
  throw new Error('could not find button');
}
btn.addEventListener("click", function() {
  btn.classList.toggle("paused");
  return false;
});
:root {
  --pauseButtonhight: 16px;
  --pauseButtonwidth: 13px;
  --pauseButtonborder: 8px;
}

.button {
  border: 0;
  vertical-align: middle;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  padding: 0;
  height: var(--pauseButtonhight);
  border-color: transparent transparent transparent #7083d8;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
}

.button.paused {
  padding-top: 8px;
  border-style: double;
  border-width: 0px 0 0px var(--pauseButtonwidth);
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}
<div>
  <label for="playspersecond">Updates per second: </label>
  <input type="number" id="playspersecond" value="5" min="0" max="10" />
  <button class="button" id="play"></button>
</div>

相关问题