JS实现左右拖动改变内容显示区域大小的方法

这里演示左右可拖动的内容显示区效果,左右拖动红条改变显示区域宽度大小,往左拖则全部显示右侧内容,往右拖则全部显示左则内容,类似QQ的聊天窗口一样,可以向上或向下拖动大小,以适合使用者的要求。本代码完全JavaScript与CSS代码结合实现,在各主流浏览器内运行的兼容性也不错,左右拖动改变大小的JS代码推荐给大家。

运行效果截图如下:

JS实现左右拖动改变内容显示区域大小的方法

在线演示地址如下:

具体代码如下:

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>左右可拖动的内容显示区</title> <style> ul,li{margin:0;padding:0;} body{font:14px/1.5 Arial;color:#666;} #box{position:relative;width:600px;height:400px;border:2px solid #000;margin:10px auto;overflow:hidden;} #box ul{list-style-position:inside;margin:10px;} #top,#bottom{color:#FFF;width:300px;height:400px;overflow:hidden;} #top{background:green; float:left} #bottom{background:skyblue;float:right} #line{position:absolute;top:0;left:50%;height:100%;width:4px;overflow:hidden;background:red;cursor:w-resize;} </style> <script> function $(id) { return document.getElementById(id) } window.onload = function() { var oBox = $("box"), oTop = $("top"), oBottom = $("bottom"), oLine = $("line"); oLine.onmousedown = function(e) { var disX = (e || event).clientX; oLine.left = oLine.offsetLeft; document.onmousemove = function(e) { var iT = oLine.left + ((e || event).clientX - disX); var e=e||window.event,tarnameb=e.target||e.srcElement; var maxT = oBox.clientWight - oLine.offsetWidth; oLine.style.margin = 0; iT < 0 && (iT = 0); iT > maxT && (iT = maxT); oLine.style.left = oTop.style.width = iT + "px"; oBottom.style.width = oBox.clientWidth - iT + "px"; $("msg").innerText='top.width:'+oLine.style.width+'---bottom.width:'+oBottom.style.width+'---oLine.offsetLeft:'+oLine.offsetLeft+'---disX:'+disX+'---tarnameb:'+tarnameb.tagName; return false }; document.onmouseup = function() { document.onmousemove = null; document.onmouseup = null; oLine.releaseCapture && oLine.releaseCapture() }; oLine.setCapture && oLine.setCapture(); return false }; }; </script> </head> <body> <center>左右拖动红条改变显示区域宽度<span></span></center> <div> <div> <ul> <li><a href="#" target="_blank">jQuery初学实例代码集</a></li><li><a href="#" target="_blank">100多个ExtJS应用初学实例集</a></li> <li><a href="#" target="_blank">基于jQuery的省、市、县三级级联菜单</a></li> <li><a href="#" target="_blank">一个类似QQ网的JS相册展示特效</a></li> <li><a href="#" target="_blank">eWebEditor v4.60 最新通用精简版</a></li> <li><a href="#" target="_blank">FCKeditor 2.6.4.1 网页编辑器</a></li> <li><a href="#" target="_blank">jQuery平滑图片滚动</a></li> <li><a href="#" target="_blank">Xml+JS省市县三级联动菜单</a></li> <li><a href="#" target="_blank">jQuery 鼠标滑过链接文字弹出层提示的效果</a></li> <li><a href="#" target="_blank">JS可控制的图片左右滚动特效(走马灯)</a></li> </ul> </div> <div> <ul> <li><a href="#" target="_blank">网页上部大Banner广告特效及图片横向滚动代码</a></li> <li><a href="#" target="_blank">FlexSlider网页广告、图片焦点图切换插件</a></li> <li><a href="#" target="_blank">兼容IE,火狐的JavaScript图片切换</a></li> <li><a href="#" target="_blank">jQuery仿ios无线局域网WIFI提示效果(折叠面板)</a></li> <li><a href="#" target="_blank">TopUp js图片展示及弹出层特效代码</a></li> <li><a href="#" target="_blank">jQuery仿Apple苹果手机放大镜阅读效果</a></li> <li><a href="#" target="_blank">Colortip 文字title多样式提示插件</a></li> <li><a href="#" target="_blank">网页换肤,Ajax网页风格切换代码集</a></li> <li><a href="#" target="_blank">超强大、漂亮的蓝色网页弹出层效果</a></li> <li><a href="#" target="_blank">jQuery 图像预览功能的代码实现</a></li> </ul> </div> <div></div> </div> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wggyfs.html