当前位置:首页 > JavaScript

js实现徽章

2026-03-14 23:27:30JavaScript

实现徽章的基本方法

使用HTML和CSS创建徽章的基础结构,通过JavaScript动态添加或修改徽章内容。以下是一个简单示例:

<div class="badge-container">
  <span class="badge">1</span>
</div>
.badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: red;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
}
// 更新徽章内容
function updateBadge(count) {
  const badge = document.querySelector('.badge');
  badge.textContent = count;
  badge.style.display = count > 0 ? 'flex' : 'none';
}

动态创建徽章

通过JavaScript动态创建徽章元素并附加到目标元素上:

js实现徽章

function createBadge(parentElement, count) {
  const badge = document.createElement('span');
  badge.className = 'badge';
  badge.textContent = count;

  // 设置父元素为相对定位
  parentElement.style.position = 'relative';

  // 添加徽章到父元素
  parentElement.appendChild(badge);

  return badge;
}

使用SVG创建更复杂的徽章

对于需要更复杂样式的徽章,可以使用SVG:

js实现徽章

function createSVGBadge(parentElement, text) {
  const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  svg.setAttribute('width', '24');
  svg.setAttribute('height', '24');

  const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
  circle.setAttribute('cx', '12');
  circle.setAttribute('cy', '12');
  circle.setAttribute('r', '10');
  circle.setAttribute('fill', 'red');

  const textElement = document.createElementNS("http://www.w3.org/2000/svg", "text");
  textElement.setAttribute('x', '12');
  textElement.setAttribute('y', '16');
  textElement.setAttribute('text-anchor', 'middle');
  textElement.setAttribute('fill', 'white');
  textElement.textContent = text;

  svg.appendChild(circle);
  svg.appendChild(textElement);

  parentElement.appendChild(svg);
  return svg;
}

响应式徽章组件

创建一个可重用的徽章组件类:

class Badge {
  constructor(options) {
    this.element = document.createElement('span');
    this.element.className = 'badge';

    this.update(options);
  }

  update({ count, color, position }) {
    this.element.textContent = count;
    this.element.style.backgroundColor = color || 'red';

    if (position) {
      this.element.style.top = position.top || '0';
      this.element.style.right = position.right || '0';
    }

    this.element.style.display = count > 0 ? 'block' : 'none';
  }

  attachTo(parentElement) {
    parentElement.style.position = 'relative';
    parentElement.appendChild(this.element);
  }
}

使用示例

// 创建并附加徽章
const notificationIcon = document.getElementById('notification-icon');
const badge = new Badge({ count: 5, color: 'blue' });
badge.attachTo(notificationIcon);

// 更新徽章
badge.update({ count: 3 });

动画效果

为徽章添加简单的动画效果:

.badge-pulse {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}
// 添加脉冲动画
function addPulseAnimation(badgeElement) {
  badgeElement.classList.add('badge-pulse');
}

标签: 徽章js
分享给朋友:

相关文章

js实现动画

js实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-containe…

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(f…