当前位置:首页 > 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
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 jQ…

js jquery

js jquery

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

js实现dh

js实现dh

JavaScript 实现 DH(Diffie-Hellman)密钥交换 Diffie-Hellman(DH)密钥交换是一种安全协议,允许双方在不安全的通信信道上建立一个共享密钥。以下是如何在 Jav…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js实现求导

js实现求导

实现数值求导的方法 在JavaScript中实现求导通常采用数值方法,因为JavaScript不是符号计算语言。以下是常见的数值微分方法: 中心差分法 中心差分法提供较高精度的导数近似:…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的JWT令牌…