当前位置:首页 > JavaScript

js实现上下分屏

2026-02-03 01:22:19JavaScript

实现上下分屏的JavaScript方法

使用HTML和CSS创建基础结构,通过JavaScript动态调整分屏比例。以下是一个简单的实现示例:

HTML结构

<div class="split-container">
  <div class="top-pane" id="topPane">上部分内容</div>
  <div class="divider" id="divider"></div>
  <div class="bottom-pane" id="bottomPane">下部分内容</div>
</div>

CSS样式

.split-container {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.top-pane, .bottom-pane {
  position: absolute;
  width: 100%;
  overflow: auto;
}

.top-pane {
  top: 0;
  height: 50%;
  background-color: #f0f0f0;
}

.bottom-pane {
  bottom: 0;
  height: 50%;
  background-color: #e0e0e0;
}

.divider {
  position: absolute;
  width: 100%;
  height: 5px;
  background-color: #ccc;
  cursor: row-resize;
  z-index: 10;
}

JavaScript实现

const divider = document.getElementById('divider');
const topPane = document.getElementById('topPane');
const bottomPane = document.getElementById('bottomPane');
let isDragging = false;

divider.addEventListener('mousedown', function(e) {
  isDragging = true;
  document.body.style.cursor = 'row-resize';
  e.preventDefault();
});

document.addEventListener('mousemove', function(e) {
  if (!isDragging) return;

  const containerHeight = document.querySelector('.split-container').offsetHeight;
  const dividerPosition = e.clientY;
  const topHeight = (dividerPosition / containerHeight) * 100;
  const bottomHeight = 100 - topHeight;

  topPane.style.height = `${topHeight}%`;
  bottomPane.style.height = `${bottomHeight}%`;
  divider.style.top = `${topHeight}%`;
});

document.addEventListener('mouseup', function() {
  isDragging = false;
  document.body.style.cursor = '';
});

使用现成库的方案

对于更复杂的需求,可以考虑使用现成的分屏库:

Split.js实现

import Split from 'split.js';

Split(['#topPane', '#bottomPane'], {
  direction: 'vertical',
  sizes: [50, 50],
  minSize: [100, 100],
  gutterSize: 5,
  cursor: 'row-resize'
});

响应式处理

添加窗口大小变化时的处理逻辑:

js实现上下分屏

window.addEventListener('resize', function() {
  const container = document.querySelector('.split-container');
  const currentTopHeight = parseFloat(topPane.style.height) || 50;

  topPane.style.height = `${currentTopHeight}%`;
  bottomPane.style.height = `${100 - currentTopHeight}%`;
  divider.style.top = `${currentTopHeight}%`;
});

注意事项

  • 确保容器元素有明确的高度设置
  • 考虑添加最小高度限制防止面板太小
  • 移动端设备可能需要添加触摸事件支持
  • 复杂的应用场景建议使用专业库如Split.js或Golden Layout

以上方案提供了从基础到进阶的多种实现方式,可根据具体需求选择适合的方法。

标签: 上下js
分享给朋友:

相关文章

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…

js sort 实现

js sort 实现

数组排序基础方法 JavaScript 数组的 sort() 方法默认按照字符串 Unicode 码点顺序排序。直接调用时会将元素转为字符串再比较: const arr = [10, 2, 5, 1…

js实现放大缩小

js实现放大缩小

使用 CSS transform 实现缩放 通过修改元素的 transform 属性实现平滑缩放效果。CSS 的 scale() 函数可以轻松调整元素大小。 const element = docu…

vue js 实现排序

vue js 实现排序

Vue.js 实现排序的方法 在 Vue.js 中实现排序可以通过多种方式完成,包括使用计算属性、方法或直接操作数据。以下是几种常见的实现方式: 使用计算属性排序 计算属性可以自动响应数据变化并重新…

vue.js实现拍照

vue.js实现拍照

Vue.js 实现拍照功能 在 Vue.js 中实现拍照功能通常需要结合浏览器的媒体 API(如 getUserMedia)和 Canvas 元素。以下是实现步骤: 准备工作 确保项目已安装 Vue…

vue.js实现tab

vue.js实现tab

Vue.js 实现 Tab 切换功能 在 Vue.js 中实现 Tab 切换功能可以通过动态组件或条件渲染实现。以下是两种常见方法: 动态组件实现 通过 <component> 结合 v…