简体   繁体   English

右div的边距在调整大小时应与左textarea宽度匹配

[英]Margin of right div should match the left textarea width while resizing

When resizing the left <textarea> (with the bottom-right-corner resize area), how to have the right <div> 's margin automatically match the <textarea> 's width? 调整左<textarea>大小(具有右下角的调整大小区域)时, 如何使右<div>的边距自动匹配<textarea>的宽度?

 * { margin: 0; padding: 0; } #a { width: 50%; resize: horizontal; height: 100%; position: fixed; } #b { margin-left: 50%; } 
 <textarea id="a">hello</textarea> <div id="b">h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h</div> 

Note: the left textarea should always take 100% of the browser height (always displayed editor), whereas the right div can have a very long height (many pages to be scrolled). 注意:左侧的textarea应该始终占据浏览器高度的100%(始终显示为编辑器),而右侧的div可以具有很长的高度(许多页面需要滚动)。


Edit: this nearly works: 编辑:这几乎起作用:

 $('#a').data('x', $('#a').outerWidth()).mousemove(function () { if ($('#a').outerWidth() != $('#a').data('x')) { $('#b').css('margin-left', $('#a').outerWidth()); $('#a').data('x', $('#a').outerWidth()); } }); 
 * { margin: 0; padding: 0; } #a { width: 50%; resize: horizontal; height: 100%; position: fixed; } #b { margin-left: 50%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <textarea id="a">hello</textarea> <div id="b">h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h</div> 

but it doesn't work if you click down on the textareas's resizing area, and then drag the mouse out of the browser window (or if you drag the window over the console panel of the browser). 但是如果您向下单击文本区域的调整大小区域, 然后将鼠标拖出浏览器窗口 (或者将窗口拖到浏览器的控制台面板上), 则此方法将不起作用。

I don't think this is possible with position: fixed - you could put everything in a flex container and set the textarea to height: 100vh . 我认为position: fixed是不可能的- position: fixed -您可以将所有内容放在flex容器中并将textarea设置为height: 100vh Try running the snippet below. 尝试运行下面的代码段。

 * { margin: 0; padding: 0; } #flex { display: flex; } #a { width: 50%; resize: horizontal; } 
 <div id='flex'> <textarea id="a">hello</textarea> <div id="b">h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h<br>h</div> </div> 

This should work: 这应该工作:

 var a_width = $("#a").outerWidth(); $("#a").mouseup(function () { if (a_width != $("#a").outerWidth()) { a_width = $("#a").outerWidth(); $("#b").css( { marginLeft : a_width }); } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM