js+css实现弹性伸缩tab选项卡切换动画效果代码

代码语言:html

所属分类:选项卡

代码描述:js+css实现弹性伸缩tab选项卡切换动画效果代码

代码标签: js css 弹性 伸缩 tab 选项卡 切换 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  
  
  
<style>
* {
  box-sizing: border-box;
}

body {
  padding: 0;
  margin: 0;
  background: hsl(270.2, 50%, 8%);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.tabs {
  --tab-width: 100px;
  --tab-height: 30px;
  --tab-border-radius: 5px;
  background: hsl(270.2, 100%, 3.5%);
  border-radius: 5px;
  padding: 8px 8px;
  display: flex;
  gap: 0 20px;
  position: relative;
  overflow: hidden;
  outline: 1px solid hsl(270.2, 50%, 10%);
}

.tab,
.active-tab {
  --tab-text-color: hsl(247.2, 10%, 40%);
  color: var(--tab-text-color);
  font-size: 14px;
  font-weight: 700;
  -webkit-user-select: none;
  user-select: none;
  line-height: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
    Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue',
    sans-serif;
  width: var(--tab-width);
  height: var(--tab-height);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  position: relative;
  z-index: 1;
  cursor: pointer;
}

.active-tab {
  position: absolute;
  z-index: 3;
  pointer-events: none;
}

.switcher {
  opacity: 0.5;
  position: absolute;
  z-index: 2;
  width: var(--tab-width);
  height: var(--tab-height);
  background: hsl(270.2, 100%, 80%);
  border-radius: var(--tab-border-radius);
  touch-action: none;
  cursor: pointer;
}
</style>


  
  
</head>

<body >
  <div class="tabs">
  <div class="switcher"></div>
  <div class="tab" data-active-color="#ffd609">Pending</div>
  <div class="tab" data-active-color="#46dc00">Approved</div>
  <div class="tab" data-active-color="#ff6d6d">Rejected</div>
</div>
  
      <script  >
class Vec2 {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  add(v) {
    this.x += v.x;
    this.y += v.y;
    return this;
  }

  scale(n) {
    this.x *= n;
    this.y *= n;
    return this;
  }

  get magnitudeSquared() {
    return this.x * this.x + this.y * this.y;
  }

  get magnitude() {
    return Math.sqrt(this.x * this.x + this.y * this.y);
  }

  get unitVector() {
    const result = new Vec2(0, 0);
    const length = this.magnitude;
    if (length !== 0) {
      result.x = this.x / length;
      result.y = this.y / length;
    }
    return result;
  }

  static scale(v, n) {
    return new Vec2(v.x, v.y).scale(n);
  }

  static sub(v1, v2) {
    return new Vec2(v1.x - v2.x, v1.y - v2.y);
  }}


class BoxShape {
  constructor(width, height, inset) {
    this.width = width;
    this.height = height;
    this.inset = inset || { top: 0, right: 0, bottom: 0, left: 0 };
  }}


class CircleShape {
  constructor(radius) {
    this.radius = radius;
  }}


class Force {
  static generateSpringForce(body, anchor, restLength, k, b) {
    const d = Vec2.sub(body.position, anchor);
    const displacement = d.magnitude - restLength;

    const springDirection = d.unitVector;
    const springMagnitude = -k * displacement;

    const dampingMagnitude = body.velocity.magnitude * -b;

    const magnitude = dampingMagnitude + springMagnitude;

    return Vec2.scale(springDirection, magnitude);
  }}


class Body {
  constructor(shape, x, y, mass) {
    this.shape = shape;
    this.mass = mass;
    this.invMass = mass !== 0 ? 1 / mass : 0;
    this.initialPosition = new Vec2(x, y);
    this.position = new Vec2(x, y);
    this.velocity = new Vec2(0, 0);
    this.acceleration = new Vec2(0, 0);
    this.sumForces = new Vec2(0, 0);
  }

  get relPosition() {
    return Vec2.sub(this.position, this.initialPosition);
  }

  update(dt) {
    this.integrateLinear(dt);
  }

  addForce(force) {
    this.sumForces.add(force);
  }

  clearForces() {
    this.sumForces = new Vec2(0, 0);
  }

  integrateLinear(dt) {
    this.acceleration = Vec2.scale(this.sumForces, this.invMass);
    this.velocity.add(Vec2.scale(this.acceleration, dt));
    this.position.add(Vec2.scale(this.velocity, dt));

    this.clearForces();
  }}


const clamp = (num, min, max) => Math.min(Math.max(num, min), max);

let previousTime = 0;

const SWITCHER_EDGE_OFFSET = 5;

let isDragging = false;

const tabsContainerEl = document.querySelector('.tabs');
const tabsRect = tabsContainerEl.getBoundingClientRect();

const switcherEl = document.querySelector('.switcher');
const switcherRect = switcherEl.getBoundingClientRect();

const anchorEl = document.createElement('div');
anchorEl.style.position = 'absolute'.........完整代码请登录后点击上方下载按钮下载查看

网友评论0