当前位置:首页 > JavaScript

js 实现圆角

2026-04-06 00:21:18JavaScript

使用 CSS 实现圆角

在 JavaScript 中可以通过操作 DOM 元素的 style 属性来动态设置圆角。以下是几种常见的实现方式:

// 获取目标元素
const element = document.getElementById('target-element');

// 设置统一的圆角半径(例如 10px)
element.style.borderRadius = '10px';

// 设置不同方向的圆角半径(左上、右上、右下、左下)
element.style.borderRadius = '10px 5px 15px 20px';

// 使用百分比实现圆形(宽高需相等)
element.style.borderRadius = '50%';

使用 Canvas 绘制圆角矩形

通过 Canvas API 可以绘制带有圆角的矩形图形:

js 实现圆角

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

function drawRoundedRect(x, y, width, height, radius) {
  ctx.beginPath();
  ctx.moveTo(x + radius, y);
  ctx.lineTo(x + width - radius, y);
  ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
  ctx.lineTo(x + width, y + height - radius);
  ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  ctx.lineTo(x + radius, y + height);
  ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
  ctx.lineTo(x, y + radius);
  ctx.quadraticCurveTo(x, y, x + radius, y);
  ctx.closePath();
  ctx.fillStyle = '#8ED6FF';
  ctx.fill();
}

drawRoundedRect(50, 50, 200, 100, 20);

SVG 实现圆角

通过 JavaScript 动态创建 SVG 元素并设置圆角属性:

js 实现圆角

const svgNS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(svgNS, "svg");
svg.setAttribute('width', '300');
svg.setAttribute('height', '200');

const rect = document.createElementNS(svgNS, "rect");
rect.setAttribute('x', '50');
rect.setAttribute('y', '50');
rect.setAttribute('width', '200');
rect.setAttribute('height', '100');
rect.setAttribute('rx', '20'); // 水平圆角半径
rect.setAttribute('ry', '10'); // 垂直圆角半径
rect.setAttribute('fill', '#FF9E7D');

svg.appendChild(rect);
document.body.appendChild(svg);

动态计算圆角值

根据元素尺寸自动计算合适的圆角值:

function adaptiveBorderRadius(element) {
  const width = element.offsetWidth;
  const height = element.offsetHeight;
  const minDimension = Math.min(width, height);
  element.style.borderRadius = `${minDimension * 0.1}px`;
}

window.addEventListener('resize', () => {
  const box = document.getElementById('adaptive-box');
  adaptiveBorderRadius(box);
});

圆角动画效果

使用 JavaScript 实现圆角变化的动画:

function animateBorderRadius(element, start, end, duration) {
  const startTime = performance.now();

  function update(currentTime) {
    const elapsed = currentTime - startTime;
    const progress = Math.min(elapsed / duration, 1);
    const radius = start + (end - start) * progress;
    element.style.borderRadius = `${radius}px`;

    if (progress < 1) {
      requestAnimationFrame(update);
    }
  }

  requestAnimationFrame(update);
}

const animatedElement = document.getElementById('animated-element');
animateBorderRadius(animatedElement, 0, 50, 1000);

标签: 圆角js
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 jQ…

js实现跑马灯

js实现跑马灯

实现跑马灯效果 使用HTML和JavaScript可以轻松实现跑马灯效果。以下是两种常见的实现方式: HTML结构 <div id="marquee"> <span>…

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculato…

js 实现递归

js 实现递归

递归的基本概念 递归是指函数直接或间接调用自身的过程。在JavaScript中,递归通常用于解决可以分解为相似子问题的问题,如阶乘、斐波那契数列、树形结构遍历等。 递归的实现要点 基线条件(Base…

js 多选 实现

js 多选 实现

实现多选的 JavaScript 方法 使用复选框(Checkbox)实现多选 通过 HTML 的 checkbox 元素配合 JavaScript 实现多选功能。用户可以选择多个选项,提交时获取所有…

vue js实现弹窗

vue js实现弹窗

Vue.js 实现弹窗的方法 使用组件实现弹窗 创建一个独立的弹窗组件,例如 Modal.vue,包含弹窗的 HTML 结构和样式。在组件中定义 props 控制弹窗的显示与隐藏。 <temp…