我的按钮在我点击它时会移动。如何修复这个问题?

huangapple go评论101阅读模式
英文:

My button moves when I click it. How do I fix that?

问题

暂停/播放按钮在我点击时会移动。如何修复这个问题?

  1. <!-- 开始片段: js 隐藏: false 控制台: true Babel: false -->
  2. <!-- 语言: lang-js -->
  3. const btn = document.querySelector(".button");
  4. if (btn === null) {
  5. throw new Error('无法找到按钮');
  6. }
  7. btn.addEventListener("click", function() {
  8. btn.classList.toggle("paused");
  9. return false;
  10. });
  11. <!-- 语言: lang-css -->
  12. :root {
  13. --pauseButtonhight: 16px;
  14. --pauseButtonwidth: 13px;
  15. --pauseButtonborder: 8px;
  16. }
  17. .button {
  18. border: 0;
  19. background: transparent;
  20. box-sizing: border-box;
  21. width: 0;
  22. padding: 0;
  23. height: var(--pauseButtonhight);
  24. border-color: transparent transparent transparent #7083d8;
  25. transition: 100ms all ease;
  26. cursor: pointer;
  27. border-style: solid;
  28. border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
  29. }
  30. .button.paused {
  31. padding-top: 8px;
  32. border-style: double;
  33. border-width: 0px 0 0px var(--pauseButtonwidth);
  34. }
  35. .button:hover {
  36. border-color: transparent transparent transparent #404040;
  37. }
  38. <!-- 语言: lang-html -->
  39. <div>
  40. <label for="playspersecond">每秒更新次数:</label>
  41. <input type="number" id="playspersecond" value="5" min="0" max="10" />
  42. <button class="button" id="play"></button>
  43. </div>
  44. <!-- 结束片段 -->

当你按下按钮时,按钮会上下移动,并且还会移动旁边的文本。我认为这与我在不同状态下绘制按钮有关,但我对CSS/HTML还不太了解,不知道该怎么办。这是这个暂停按钮的编辑版本。

如果你有更好/正确的暂停/播放按钮实现方法,那也可以。

英文:

The pause/play button moves when I click it. How do I fix that?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const btn = document.querySelector(&quot;.button&quot;);
  2. if (btn === null) {
  3. throw new Error(&#39;could not find button&#39;);
  4. }
  5. btn.addEventListener(&quot;click&quot;, function() {
  6. btn.classList.toggle(&quot;paused&quot;);
  7. return false;
  8. });

<!-- language: lang-css -->

  1. :root {
  2. --pauseButtonhight: 16px;
  3. --pauseButtonwidth: 13px;
  4. --pauseButtonborder: 8px;
  5. }
  6. .button {
  7. border: 0;
  8. background: transparent;
  9. box-sizing: border-box;
  10. width: 0;
  11. padding: 0;
  12. height: var(--pauseButtonhight);
  13. border-color: transparent transparent transparent #7083d8;
  14. transition: 100ms all ease;
  15. cursor: pointer;
  16. border-style: solid;
  17. border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
  18. }
  19. .button.paused {
  20. padding-top: 8px;
  21. border-style: double;
  22. border-width: 0px 0 0px var(--pauseButtonwidth);
  23. }
  24. .button:hover {
  25. border-color: transparent transparent transparent #404040;
  26. }

<!-- language: lang-html -->

  1. &lt;div&gt;
  2. &lt;label for=&quot;playspersecond&quot;&gt;Updates per second: &lt;/label&gt;
  3. &lt;input type=&quot;number&quot; id=&quot;playspersecond&quot; value=&quot;5&quot; min=&quot;0&quot; max=&quot;10&quot; /&gt;
  4. &lt;button class=&quot;button&quot; id=&quot;play&quot;&gt;&lt;/button&gt;
  5. &lt;/div&gt;

<!-- end snippet -->

The button moves up and down when you press it, and moves the text next to it as well.
I think this has something to do with how I am drawing the button in the different states, but I am new to CSS/HTML so I don't know what to do.
This is an edited version of this pause button

If you have a better/correct way of doing a pause/play button, that would work as well.

答案1

得分: 1

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

  1. .vertical-align: middle;
英文:

Set the vertical alignment of the button to middle (or top):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const btn = document.querySelector(&quot;.button&quot;);
  2. if (btn === null) {
  3. throw new Error(&#39;could not find button&#39;);
  4. }
  5. btn.addEventListener(&quot;click&quot;, function() {
  6. btn.classList.toggle(&quot;paused&quot;);
  7. return false;
  8. });

<!-- language: lang-css -->

  1. :root {
  2. --pauseButtonhight: 16px;
  3. --pauseButtonwidth: 13px;
  4. --pauseButtonborder: 8px;
  5. }
  6. .button {
  7. border: 0;
  8. background: transparent;
  9. box-sizing: border-box;
  10. width: 0;
  11. padding: 0;
  12. height: var(--pauseButtonhight);
  13. border-color: transparent transparent transparent #7083d8;
  14. transition: 100ms all ease;
  15. cursor: pointer;
  16. border-style: solid;
  17. border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
  18. vertical-align: middle;
  19. }
  20. .button.paused {
  21. padding-top: 8px;
  22. border-style: double;
  23. border-width: 0px 0 0px var(--pauseButtonwidth);
  24. }
  25. .button:hover {
  26. border-color: transparent transparent transparent #404040;
  27. }

<!-- language: lang-html -->

  1. &lt;div&gt;
  2. &lt;label for=&quot;playspersecond&quot;&gt;Updates per second: &lt;/label&gt;
  3. &lt;input type=&quot;number&quot; id=&quot;playspersecond&quot; value=&quot;5&quot; min=&quot;0&quot; max=&quot;10&quot; /&gt;
  4. &lt;button class=&quot;button&quot; id=&quot;play&quot;&gt;&lt;/button&gt;
  5. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

vertical-align: middle;添加到.button类中。

像这样。

  1. .button {
  2. border: 0;
  3. vertical-align: middle;
  4. background: transparent;
  5. /* 其他样式 */
  6. }
英文:

Add vertical-align: middle; to .button class.

Like this.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

  1. const btn = document.querySelector(&quot;.button&quot;);
  2. if (btn === null) {
  3. throw new Error(&#39;could not find button&#39;);
  4. }
  5. btn.addEventListener(&quot;click&quot;, function() {
  6. btn.classList.toggle(&quot;paused&quot;);
  7. return false;
  8. });

<!-- language: lang-css -->

  1. :root {
  2. --pauseButtonhight: 16px;
  3. --pauseButtonwidth: 13px;
  4. --pauseButtonborder: 8px;
  5. }
  6. .button {
  7. border: 0;
  8. vertical-align: middle;
  9. background: transparent;
  10. box-sizing: border-box;
  11. width: 0;
  12. padding: 0;
  13. height: var(--pauseButtonhight);
  14. border-color: transparent transparent transparent #7083d8;
  15. transition: 100ms all ease;
  16. cursor: pointer;
  17. border-style: solid;
  18. border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
  19. }
  20. .button.paused {
  21. padding-top: 8px;
  22. border-style: double;
  23. border-width: 0px 0 0px var(--pauseButtonwidth);
  24. }
  25. .button:hover {
  26. border-color: transparent transparent transparent #404040;
  27. }

<!-- language: lang-html -->

  1. &lt;div&gt;
  2. &lt;label for=&quot;playspersecond&quot;&gt;Updates per second: &lt;/label&gt;
  3. &lt;input type=&quot;number&quot; id=&quot;playspersecond&quot; value=&quot;5&quot; min=&quot;0&quot; max=&quot;10&quot; /&gt;
  4. &lt;button class=&quot;button&quot; id=&quot;play&quot;&gt;&lt;/button&gt;
  5. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 03:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定