技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營(yíng)

贊助商

分類目錄

贊助商

最新文章

搜索

CSS實(shí)現(xiàn)有過(guò)度滑動(dòng)效果的選項(xiàng)卡或標(biāo)簽(slide tabs)

作者:admin    時(shí)間:2021-8-7 11:7:11    瀏覽:

網(wǎng)頁(yè)選項(xiàng)卡或標(biāo)簽(tabs),是在網(wǎng)頁(yè)設(shè)計(jì)中不可缺少的部分。因?yàn)樵谟脩艚换ミ^(guò)程中,點(diǎn)擊標(biāo)簽的機(jī)會(huì)非常大,如果標(biāo)簽設(shè)計(jì)得不好,會(huì)影響用戶的使用體驗(yàn)。所以,設(shè)計(jì)師要在標(biāo)簽設(shè)計(jì)上下一番功夫。本文介紹一個(gè)用純css實(shí)現(xiàn)的有過(guò)度效果的滑動(dòng)選項(xiàng)卡或標(biāo)簽(slide tabs)。

滑動(dòng)選項(xiàng)卡或標(biāo)簽(slide tabs)
滑動(dòng)選項(xiàng)卡或標(biāo)簽(slide tabs)

從上圖看到,當(dāng)點(diǎn)擊標(biāo)簽時(shí),有一個(gè)過(guò)渡滑動(dòng)效果,使用體驗(yàn)是相當(dāng)好的。

這個(gè)滑動(dòng)標(biāo)簽(slide tabs)實(shí)現(xiàn)起來(lái)并不復(fù)雜,用純CSS就能實(shí)現(xiàn),并不需要太多代碼。

CSS

: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);
  }
}

HTML

<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">2</span></label>
<input type="radio" id="radio-2" name="tabs" />
<label class="tab" for="radio-2">Development</label>
<input type="radio" id="radio-3" name="tabs" />
<label class="tab" for="radio-3">Completed</label>
<span class="glider"></span>
</div>
</div>

execcodegetcode

代碼解釋

HTML中 class="container" 的  <div> 是對(duì)整個(gè)標(biāo)簽內(nèi)容進(jìn)行定位。display: flex; 可實(shí)現(xiàn)內(nèi)容垂直居中。

如果想要添加新標(biāo)簽項(xiàng),那么HTML和CSS都要添加部分代碼。

首先HTML添加新標(biāo)簽項(xiàng)代碼,假設(shè)這是第4個(gè)標(biāo)簽。

<input type="radio" id="radio-4" name="tabs" />
<label class="tab" for="radio-4">Demo</label>

然后CSS也添加相應(yīng)的代碼,注意數(shù)字的變化,一個(gè)是radio-4,另一個(gè)是300%。

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

這樣,就會(huì)看到如下圖的結(jié)果——新增了一個(gè)demo標(biāo)簽項(xiàng)。

 

 如果你要修改標(biāo)簽的顏色,過(guò)度效果滑動(dòng)的速度,都可以修改CSS來(lái)實(shí)現(xiàn)。修改起來(lái)不難,只需知道 background-color 是顏色設(shè)置,而 transition 是動(dòng)畫設(shè)置即可。

 您可能對(duì)以下文章也感興趣

x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */