拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 为什么单击按钮时按钮会上下移动?

为什么单击按钮时按钮会上下移动?

白鹭 - 2022-02-13 2000 0 0

我有两个下拉按钮,当您单击“难度”按钮时,“持续时间”按钮会移到底部;同样,如果您单击“持续时间”按钮,它会将“难度”按钮移至底部。

有趣的是,当先点击难度按钮,然后点击持续时间按钮时,两者都将是正确的高度;但是如果你先点击 Duration 按钮,然后点击 Difficulty 按钮,只有 Duration 按钮会关闭其内容并再次移动到底部。这就是我的代码的样子:

HTML 代码:

<!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="style_MP.css"/>
</head>

<body>
<div class="main_page">
    <h1>Welcome to My Typing Test!</h1>
    <h2>Select Your Difficulty and Duration</h2>
<div>



    <!--Below, the word "diff" is short for "difficulty" -->
    <!--So "diff-settings" means "difficulty settings" -->
    <!--"diff-options" means "difficulty options" and so on -->
    
<div class="difficulty">
  <button onclick="myfunction01()" class="diff-settings">Difficulty</button>
  <div class="diff-options" id="diff-select">
    <a href="beginner">Beginner</a>
    <a href="intermediate">Intermediate</a>
    <a href="advanced">Advanced</a>
    <a href="expert">Expert</a>
  </div>
</div>

<div class="duration">
  <button onclick="myfunction02()" class="duration-settings">Duration</button>
  <div class="duration-options" id="duration-select">
    <a href="30-seconds">30 Seconds</a>
    <a href="60-seconds">60 Seconds</a>
    <a href="120-seconds">120 Seconds</a>
    <a href="custom">Custom</a>
  </div>
</div>



<script src="script_MP.js"></script>
</body>
</html>

CSS 代码:

body{background-color:grey}

.main_page{text-align: center;}

h1{font-size: 50px;}

h2{font-size: 30px; padding-bottom: 40px;}

.difficulty , .duration{
display: inline-block;
margin: 5px;
}


    /* This section is for the Difficulty Button only */

.diff-settings{
padding: 20px;
position: relative;
border: none;
box-shadow: none;
background-color: green;
color:white;
font-size: 20px;
width: 130px;
}


.diff-settings:hover, .diff-settings:focus{
background-color:darkgreen;
color:white;
cursor: pointer;
}


.diff-options{
display: none;
font-size: 20px;
width: 130px;
}

.diff-options a{
background-color: green;
color: white;
display: block;
padding: 8px;
text-decoration: none;
}

.diff-options a:hover {background-color: darkgreen;}

.show {display: block;}

    

    /* This section is for the Duration Button only */

.duration-settings{
padding: 20px;
border: none;
box-shadow: none;
background-color: green;
color:white;
font-size: 20px;
width: 130px;
}
    
    
.duration-settings:hover, .duration-settings:focus{
background-color:darkgreen;
color:white;
cursor: pointer;
}
    
    
.duration-options{
display: none;
font-size: 20px;
width: 130px;
}
    
.duration-options a{
background-color: green;
color: white;
display: block;
padding: 8px;
text-decoration: none;
}
    
.duration-options a:hover {background-color: darkgreen;}
    
.show {display: block;}

为了阻止两个按钮在单击时移到底部,我应该更改什么?

uj5u.com热心网友回复:

只需添加这个;):

.diff-options, .duration-options {
  position: absolute;
}

当您更改块的显示时,会考虑并移动以下元素。通过添加绝对位置,计算下一个元素时不再考虑它。

uj5u.com热心网友回复:

使按钮的父 div 使用 flex 可以解决问题。

.flex{
    display: flex;
    justify-content: center;
}
<div class="flex">
    <!--Below, the word "diff" is short for "difficulty" -->
    <!--So "diff-settings" means "difficulty settings" -->
    <!--"diff-options" means "difficulty options" and so on -->
    
    <div class="difficulty">
    <button onclick="myfunction01()" class="diff-settings">Difficulty</button>
    <div class="diff-options" id="diff-select">
        <a href="beginner">Beginner</a>
        <a href="intermediate">Intermediate</a>
        <a href="advanced">Advanced</a>
        <a href="expert">Expert</a>
    </div>
    </div>

    <div class="duration">
    <button onclick="myfunction02()" class="duration-settings">Duration</button>
    <div class="duration-options" id="duration-select">
        <a href="30-seconds">30 Seconds</a>
        <a href="60-seconds">60 Seconds</a>
        <a href="120-seconds">120 Seconds</a>
        <a href="custom">Custom</a>
    </div>
    </div>
</div>

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *