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

标签 html css button footer

我想在页脚中创建“按钮”。单击这些按钮将以 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/

相关文章:

javascript - 单击元素时导航栏下拉列表的标题不会更改

html - Bootstrap 功能列表布局

javascript - 通过 html 标签实现 onclick 按钮的 js 功能

html - 与显示 block 一起使用时元素的位置错误

html - 如何在多个 div 中重用 angularjs 条形图指令

javascript - 如何使用angular js在母版页中设置事件菜单

asp.net - 简单的 ajax 调用不适用于按钮单击

java - 在 Swing 中显示多个按钮

jQuery CSS 主题滚动器

javascript - 表格宽度不变