当前位置:首页 > JavaScript

js实现slideup

2026-02-01 21:52:47JavaScript

js实现slideup

实现 SlideUp 动画效果

使用 JavaScript 实现元素的 slideUp 动画效果可以通过操作元素的 heightoverflow 属性来实现。以下是几种常见的方法:

使用原生 JavaScript

function slideUp(element, duration = 300) {
  element.style.height = element.offsetHeight + 'px';
  element.style.transition = `height ${duration}ms ease`;
  element.offsetHeight; // 触发重绘
  element.style.height = '0';
  element.style.overflow = 'hidden';

  setTimeout(() => {
    element.style.display = 'none';
  }, duration);
}

使用 CSS 动画

function slideUp(element, duration = 300) {
  element.style.animation = `slide-up ${duration}ms forwards`;

  // 定义 CSS 动画
  const style = document.createElement('style');
  style.textContent = `
    @keyframes slide-up {
      from { height: ${element.offsetHeight}px; }
      to { height: 0; }
    }
  `;
  document.head.appendChild(style);

  setTimeout(() => {
    element.style.display = 'none';
    document.head.removeChild(style);
  }, duration);
}

使用 jQuery 风格实现

function slideUp(element, duration = 300) {
  const el = element;
  el.style.transitionProperty = 'height, margin, padding';
  el.style.transitionDuration = duration + 'ms';
  el.style.boxSizing = 'border-box';
  el.style.height = el.offsetHeight + 'px';
  el.offsetHeight;

  el.style.overflow = 'hidden';
  el.style.height = '0';
  el.style.paddingTop = '0';
  el.style.paddingBottom = '0';
  el.style.marginTop = '0';
  el.style.marginBottom = '0';

  window.setTimeout(() => {
    el.style.display = 'none';
    el.style.removeProperty('height');
    el.style.removeProperty('padding-top');
    el.style.removeProperty('padding-bottom');
    el.style.removeProperty('margin-top');
    el.style.removeProperty('margin-bottom');
    el.style.removeProperty('overflow');
    el.style.removeProperty('transition-duration');
    el.style.removeProperty('transition-property');
  }, duration);
}

使用 requestAnimationFrame 实现平滑动画

function slideUp(element, duration = 300) {
  const startHeight = element.offsetHeight;
  const startTime = performance.now();

  function animate(currentTime) {
    const elapsed = currentTime - startTime;
    const progress = Math.min(elapsed / duration, 1);
    const height = startHeight * (1 - progress);

    element.style.height = height + 'px';
    element.style.overflow = 'hidden';

    if (progress < 1) {
      requestAnimationFrame(animate);
    } else {
      element.style.display = 'none';
      element.style.height = '';
      element.style.overflow = '';
    }
  }

  requestAnimationFrame(animate);
}

注意事项

  • 确保目标元素最初是可见的(display 不为 none
  • 动画过程中可能需要处理 paddingmargin 以确保视觉效果一致
  • 对于现代浏览器,CSS transitions 通常是性能最佳的选择
  • 如果需要反向操作(slideDown),需要保存原始高度或计算内容高度

这些方法可以根据具体需求进行调整,例如添加回调函数、支持 Promise 等。

js实现slideup

标签: jsslideup
分享给朋友:

相关文章

vue实现js休眠

vue实现js休眠

Vue 中实现 JavaScript 休眠 在 Vue 中实现 JavaScript 休眠通常需要使用异步方式,以避免阻塞主线程。以下是几种常见方法: 使用 setTimeout 和 Promis…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js防抖和节流实现

js防抖和节流实现

防抖(Debounce)的实现 防抖的核心思想是在事件被触发后,延迟执行回调函数。如果在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口大小调整等场景。 function debounce…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…