动态上涨

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>动态上涨</title>
    <style type="text/css">
        #parentBox{
            width: 200px;
            height: 200px;
            border: 1px dashed #666;
            margin: 0 auto;
            overflow: hidden;
            /*让文本超出隐藏*/
        }
        #childBox{
            width:200px;
            background: #f00;
        }
    </style>
</head>
<body>
    <div id="parentBox">
        <div id="childBox">
            <p id="content" style="display:none;">内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域</p>
        </div>
    </div>
    <script type="text/javascript">
    //获取dom对象
        var parentBox=document.getElementById("parentBox");
        var childBox=document.getElementById("childBox");
        var content=document.getElementById("content");
        upHeight(parentBox,childBox,100);
    //upHeight为上涨的高度
    //parentBox为父容器
    //childBox为动态上涨部分
    //showHeight为想要让他上涨的高度(预期高度);
        function upHeight(parentBox,childBox,showHeight){
             parentBox.style.position="relative";
             childBox.style.position="absolute";
                childBox.style.bottom=0;
                childBox.style.height=0+"px";
                var interval;
            parentBox.οnmοuseenter=function(){
                 if(interval!=-1){//判断定时器是否清零
                       clearInterval(interval);
                    }
                interval=setInterval(function(){
                var height=parseFloat(childBox.style.height);
                    height+=10;
                    if(height>=showHeight){
                        height=showHeight;
                        clearInterval(interval);
                    }
                    childBox.style.height=height+"px";
                },100);
                content.style.display="block"; 
            };
            parentBox.οnmοuseleave=function(){
                if(interval!=-1){
                    clearInterval(interval);
                }
                interval=setInterval(function(){
                    var height=parseFloat(childBox.style.height);
                        height-=10;
                        if(height<=0){
                            height=0;
                            clearInterval(interval);
                            //把display放在定时器中,内容跟随下降消失,不会立刻消失。
                            content.style.display="none"; 
                        }
                        childBox.style.height=height+"px";
                },100);
                
            }
        }   
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值