原生js实现一个折叠面板的功能

基本原理是父容器的高度设置为0,超出部分overflow:hidden,在事件触发或者方法被调用的时候更改父元素的高度即可.动态的改变父元素的高度就可以实现对应的效果,可以加入缓动动画公式来实现。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
    #box{
        height: 0;
        overflow: hidden;
    }
        #div{
            background: blue;width: 100px;min-height: 200px;
        }
    </style>
</head>
<body>
    <span id="span">展开</span>
    <div id="box">
        <div style="" id="div">我是div
        我是div<br/>
        </div>
    </div>
    
    <script type="text/javascript">
        
        var tween = {
            linear: function (t,b,c,d) {
                return c*t/d +b;
            },
            easeIn: function () {
                return c* (t/=d)*t +b;
            }
        }

        var Animate = function (dom) {
            this.dom = dom;         // 进行运动的dom节点
            this.startTime = 0;     // 动画开始的时间
            this.startPos = 0;      // 动画开始时dom节点的位置
            this.endPos = 0;
            this.propertyName = null;
            this.easing = null;
            this.duration = null;
        }
        Animate.prototype.start = function (propertyName,endPos,duration,easing,fn) {
            this.startTime = +new Date;
            this.startPos = this.dom.getBoundingClientRect()[propertyName]
            this.propertyName = propertyName
            this.endPos = endPos;
            this.duration = duration
            this.easing = tween[easing]

            var self = this;
            var timeId = setInterval(function () {
                if (self.step() === false) {
                    clearInterval(timeId)
                    fn && fn()
                }
            }, 19)
        }
        Animate.prototype.step = function () {
            var t = +new Date;
            if (t>=this.startTime + this.duration){
                this.update(this.endPos)
                return false
            }
            var pos = this.easing(t -this.startTime,this.startPos,this.endPos - this.startPos,this.duration)
            this.update(pos)
        }
        Animate.prototype.update = function (pos) {
            this.dom.style[this.propertyName] = pos + 'px'
        }
        var div = document.getElementById('div')
        var box = document.getElementById('box')
        let sty = '收起'
        let initPos = 0;
        // alert(div.offsetHeight)
        document.getElementById('span').onclick = function () {
            
            let realH = div.offsetHeight
            var animate = new Animate(box);
            if (initPos !== 0) {
                animate.start('height', 0, 1000, 'linear',function(){
                    initPos = 0
                })
                return;
            }
            console.log(realH, div.style.height)
            
            animate.start('height', realH, 1000, 'linear',function(){
                initPos = box.offsetHeight
            })
        }

    </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 【原创.七绝】新韵 柳岸信步 杨柳低垂绕倚栏, 清风卷叶岸边漩...
    余晖远眺阅读 186评论 0 6
  • 我怎么如此幸运今天该我们俩待客,可是爸爸腰扭了,今天是我们第一次过年去饭店吃饭,虽然包间不够大,没有俩桌人在一起,...
    Wan9sha阅读 160评论 0 0
  • 凌晨的天边透过一丝从黑夜里挣脱出来的微光,又一个夜晚被不可避免的拉长了!怎么样去形容和描述我的18岁呢?自忖...
    R丶eset阅读 244评论 0 0
  • "真是糟糕的一天!","他没回我的电话,真是太没礼貌了!",“她真让我失望!” 我们通常用抱怨的形式,讲给自己和他...
    觉知中的帆阅读 102评论 0 0

友情链接更多精彩内容