CSS多按鈕左右滾動功能是一種常見的網頁設計技巧,它可以讓網頁中有多個按鈕時,通過左右滑動來展示不同的按鈕。下面我們來介紹一下CSS實現多按鈕左右滾動的方法。
//HTML代碼 <div class="button_wrapper"> <button class="button active">按鈕1</button> <button class="button">按鈕2</button> <button class="button">按鈕3</button> <button class="button">按鈕4</button> <button class="button">按鈕5</button> <button class="button">按鈕6</button> <button class="button">按鈕7</button> <button class="button">按鈕8</button> <button class="button">按鈕9</button> </div> //CSS代碼 .button_wrapper { overflow-x: auto; white-space: nowrap; } .button { display: inline-block; padding: 10px 15px; font-size: 14px; border: none; background-color: #eee; margin-right: 10px; cursor: pointer; } .active { background-color: #ccc; }
首先,我們需要將所有按鈕放在一個容器內,并設置容器的寬度不變,高度可以根據實際需求來設定,設置滾動條屬性overflow-x為auto,這樣如果容器內的元素超出容器寬度,就會自動出現水平滾動條;接著設置按鈕為內聯元素,使按鈕默認在同一行內排列,white-space: nowrap;則是限制內容不能換行。為了讓按鈕之間有間距,我們需要設置按鈕的margin-right屬性;最后設置一個.active的樣式,用于表明當前選中的按鈕,可以通過在JavaScript中動態修改控制。
綜上所述,通過以上CSS代碼,我們就可以實現多按鈕的左右滾動功能了。