gpt4 book ai didi

html - 以固定页脚格式显示 Accordion 内容的按钮

转载 作者:太空宇宙 更新时间:2023-11-04 05:51:13 26 4
gpt4 key购买 nike

我想在页脚中创建“按钮”。单击这些按钮将以 Accordion 式样式显示内容。我已经生成了我认为完美的代码,它可以工作,但是,当我单击一个按钮时,所有按钮都会随着内容向上移动。我只希望其中一个向上移动,其余的则锁定在页脚上。此外,可能在窗口内居中(目前它粘在左边)

如有任何帮助,我们将不胜感激。

我添加了一张图片来展示我想要的: enter image description here

现在代码如下:

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>

body {
font-family: "Lato", sans-serif;
margin: 0;
}

.footer {
position:fixed;
bottom:0px;
margin-left: 0 auto;
text-align: center;
width:100%;
}

* {
box-sizing: border-box;
}

/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 20%;
padding-left:10px;
padding-right:10px;

}

/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}

.accordion {
padding: 18px;
margin-left: 10px;
margin-right: 10px;
background-color: #eee;
cursor: pointer;
width: 100%;
text-align: center;
border: none;
outline: none;
font-size: 15px;
transition: 0.4s;
}

.active, .accordion:hover {
background-color: #ccc;
}

.panel {
text-align: left;
margin-left: 10px;
margin-right: 10px;
width: 100%;
background-color: #eee;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}

.panel p {
padding-left:10px;
padding-right:10px;
}

</style>
</head>
<body>

<h2>My footer buttons</h2>
<div class="footer">
<div class="row">
<div class="column">

<div class="div1">
<button class="accordion">Button 1</button>
<div class="panel">
<p>Button 1 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
<div class="column">


<div class="div1">
<button class="accordion">Button 2</button>
<div class="panel">
<p>Button 2 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
<div class="column">

<div class="div1">
<button class="accordion">Button 3</button>
<div class="panel">
<p>Button 3 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
<div class="column">

<div class="div1">
<button class="accordion">Button 4</button>
<div class="panel">
<p>Button 4 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
</div>
</div>

<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
</script>

</body>
</html>

最佳答案

在 .row 上使用 flexbox 并对齐结束

.row {
display: flex;
align-items: flex-end;
justify-content:center;
}

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
body {
font-family: "Lato", sans-serif;
margin: 0;
}

.footer {
position: fixed;
bottom: 0px;
margin-left: 0 auto;
text-align: center;
width: 100%;
}

* {
box-sizing: border-box;
}


/* Create two equal columns that floats next to each other */

.column {
float: left;
width: 20%;
padding-left: 10px;
padding-right: 10px;
}

.row {
display: flex;
align-items: flex-end;
justify-content:center;
}


/* Clear floats after the columns */

.row:after {
content: "";
display: table;
clear: both;
}

.accordion {
padding: 18px;
margin-left: 10px;
margin-right: 10px;
background-color: #eee;
cursor: pointer;
width: 100%;
text-align: center;
border: none;
outline: none;
font-size: 15px;
transition: 0.4s;
}

.active,
.accordion:hover {
background-color: #ccc;
}

.panel {
text-align: left;
margin-left: 10px;
margin-right: 10px;
width: 100%;
background-color: #eee;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}

.panel p {
padding-left: 10px;
padding-right: 10px;
}
<h2>My footer buttons</h2>
<div class="footer">
<div class="row">
<div class="column">

<div class="div1">
<button class="accordion">Button 1</button>
<div class="panel">
<p>Button 1 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
<div class="column">


<div class="div1">
<button class="accordion">Button 2</button>
<div class="panel">
<p>Button 2 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
<div class="column">

<div class="div1">
<button class="accordion">Button 3</button>
<div class="panel">
<p>Button 3 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
<div class="column">

<div class="div1">
<button class="accordion">Button 4</button>
<div class="panel">
<p>Button 4 content and information to go here <br><br><a href="">more information</a></p>
</div>
</div>

</div>
</div>
</div>

关于html - 以固定页脚格式显示 Accordion 内容的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58241113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com