当前位置:首页 > CSS

css制作弹球

2026-02-12 21:12:21CSS

使用CSS制作弹球效果

通过CSS动画和关键帧(@keyframes)可以创建简单的弹球效果。以下是实现方法:

创建HTML结构

<div class="ball"></div>

添加基础CSS样式

.ball {
  width: 50px;
  height: 50px;
  background-color: #ff5252;
  border-radius: 50%;
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}

实现弹跳动画

@keyframes bounce {
  0% {
    bottom: 0;
    animation-timing-function: ease-out;
  }
  50% {
    bottom: 200px;
    animation-timing-function: ease-in;
  }
  100% {
    bottom: 0;
    animation-timing-function: ease-out;
  }
}

.ball {
  animation: bounce 1s infinite;
}

添加物理效果

通过调整动画时间函数和关键帧位置模拟真实弹跳:

css制作弹球

@keyframes realisticBounce {
  0% {
    bottom: 0;
    animation-timing-function: cubic-bezier(0.1, 0.25, 0.1, 1);
  }
  15% {
    bottom: 300px;
    animation-timing-function: cubic-bezier(0.05, 0, 0.85, 0.05);
  }
  25% {
    bottom: 200px;
  }
  35% {
    bottom: 280px;
  }
  45% {
    bottom: 230px;
  }
  55% {
    bottom: 270px;
  }
  65% {
    bottom: 240px;
  }
  75% {
    bottom: 260px;
  }
  85% {
    bottom: 245px;
  }
  100% {
    bottom: 250px;
  }
}

添加阴影效果增强真实感

.ball::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 10px;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  bottom: -5px;
  left: 0;
  transform: scaleX(0.8);
  animation: shadow 1s infinite;
}

@keyframes shadow {
  0%, 100% {
    transform: scaleX(0.8);
    opacity: 0.6;
  }
  50% {
    transform: scaleX(1.2);
    opacity: 0.3;
  }
}

完整示例代码

<!DOCTYPE html>
<html>
<head>
  <style>
    .ball {
      width: 50px;
      height: 50px;
      background: radial-gradient(circle at 30% 30%, #ff5252, #d50000);
      border-radius: 50%;
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      animation: bounce 1s infinite;
      box-shadow: inset -5px -5px 10px rgba(0, 0, 0, 0.3);
    }

    .ball::before {
      content: '';
      position: absolute;
      width: 100%;
      height: 10px;
      background: rgba(0, 0, 0, 0.1);
      border-radius: 50%;
      bottom: -5px;
      left: 0;
      transform: scaleX(0.8);
      animation: shadow 1s infinite;
    }

    @keyframes bounce {
      0% {
        bottom: 0;
        animation-timing-function: ease-out;
      }
      50% {
        bottom: 200px;
        animation-timing-function: ease-in;
      }
      100% {
        bottom: 0;
        animation-timing-function: ease-out;
      }
    }

    @keyframes shadow {
      0%, 100% {
        transform: scaleX(0.8);
        opacity: 0.6;
      }
      50% {
        transform: scaleX(1.2);
        opacity: 0.3;
      }
    }
  </style>
</head>
<body>
  <div class="ball"></div>
</body>
</html>

这个实现包含了基本的弹跳动画、物理效果模拟和视觉增强效果。可以通过调整关键帧百分比、高度值和动画时间函数来改变弹跳的节奏和高度。

标签: 弹球css
分享给朋友:

相关文章

用css制作网页

用css制作网页

创建HTML文件结构 新建一个index.html文件,写入基础HTML5结构。通过<link>标签引入外部CSS文件,示例结构如下: <!DOCTYPE html> <…

纯css制作下拉菜单

纯css制作下拉菜单

纯CSS下拉菜单实现方法 通过CSS的:hover伪类和display属性可以实现纯CSS下拉菜单,无需JavaScript。以下是具体实现方式: HTML结构示例: <nav class=…

css与html制作

css与html制作

CSS与HTML制作基础 CSS与HTML是构建网页的核心技术,HTML负责结构,CSS负责样式。以下是关键要点: HTML基础结构 <!DOCTYPE html> <html&…

制作css开关

制作css开关

使用纯CSS创建开关 HTML结构需要包含一个隐藏的复选框和一个关联的标签元素: <label class="switch"> <input type="checkbox"&g…

css风格制作

css风格制作

CSS 风格制作方法 内联样式 直接在 HTML 元素的 style 属性中编写 CSS 代码,适用于单个元素的快速样式设置。 示例: <p style="color: blue; fon…

css 制作表格

css 制作表格

CSS 制作表格的方法 使用 CSS 制作表格可以通过多种方式实现,包括原生 HTML 表格样式、Flexbox 布局或 Grid 布局。以下是几种常见的方法: 原生 HTML 表格样式 HTML…