js实现上下拖动,改变导航栏的高度

29 篇文章 0 订阅
5 篇文章 0 订阅

刚写了一个左右拖动改变内容宽度的例子,自己又改了一下,做了一个上下拖动改变高度的例子,后面需要的话,可以直接拿来用,哈哈哈。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</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:600px;
  height:200px;
  overflow:hidden;
}
#top{
  background:green; 
}
#bottom{
  background:skyblue;  
  position: absolute;
  /*float:right*/
}
#line{
  position:absolute;
  top:50%;
  left:0;
  height:4px;
  width:100%;
  overflow:hidden;
  background:red;
  cursor:s-resize;
}
</style>
</head>
<body>
<center>上下拖动红条可改变显示区域宽度<span id="msg"></span></center>
<div id="box">
 <div id="top">
 上
 </div>
 <div id="bottom">
 下
 </div>
 <div id="line"></div>
</div>

<script>
function $(id) {
 return document.getElementById(id); 
}
window.onload = function() {
 var oBox = $("box"), oTop = $("top"), oBottom = $("bottom"), oLine = $("line");

 oLine.onmousedown = function(e) {
   var disY = (e || event).clientY;
   oLine.top = oLine.offsetTop;

   document.onmousemove = function(e) {
      var iT = oLine.top + ((e || event).clientY - disY);
      var e=e||window.event,tarnameb=e.target||e.srcElement;
      var maxT = oBox.clientHeight - oLine.clientHeight;
      oLine.style.margin = 0;
      iT < 0 && (iT = 0);
      iT > maxT && (iT = maxT);
      oLine.style.top = oTop.style.height = iT + "px";
      oBottom.style.height = oBox.clientHeight - iT + "px";
     $("msg").innerText='top.width:'+oLine.style.height+'---bottom.height:'+oBottom.style.height+'---oLine.offsetHeight:'+oLine.offsetHeight+'---disY:'+disY+'---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>
</body>
</html>

若想变为自适应的,可以把宽高设置为百分比。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值