当前位置:首页 > JavaScript

js实现超大的大括号

2026-01-31 08:31:43JavaScript

使用 CSS 和 SVG 实现超大括号

在 JavaScript 中可以通过动态生成 SVG 或 CSS 样式来创建超大括号效果。以下是两种实现方式:

CSS 实现方案 通过伪元素和边框旋转实现大括号效果:

.big-brace {
  position: relative;
  height: 200px;
  width: 50px;
}

.big-brace::before, .big-brace::after {
  content: '';
  position: absolute;
  width: 25px;
  height: 100px;
  border: 5px solid #000;
  border-radius: 50%;
}

.big-brace::before {
  top: 0;
  border-right: none;
  border-bottom: none;
  transform: rotate(-45deg);
}

.big-brace::after {
  bottom: 0;
  border-left: none;
  border-top: none;
  transform: rotate(-45deg);
}

SVG 实现方案 使用 JavaScript 动态创建 SVG 路径:

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

  const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
  const curveHeight = height * 0.8;
  const curveWidth = width * 0.4;

  path.setAttribute('d', 
    `M ${width/2} 0 
     Q ${width/2 - curveWidth} ${curveHeight/4}, ${width/2 - curveWidth} ${curveHeight/2}
     Q ${width/2 - curveWidth} ${3*curveHeight/4}, ${width/2} ${curveHeight}
     M ${width/2} 0
     Q ${width/2 + curveWidth} ${curveHeight/4}, ${width/2 + curveWidth} ${curveHeight/2}
     Q ${width/2 + curveWidth} ${3*curveHeight/4}, ${width/2} ${curveHeight}`
  );

  path.setAttribute('stroke', 'black');
  path.setAttribute('fill', 'none');
  path.setAttribute('stroke-width', '5');

  svg.appendChild(path);
  return svg;
}

// 使用示例
document.body.appendChild(createBigBrace(100, 500));

使用 Canvas 绘制大括号

通过 Canvas API 可以更灵活地控制大括号的形状和大小:

function drawBigBrace(ctx, x, y, width, height) {
  const halfWidth = width / 2;
  const halfHeight = height / 2;

  ctx.beginPath();
  ctx.moveTo(x + halfWidth, y);

  // 上半部分曲线
  ctx.bezierCurveTo(
    x + halfWidth - width*0.3, y + height*0.2,
    x + halfWidth - width*0.3, y + halfHeight,
    x + halfWidth, y + halfHeight
  );

  // 下半部分曲线
  ctx.bezierCurveTo(
    x + halfWidth - width*0.3, y + halfHeight,
    x + halfWidth - width*0.3, y + height*0.8,
    x + halfWidth, y + height
  );

  ctx.moveTo(x + halfWidth, y);

  // 上半部分镜像曲线
  ctx.bezierCurveTo(
    x + halfWidth + width*0.3, y + height*0.2,
    x + halfWidth + width*0.3, y + halfHeight,
    x + halfWidth, y + halfHeight
  );

  // 下半部分镜像曲线
  ctx.bezierCurveTo(
    x + halfWidth + width*0.3, y + halfHeight,
    x + halfWidth + width*0.3, y + height*0.8,
    x + halfWidth, y + height
  );

  ctx.lineWidth = 5;
  ctx.stroke();
}

// 使用示例
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 600;
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
drawBigBrace(ctx, 50, 50, 100, 500);

使用 Unicode 字符

对于简单的需求,可以直接使用 Unicode 中的大括号字符:

js实现超大的大括号

const bigBrace = document.createElement('div');
bigBrace.style.fontSize = '200px';
bigBrace.style.lineHeight = '1';
bigBrace.textContent = '{'; // 或 '}' 
document.body.appendChild(bigBrace);

这种方法简单但可定制性较差,适合快速实现基本效果。

标签: 括号js
分享给朋友:

相关文章

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

js实现图表

js实现图表

在JavaScript中实现图表通常使用流行的图表库,以下是几种常见的方法和工具: 使用Chart.js Chart.js是一个简单灵活的库,适合快速生成响应式图表。安装方式包括CDN引入或npm安…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js实现游标

js实现游标

使用JavaScript实现游标 在JavaScript中,可以通过操作DOM元素的cursor样式属性来实现自定义游标效果。以下是几种常见的实现方法: 修改默认鼠标指针样式 通过CSS的curso…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…

js验证码的实现

js验证码的实现

验证码的基本实现原理 验证码(CAPTCHA)的核心目标是区分人类用户和自动化程序。JavaScript可用于生成或验证客户端验证码,但需注意纯前端验证可能被绕过,通常需结合后端验证。 纯前端验证码…