원본 js 접기 패널 기능 구현

3269 단어
기본 원리는 부모 용기의 높이를 0으로 설정하고 일부overflow:hidden을 초과하여 이벤트가 터치되거나 방법이 호출될 때 부모 요소의 높이를 변경하면 됩니다.동적으로 부모 요소의 높이를 바꾸면 대응하는 효과를 실현할 수 있고 느린 애니메이션 공식을 넣어 실현할 수 있다.



    
    <style type="text/css">
    #box{
        height: 0;
        overflow: hidden;
    }
        #div{
            background: blue;width: 100px;min-height: 200px;
        }
    </style>


    <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>


</code></pre> 
</article>
                            </div>
                        </div>

좋은 웹페이지 즐겨찾기