返回博客列表

现代 CSS 技巧:毛玻璃、渐变与动画

2026年3月24日
2 分钟阅读
技术

毛玻璃效果 (Glassmorphism)

毛玻璃效果是近年来非常流行的设计趋势,它通过背景模糊和半透明来创造层次感。

基础实现

.glass-card {
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
}

暗色模式适配

.dark .glass-card {
  background: rgba(30, 41, 59, 0.7);
  border-color: rgba(71, 85, 105, 0.3);
}

渐变色

线性渐变

.gradient-bg {
  background: linear-gradient(135deg, #6366f1, #8b5cf6, #a855f7);
}

文字渐变

.gradient-text {
  background: linear-gradient(135deg, #6366f1, #a855f7);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

动态渐变背景

@keyframes gradient-shift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.animated-gradient {
  background: linear-gradient(-45deg, #6366f1, #8b5cf6, #a855f7, #6366f1);
  background-size: 400% 400%;
  animation: gradient-shift 6s ease infinite;
}

CSS 动画技巧

悬停浮起效果

.hover-float {
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.hover-float:hover {
  transform: translateY(-4px);
  box-shadow: 0 20px 40px rgba(99, 102, 241, 0.15);
}

滚动渐现动画

结合 Intersection Observer API:

const observer = new IntersectionObserver(
  (entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.classList.add('visible')
      }
    })
  },
  { threshold: 0.1 }
)

document.querySelectorAll('.animate-on-scroll').forEach(el => {
  observer.observe(el)
})
.animate-on-scroll {
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.animate-on-scroll.visible {
  opacity: 1;
  transform: translateY(0);
}

实用的 CSS 小技巧

自定义滚动条

::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-thumb {
  background: #94a3b8;
  border-radius: 3px;
}

平滑的主题切换

body {
  transition: background-color 0.3s ease, color 0.3s ease;
}

总结

现代 CSS 提供了强大的视觉效果能力。合理使用毛玻璃、渐变和动画,可以为用户带来优秀的视觉体验。记住,好的动画应该是微妙的、有目的的,不要过度使用。

评论 留下你的痕迹