div 拖拽改变容器宽度
2018-01-23 10:46
344 查看
左右排版的 div 容器,可以拖动改变 div 宽度的占比。
完整代码:
完整代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Resizable Div Container</title> <meta name="description" content="可以对div进行拖动缩放大小"> <style type="text/css"> #container.resizable { width: 100%; height: 100%; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: moz-none; -ms-user-select: none; user-select: none; position: relative; opacity: 0; } #container.resizable #left { position: absolute; top: 0px; bottom: 0px; left: 0px; right: 85%; overflow: hidden; background-color: #ccc; } #container.resizable #right { position: absolute; right: 0px; top: 0px; bottom: 0px; width: 85%; background: #fff; padding-left: 4px; overflow: hidden; background-color: #ddd; } #container.resizable #resizor { position: absolute; top: 50%; right: 85%; cursor: w-resize; background-color: #666; border-radius: 5px; margin-top: -10px; width: 6px; height: 20px; /*background: url('/public/static/admin/images/resize.png') repeat-y;*/ background-size: cover; background-position: center; z-index: 99999; } </style> </head> <body> <div id="container" class="resizable"> <!-- Left side --> <div id="left"> <p>左侧内容</p> </div> <div id="resizor" title="拖动我"></div> <!-- Right side --> <div id="right"> <p>右侧内容</p> </div> </div> <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script> <script type="text/javascript"> var isResizing = false; var lastDownX = 0; $(function () { var container = $('#container.resizable'); // 根据当前窗口大小设定 container 的高度 container.css({'height': ($(window).height() - 20), 'opacity': 1}); var left = $('#left'); var right = $('#right'); var resizor = $('#resizor'); resizor.on('mousedown', function (e) { isResizing = true; lastDownX = e.clientX; }); $(document).on('mousemove', function (e) { if (!isResizing) return true; var offsetRight = container.width() - (e.clientX - container.offset().left); // 判断左右拖动范围 if (offsetRight < 0 || offsetRight >= container.width()) { isResizing = false; return true; } left.css('right', offsetRight); resizor.css('right', offsetRight); right.css('width', offsetRight); }).on('mouseup', function (e) { isResizing = false; }); }); </script> </body> </html>
相关文章推荐
- 可拖拽边框改变div宽度的管理界面——jQuery UI 实现
- 拖拽改变div的大小
- 浏览器窗口大小改变时,动态改变div高度和宽度
- js改变div宽度
- jquery动态改变div宽度和高度
- 鼠标拖动改变div容器的大小
- jquery实现动态改变div宽度和高度
- jquery拖拽改变div大小效果
- jquery动态改变div宽度和高度
- jquery实现div拖拽宽度
- jquery实现div拖拽宽度示例代码
- DIV,Table固定宽度,防止数字和字母将容器撑大
- td内容自动换行 ,td超过宽度显示点点点… , td 使用 overflow:hidden 无效,英文 数字 不换行 撑破div容器
- 作为容器的div层设置宽度
- 如何自动在html页面加载时动态改变div等元素的高度和宽度
- 完美拖拽+拖动改变Div的宽高+关闭按钮[带演示]
- CSS如何让DIV的宽度随内容的改变而改变
- 鼠标拖拽改变对象的属性,鼠标拖拽DIV移动
- 拖动鼠标改变div层的大小宽度
- 如何让FlowLayoutPanel容器里面的【子控件】随着容器(宽度)发送改变而发送改变?