当前位置:首页 > JavaScript

js实现上下分屏

2026-03-16 01:56:21JavaScript

实现上下分屏的基本思路

使用HTML和CSS创建两个固定高度的容器,分别放置上下两部分内容。通过JavaScript动态调整高度或处理交互逻辑。

js实现上下分屏

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%;
}

.bottom-pane {
  bottom: 0;
  height: 50%;
}

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

JavaScript实现拖动调整

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

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

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

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

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

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

响应式处理

添加窗口大小变化的监听,确保分屏布局适应不同屏幕尺寸:

js实现上下分屏

window.addEventListener('resize', () => {
  const containerHeight = document.querySelector('.split-container').offsetHeight;
  const topHeightPercent = (parseFloat(topPane.style.height) || 50);

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

替代方案:使用CSS Grid

对于不需要拖动调整的简单分屏,可以使用CSS Grid布局:

.grid-container {
  display: grid;
  grid-template-rows: 1fr 1fr;
  height: 100vh;
}

.grid-top {
  overflow: auto;
}

.grid-bottom {
  overflow: auto;
}

注意事项

  1. 确保容器有明确的高度值(如100vh或固定像素值)
  2. 考虑为内容区域添加overflow属性处理内容溢出
  3. 移动端设备可能需要添加触摸事件支持
  4. 复杂的交互场景建议使用现成的库如Split.js

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

相关文章

js实现轮播

js实现轮播

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

js实现验证码

js实现验证码

使用Canvas生成图形验证码 在HTML中创建一个Canvas元素用于绘制验证码。通过JavaScript随机生成数字或字母组合,并添加干扰线、噪点等干扰元素增强安全性。 <canvas i…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('image…

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document.…

js实现pdf在线预览

js实现pdf在线预览

使用PDF.js实现PDF在线预览 PDF.js是由Mozilla开发的一个开源JavaScript库,可以在网页中直接渲染PDF文件。以下是实现PDF在线预览的步骤: 引入PDF.js库 在HT…

jquery js

jquery js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够更高…