首页 > 解决方案 > 纯 CSS 水平滚动

问题描述

我正在尝试为我正在制作的小型 Web 应用程序创建选项卡的外观和感觉,您可以在它们之间切换。您可以创建将显示新内容的新选项卡。选项卡的数量将继续水平增长,数量越多,直至浏览器宽度。选项卡填满全宽后,我希望能够水平滚动以查看选项卡的溢出。我的问题是我尝试过的代码,它使选项卡的宽度在溢出时变小并且不允许滚动。我怎样才能达到预期的效果?

这是我的代码:

let $tabs = document.querySelectorAll('.tab');
let $lastTab = $tabs[$tabs.length - 1];



$tabs.forEach((tab) => {
  
  tab.addEventListener('click', () => {
    const $currentlyActive = document.querySelector('.active');
    
    $currentlyActive.classList.remove('active');

    tab.classList.add('active');

    let newTab = document.createElement('div');
    newTab.className = 'tab';
    newTab.innerText = 'Day ??';
    $lastTab.insertAdjacentElement('beforebegin', newTab);
    $tabs = document.querySelectorAll('.tab');
  });
});
.breakout-holder {
  width: 100%;
  border: 1px solid black;
  height: 150px;
  box-sizing: border-box;
}

.heading {
  /* background-color: grey; */
  height: 60px;
  display: flex;
  align-items: flex-end;
  position: relative;
  overflow-y: hidden;
  overflow-x: scroll;
}

.heading::-webkit-scrollbar {
  display: none;
}

.tab.active {
  background: linear-gradient(to left, lightgreen 50%, lightgrey 50%) right;
  background-size: 200%;
  transition: 0.5s ease-out;
  z-index: 5000;
}

.tab {
  position: relative;
  font-weight: bold;
  background-color: lightgrey;
  color: black;
  height: 90%;
  width: 10ch;
  padding: 0 20px;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  /* border-top: 1px solid black;
  border-right: 1px solid black;
  border-left: 1px solid black; */
  border-radius: 15px 15px 0 0;
  cursor: pointer;
  /* tab.active */

  background-color: lightgrey !important;
  z-index: 5;
  position: relative;
}

.tab:not(:first-child) {
  margin-left: -5px;
}

.tab::before,
.tab::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 2px;
  box-shadow: -3px 0px 4px 1px rgba(0, 0, 0, 0.3);
  transform: skew(-5deg, -10deg);
  width: 1px;
  height: 60%;
  border-radius: 0;
  display: none;
}

.tab::after {
  left: auto;
  right: 0;
  box-shadow: 3px 1px 4px 1px rgba(0, 0, 0, 0.3);
  transform: skew(5deg, 10deg);
}

.tab::before,
.tab::after {
  z-index: -1;
  display: block;
}

.tab.active:first-child::before {
  display: none;
}

.tab.active:last-child::after {
  display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="breakoutStyle.css">
    <title>Breakout room form</title>
</head>
<body>
    <div class="breakout-holder">
        <div class="heading">
            <div class="tab active">
                Day 1
            </div>
            <div class="tab">
                Day 2
            </div>
            <div class="tab">
                Day 3
            </div>
            <div class="tab">
                Day 4
            </div>
            <div class="tab">
                Day 5
            </div>
            <div class="tab">
                Day 6
            </div>
            <div class="tab">
                Day 7
            </div>
            <div class="tab">
                Day 8
            </div>
            <div class="tab">
                Day 9
            </div>
            <div class="tab">
                Day 10
            </div>
            <div class="tab" >
                +
            </div>
        </div>
        <div class="body"></div>
    </div>
</body>
<script src="app.js"></script>
</html>

标签: javascriptcss

解决方案


推荐阅读