news 2026/4/23 13:04:07

【Svelte】像 vs code 一样的布局:三栏布局

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【Svelte】像 vs code 一样的布局:三栏布局

直接贴代码:

<scriptlang="ts">import{browser}from'$app/environment';import{onMount}from'svelte';// Layout stateletleftWidth=$state(33.33);letmiddleWidth=$state(33.33);letisResizingLeft=$state(false);letisResizingRight=$state(false);onMount(()=>{consthandleMouseMove=(e:MouseEvent)=>{constcontainer=document.getElementById('layout-container');if(!container)return;constcontainerRect=container.getBoundingClientRect();constcontainerWidth=containerRect.width;constmouseXRelativeToContainer=e.clientX-containerRect.left;if(isResizingLeft){constnewLeftWidth=(mouseXRelativeToContainer/containerWidth)*100;constcurrentRightBoundary=leftWidth+middleWidth;// Ensure minimum widths of 5% and that we don't cross the second resizerif(newLeftWidth>5&&newLeftWidth<currentRightBoundary-5){leftWidth=newLeftWidth;middleWidth=currentRightBoundary-newLeftWidth;}}elseif(isResizingRight){constleftBoundaryPx=(leftWidth/100)*containerWidth;constnewMiddleWidth=((mouseXRelativeToContainer-leftBoundaryPx)/containerWidth)*100;// Ensure middle column doesn't get too small (min 5%)// and doesn't push the right column below 5%if(newMiddleWidth>5&&(leftWidth+newMiddleWidth)<95){middleWidth=newMiddleWidth;}}};consthandleMouseUp=()=>{isResizingLeft=false;isResizingRight=false;document.body.style.cursor='default';};window.addEventListener('mousemove',handleMouseMove);window.addEventListener('mouseup',handleMouseUp);return()=>{window.removeEventListener('mousemove',handleMouseMove);window.removeEventListener('mouseup',handleMouseUp);};});</script><divid="layout-container"class="flex h-[calc(100vh-64px)] w-full overflow-hidden bg-white"style="user-select:{isResizingLeft || isResizingRight ?'none':'auto'}"><!-- Column 1 --><divclass="flex h-full flex-col border-r-4 border-black bg-red-100 shrink-0"style="flex: 0 0{leftWidth}%"><divclass="p-8"><h1class="text-4xl font-black text-red-600">COLUMN 1 - LEFT</h1><pclass="mt-4 text-xl">If you can see this, the layout is working.</p></div></div><!-- Resizer 1 --><div class="z-10 w-2 cursor-col-resize bg-black hover:bg-primary shrink-0" onmousedown={() => { isResizingLeft = true; document.body.style.cursor = 'col-resize'; }} ></div><!-- Column 2 --><divclass="flex h-full flex-col border-r-4 border-black bg-blue-100 shrink-0"style="flex: 0 0{middleWidth}%"><divclass="p-8"><h1class="text-4xl font-black text-blue-600">COLUMN 2 - MIDDLE</h1><pclass="mt-4 text-xl">Drag the black bars to resize.</p></div></div><!-- Resizer 2 --><div class="z-10 w-2 cursor-col-resize bg-black hover:bg-primary shrink-0" onmousedown={() => { isResizingRight = true; document.body.style.cursor = 'col-resize'; }} ></div><!-- Column 3 --><divclass="flex h-full flex-grow flex-col bg-green-100 min-w-0"><divclass="p-8"><h1class="text-4xl font-black text-green-600">COLUMN 3 - RIGHT</h1><pclass="mt-4 text-xl">Everything should be visible now.</p></div></div></div><style>:global(body){user-select:none;/* Prevent text selection while resizing */}</style>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 12:58:15

Java抽象类能有多个吗?接口呢?:一文讲清继承与实现的5大规则

第一章&#xff1a;Java抽象类能有多个吗&#xff1f;接口呢&#xff1f; 在Java中&#xff0c;一个类不能继承多个抽象类&#xff0c;但可以实现多个接口。这是由于Java语言设计遵循单继承多实现的原则&#xff0c;旨在避免多重继承带来的复杂性和歧义&#xff0c;例如“菱形继…

作者头像 李华
网站建设 2026/4/23 10:07:49

如何优雅地在Stream中实现动态多条件筛选?这一招让代码瞬间高大上

第一章&#xff1a;Stream多条件筛选的痛点与意义在现代Java开发中&#xff0c;Stream API已成为处理集合数据的核心工具之一。面对复杂的业务场景&#xff0c;开发者常需基于多个动态条件对数据进行筛选。然而&#xff0c;传统的硬编码方式难以灵活应对条件可变的情况&#xf…

作者头像 李华
网站建设 2026/4/20 4:03:32

好写作AI:从“一次性工具”到“养成系伙伴”的学术逆袭之路

有没有发现&#xff0c;你和写作工具的关系&#xff0c;有点像大学时代的“临时抱佛脚”&#xff1f;写论文前疯狂求助&#xff0c;论文一交立即失联。但真正的学术成长&#xff0c;需要的不是“一次性神器”&#xff0c;而是一个能陪你升级打怪的 “养成系伙伴” 。今天聊聊&a…

作者头像 李华
网站建设 2026/4/21 3:10:23

学术搜索入口:高效获取学术资源的重要途径与使用指南

做科研的第一道坎&#xff0c;往往不是做实验&#xff0c;也不是写论文&#xff0c;而是——找文献。 很多新手科研小白会陷入一个怪圈&#xff1a;在知网、Google Scholar 上不断换关键词&#xff0c;结果要么信息过载&#xff0c;要么完全抓不到重点。今天分享几个长期使用的…

作者头像 李华
网站建设 2026/4/23 12:15:37

Emotion2Vec+ Large科研应用:心理学实验数据分析流程

Emotion2Vec Large科研应用&#xff1a;心理学实验数据分析流程 1. 引言&#xff1a;为什么语音情感识别对心理学研究如此重要&#xff1f; 在心理学实验中&#xff0c;情绪状态的测量一直是核心课题之一。传统方法依赖问卷、量表或面部表情观察&#xff0c;这些方式虽然有效…

作者头像 李华