17 滑动标签

效果演示

实现了一个多标签页切换的效果,其中包括一个选项卡栏、一个滑块、三个选项卡页。用户可以通过点击选项卡栏上的选项卡来切换不同的页面,滑块则用于切换不同的选项卡页。当用户点击选项卡栏上的选项卡时,滑块会平滑地移动到对应的选项卡页,同时选项卡栏上的选项卡的颜色会发生变化,以提示用户当前处于哪个页面。此外,当用户在移动设备上访问网站时,选项卡栏会自动缩小,以适应屏幕大小。

Code

<div class="container">
    <div class="tabs">
        <input type="radio" id="radio-1" name="tabs" checked />
        <label class="tab" for="radio-1">Upcoming
            <span class="notification">2span>label>
        <input type="radio" id="radio-2" name="tabs" />
        <label class="tab" for="radio-2">Developmentlabel>
        <input type="radio" id="radio-3" name="tabs" />
        <label class="tab" for="radio-3">Completedlabel>
        <span class="glider">span>
    div>
div>
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap");

:root {
  --primary-color: #185ee0;
  --secondary-color: #e6eef9;
}

*,
*:after,
*:before {
  box-sizing: border-box;
}

body {
  font-family: "Inter", sans-serif;
  background-color: rgba(230, 238, 249, 0.5);
}

.container {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tabs {
  display: flex;
  position: relative;
  background-color: #fff;
  box-shadow: 0 0 1px 0 rgba(24, 94, 224, 0.15), 0 6px 12px 0 rgba(24, 94, 224, 0.15);
  padding: 0.75rem;
  border-radius: 99px;
}

.tabs * {
  z-index: 2;
}

input[type=radio] {
  display: none;
}

.tab {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 54px;
  width: 200px;
  font-size: 1.25rem;
  font-weight: 500;
  border-radius: 99px;
  cursor: pointer;
  transition: color 0.15s ease-in;
}

.notification {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  margin-left: 0.75rem;
  border-radius: 50%;
  background-color: var(--secondary-color);
  transition: 0.15s ease-in;
}

input[type=radio]:checked+label {
  color: var(--primary-color);
}

input[type=radio]:checked+label>.notification {
  background-color: var(--primary-color);
  color: #fff;
}

input[id=radio-1]:checked~.glider {
  transform: translateX(0);
}

input[id=radio-2]:checked~.glider {
  transform: translateX(100%);
}

input[id=radio-3]:checked~.glider {
  transform: translateX(200%);
}

.glider {
  position: absolute;
  display: flex;
  height: 54px;
  width: 200px;
  background-color: var(--secondary-color);
  z-index: 1;
  border-radius: 99px;
  transition: 0.25s ease-out;
}

@media (max-width: 700px) {
  .tabs {
    transform: scale(0.6);
  }
}

实现思路拆分

@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap");

这行代码是导入Google Fonts字体库,用于设置网站的字体样式。

:root {
  --primary-color: #185ee0;
  --secondary-color: #e6eef9;
}

这段代码定义了两个CSS变量,分别是–primary-color和–secondary-color,用于设置网站的主要颜色和次要颜色。

*,
*:after,
*:before {
  box-sizing: border-box;
}

这段代码设置了所有元素的盒模型为border-box,以确保元素的宽度和高度包括了元素的边框、内边距和边框半径。

body {
  font-family: "Inter", sans-serif;
  background-color: rgba(230, 238, 249, 0.5);
}

这段代码设置了网站的字体样式和背景颜色。

.container {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

这段代码设置了一个容器,用于居中显示网站的内容。

.tabs {
  display: flex;
  position: relative;
  background-color: #fff;
  box-shadow: 0 0 1px 0 rgba(24, 94, 224, 0.15), 0 6px 12px 0 rgba(24, 94, 224, 0.15);
  padding: 0.75rem;
  border-radius: 99px;
}

这段代码设置了选项卡栏的样式,包括背景颜色、阴影、内边距和圆角半径。

.tabs * {
  z-index: 2;
}

这段代码设置了选项卡栏和选项卡页的z-index值为2,以确保它们在其他元素之上。

input[type=radio] {
  display: none;
}

这段代码隐藏了所有的单选按钮。

.tab {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 54px;
  width: 200px;
  font-size: 1.25rem;
  font-weight: 500;
  border-radius: 99px;
  cursor: pointer;
  transition: color 0.15s ease-in;
}

这段代码设置了选项卡的样式,包括高度、宽度、字体大小、字体粗细、圆角半径、鼠标指针和过渡效果。

.notification {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  margin-left: 0.75rem;
  border-radius: 50%;
  background-color: var(--secondary-color);
  transition: 0.15s ease-in;
}

这段代码设置了选项卡上的通知圆圈的样式,包括宽度、高度、左侧外边距、圆角半径、背景颜色和过渡效果。

input[type=radio]:checked+label {
  color: var(--primary-color);
}

这段代码设置了选中的选项卡的字体颜色为主要颜色。

input[type=radio]:checked+label>.notification {
  background-color: var(--primary-color);
  color: #fff;
}

这段代码设置了选中的选项卡上的通知圆圈的背景颜色和字体颜色。

input[id=radio-1]:checked~.glider {
  transform: translateX(0);
}

这段代码设置了第一个选项卡页的样式,包括平滑移动到对应的选项卡页。

input[id=radio-2]:checked~.glider {
  transform: translateX(100%);
}

这段代码设置了第二个选项卡页的样式,包括平滑移动到对应的选项卡页。

input[id=radio-3]:checked~.glider {
  transform: translateX(200%);
}

这段代码设置了第三个选项卡页的样式,包括平滑移动到对应的选项卡页。

.glider {
  position: absolute;
  display: flex;
  height: 54px;
  width: 200px;
  background-color: var(--secondary-color);
  z-index: 1;
  border-radius: 99px;
  transition: 0.25s ease-out;
}

这段代码设置了选项卡页的样式,包括位置、大小、背景颜色、z-index值、圆角半径和过渡效果。

@media (max-width: 700px) {
  .tabs {
    transform: scale(0.6);
  }
}

这段代码设置了在屏幕宽度小于700px时,选项卡栏的缩小效果。

你可能感兴趣的:(若冰说CSS,css,前端,css)