当前位置:首页 > CSS

css制作心跳效果

2026-01-28 13:31:45CSS

使用CSS动画制作心跳效果

通过CSS的@keyframestransform属性可以创建简单的心跳动画效果。核心原理是周期性缩放元素大小,模拟心跳的膨胀收缩。

基础实现代码:

.heartbeat {
  animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
  0% {
    transform: scale(1);
  }
  14% {
    transform: scale(1.3);
  }
  28% {
    transform: scale(1);
  }
  42% {
    transform: scale(1.3);
  }
  70% {
    transform: scale(1);
  }
}

增强视觉效果

添加颜色变化和阴影效果使心跳更逼真:

.heartbeat {
  width: 100px;
  height: 100px;
  background-color: #ff0000;
  box-shadow: 0 0 40px #ff0000;
  animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
  0% {
    transform: scale(1);
    box-shadow: 0 0 10px #ff0000;
  }
  50% {
    transform: scale(1.1);
    background-color: #ff3333;
    box-shadow: 0 0 30px #ff0000;
  }
  100% {
    transform: scale(1);
    box-shadow: 0 0 10px #ff0000;
  }
}

SVG实现心跳效果

使用SVG路径动画可创建更精确的心脏图形动画:

<svg width="100" height="100" viewBox="0 0 24 24">
  <path 
    d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"
    fill="red"
    class="heartbeat"
  />
</svg>
.heartbeat {
  animation: heartbeat 1s infinite;
  transform-origin: center;
}

@keyframes heartbeat {
  0% { transform: scale(1); }
  25% { transform: scale(1.1); }
  50% { transform: scale(1); }
  75% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

性能优化建议

使用will-change属性提升动画性能:

.heartbeat {
  will-change: transform;
  /* 其他动画属性 */
}

考虑使用requestAnimationFrame实现更流畅的动画,但这需要JavaScript配合。纯CSS方案在大多数现代浏览器中已能提供良好的性能表现。

css制作心跳效果

标签: 效果css
分享给朋友:

相关文章

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model…

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 动态数据绑定展示 通过 Vue 的响应式特性,实时展示数据变化。例如,表单输入与预览同步: <template> <div> <…

css导航条制作

css导航条制作

基础导航条制作 使用HTML的无序列表<ul>和CSS的display: flex属性可以快速创建横向导航条。HTML结构通常包含导航链接列表,CSS负责样式和布局。 <nav&…

css制作按钮

css制作按钮

基础按钮样式 创建一个基础的CSS按钮样式,可以通过以下代码实现。这个例子包含了按钮的基本属性,如背景色、边框、内边距和文字样式。 .button { background-color: #4C…

css制作图片

css制作图片

CSS 制作图片的方法 使用 background-image 属性 通过 background-image 属性可以为元素设置背景图片。这种方法适用于将图片作为背景而非内容的一部分。 .elem…

css 制作三角形

css 制作三角形

使用 CSS 制作三角形的方法 边框法(Border Method) 通过设置元素的宽高为0,并利用边框的透明属性来创建三角形。这是最常见且兼容性最好的方法。 向上三角形 .triangl…