当前位置:首页 > JavaScript

js实现百叶窗

2026-01-14 13:55:17JavaScript

使用CSS和JavaScript实现百叶窗效果

通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。

<style>
  .shutter-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 300px;
    overflow: hidden;
  }

  .shutter-panel {
    flex: 1;
    background: #333;
    transition: all 0.5s ease;
    transform-origin: top;
  }

  .shutter-panel.open {
    transform: scaleY(0);
  }
</style>

<div class="shutter-container" id="shutter">
  <div class="shutter-panel"></div>
  <div class="shutter-panel"></div>
  <div class="shutter-panel"></div>
  <div class="shutter-panel"></div>
  <div class="shutter-panel"></div>
</div>

<script>
  const shutter = document.getElementById('shutter');
  const panels = shutter.querySelectorAll('.shutter-panel');

  function toggleShutter() {
    panels.forEach(panel => {
      panel.classList.toggle('open');
    });
  }

  // 点击切换百叶窗状态
  shutter.addEventListener('click', toggleShutter);
</script>

使用Canvas实现动态百叶窗

Canvas可以实现更复杂的百叶窗动画效果,包括渐变展开和自定义速度。

const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
document.body.appendChild(canvas);

const ctx = canvas.getContext('2d');
const panelCount = 10;
const panelHeight = canvas.height / panelCount;
let openProgress = 0;

function drawShutter() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  for(let i = 0; i < panelCount; i++) {
    const y = i * panelHeight;
    const height = panelHeight * (1 - openProgress);

    ctx.fillStyle = `hsl(${i * 30}, 80%, 50%)`;
    ctx.fillRect(0, y, canvas.width, height);
  }
}

function animate() {
  openProgress = Math.min(openProgress + 0.01, 1);
  drawShutter();

  if(openProgress < 1) {
    requestAnimationFrame(animate);
  }
}

canvas.addEventListener('click', () => {
  openProgress = 0;
  animate();
});

drawShutter();

结合图片的百叶窗特效

实现图片上的百叶窗效果,可以通过CSS遮罩和过渡效果完成。

<style>
  .image-shutter {
    position: relative;
    width: 500px;
    height: 300px;
    overflow: hidden;
  }

  .image-shutter img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .shutter-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .shutter-overlay div {
    flex: 1;
    background: rgba(0,0,0,0.7);
    transition: transform 0.8s cubic-bezier(0.68, -0.55, 0.27, 1.55);
    transform-origin: top;
  }

  .image-shutter:hover .shutter-overlay div {
    transform: scaleY(0);
  }
</style>

<div class="image-shutter">
  <img src="your-image.jpg" alt="Sample">
  <div class="shutter-overlay">
    <div style="transition-delay: 0.1s"></div>
    <div style="transition-delay: 0.2s"></div>
    <div style="transition-delay: 0.3s"></div>
    <div style="transition-delay: 0.4s"></div>
    <div style="transition-delay: 0.5s"></div>
  </div>
</div>

响应式百叶窗组件

创建一个可复用的响应式百叶窗组件,适用于不同屏幕尺寸。

js实现百叶窗

class Shutter {
  constructor(container, options = {}) {
    this.container = typeof container === 'string' 
      ? document.querySelector(container) 
      : container;

    this.panelCount = options.panelCount || 8;
    this.color = options.color || '#333';
    this.duration = options.duration || 500;
    this.isOpen = false;

    this.init();
  }

  init() {
    this.container.style.display = 'flex';
    this.container.style.flexDirection = 'column';
    this.container.style.overflow = 'hidden';

    for(let i = 0; i < this.panelCount; i++) {
      const panel = document.createElement('div');
      panel.style.flex = '1';
      panel.style.backgroundColor = this.color;
      panel.style.transition = `transform ${this.duration}ms ease`;
      panel.style.transformOrigin = 'top';
      this.container.appendChild(panel);
    }

    this.panels = Array.from(this.container.children);
  }

  toggle() {
    this.isOpen = !this.isOpen;

    this.panels.forEach((panel, i) => {
      setTimeout(() => {
        panel.style.transform = this.isOpen ? 'scaleY(0)' : 'scaleY(1)';
      }, i * 50);
    });

    return this;
  }
}

// 使用示例
const shutter = new Shutter('#shutter-container', {
  panelCount: 6,
  color: '#4a90e2',
  duration: 800
});

document.querySelector('#toggle-btn').addEventListener('click', () => {
  shutter.toggle();
});

标签: 百叶窗js
分享给朋友:

相关文章

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js实现换肤

js实现换肤

使用CSS变量实现换肤 通过CSS变量可以轻松实现主题切换功能。CSS变量在根元素中定义,通过JavaScript动态修改这些变量值。 :root { --primary-color: #349…

js实现图表

js实现图表

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

js实现吸色

js实现吸色

使用Canvas实现吸色功能 通过Canvas的getImageData方法获取像素颜色数据。创建一个Canvas元素,将目标图像绘制到Canvas上,通过鼠标事件获取坐标对应的颜色值。 c…

js实现下拉刷新

js实现下拉刷新

监听触摸事件 通过监听 touchstart、touchmove 和 touchend 事件来检测用户下拉手势。记录触摸起始位置和移动距离。 let startY = 0; let curr…