2 回答

TA貢獻1884條經(jīng)驗 獲得超4個贊
也許這可以幫助你。我添加了一個<span>due 來控制兩個部分然后display: flex;
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";
}
});
}
.accordion {
background-color: red;
color: #444;
cursor: pointer;
width: 100%;
text-align: left;
outline: none;
transition: 0.4s;
color: #262626;
font-size: 18px;
font-weight: 700;
font-style: normal;
font-family: 'Lato';
border: 0;
margin: 0;
display: flex;
justify-content: space-between;
align-items: center;
padding: 18px;
}
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.active, .accordion:hover {
background-color: white;
}
/* Style the accordion panel. Note: hidden by default */
.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
.accordion span:after {
content: '\02795'; /* Unicode character for "plus" sign (+) */
font-size: 18px;
font-family: 'Lato';
color: #262626;
padding-left: 20px;
}
.accordion.active span:after {
content: "\2796"; /* Unicode character for "minus" sign (-) */
}
<button class="accordion">Where is your company located? <span></span></button>
<div class="panel">
<p class="zpa-regular2">We are located in the heart of San Francisco, California USA! We do have shipping warehouses located in the USA, Europe, and Asia to ensure the quickest delivery for your location.</p>
</div>
<button class="accordion">What is the warranty and return policy?<span></span></button>
<div class="panel">
<p class="zpa-regular2"><span>We have a Risk-Free Policy. During this promotion - you can try the product for 30 days - if you decide for whatever reason this is not for you then you can return the device for a full refund.</span></p>
</div>
<button class="accordion">Does the product have a specific method of operation? Is it easy to use?<span></span></button>
<div class="panel">
<p class="zpa-regular2">Yes! It is very simple and easy to use. You will receive a detailed user manual with positions and pointers to maximize your results. :)</p>
</div>

TA貢獻1877條經(jīng)驗 獲得超6個贊
這個答案是基于相關 css 屬性的實際行為來實現(xiàn)減號和加號在按鈕內(nèi)的垂直對齊(我沒有嘗試過其他元素,如 span 或 div,但我相信它的工作原理是一樣的,如果不是請原諒我的猜測) 增加字體大?。ㄔ谌魏纬潭壬希?,而不管使用的字體系列。
將此視為 Alberto Rhuetras 已經(jīng)回答的替代方案。
用例:有時你想要更大的按鈕,里面有加號或減號。但是字體大小對于按鈕來說太小了。當您增加按鈕的字體大小時,加號和減號無法像我一樣垂直對齊。那是我想出以下解決方案的時候。
注意:我在其他任何地方都找不到解決方案,所以我最終找到了這個解決方案。我愿意接受任何關于解決方案的說法,所以請隨時發(fā)表一些評論:)
/* common style */
.minus, .plus {
height: 50px;
width: 200px;
background: #216AFF;
color: white;
}
.minus {
font-size: 70px;
display: flex;
justify-content: center;
line-height: 35px;
}
.plus {
font-size: 50px;
display: flex;
justify-content: center;
line-height: 45px;
}
.demo {
font-size: 18px;
padding: 18px;
width: 100%;
text-align: left;
display: flex;
}
.demo::after {
content: "\02795";
font-size: 18px;
float: right;
line-height: 23px;
justify-content: center;
align-content: flex-start;
}
<button class="minus">-</button>
<br>
<button class="plus">+</button>
<br>
<button class="demo">Does the product have a specific method of operation? Is it easy to use? What is the warranty and return policy?</button>
在按鈕上使用 display-flex 并使用 line-height 的值在按鈕內(nèi)垂直放置加號或減號。行高值的增加會使符號向下移動,而行高值的減小會使符號向上移動。謝謝!
添加回答
舉報