Codrops JavaScript overlay,undefined不是函数错误

时间:2014-07-19 16:53:29

标签: javascript function overlay thumbnails

我想使用Tod形式Codrops为我的投资组合制作叠加层。单击缩略图时,应打开项目覆盖图。这是tut / markup / javascript:http://tympanus.net/codrops/2014/02/06/fullscreen-overlay-effects/(我使用示例编号7,'内容推送')

但问题是'trigger-overlay'是一个id,而不是一个类,因为在tut中这个例子只使用了一个按钮。我有多个缩略图,所以我将var triggerBttn从'getElementById'更改为'getElementsByClassName'

    (function() {
    var container = document.querySelector( 'div.pagewrapper' ),
        triggerBttn = document.getElementsByClassName( 'trigger-overlay' ),
        overlay = document.querySelector( 'div.overlay' ),
        closeBttn = overlay.querySelector( 'button.overlay-close' );
        transEndEventNames = {
            'WebkitTransition': 'webkitTransitionEnd',
            'MozTransition': 'transitionend',
            'OTransition': 'oTransitionEnd',
            'msTransition': 'MSTransitionEnd',
            'transition': 'transitionend'
        },
        transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
        support = { transitions : Modernizr.csstransitions };

    function toggleOverlay() {
        if( classie.has( overlay, 'open' ) ) {
            classie.remove( overlay, 'open' );
            classie.remove( container, 'overlay-open' );
            classie.add( overlay, 'close' );
            var onEndTransitionFn = function( ev ) {
                if( support.transitions ) {
                    if( ev.propertyName !== 'visibility' ) return;
                    this.removeEventListener( transEndEventName, onEndTransitionFn );
                }
                classie.remove( overlay, 'close' );
            };
            if( support.transitions ) {
                overlay.addEventListener( transEndEventName, onEndTransitionFn );
            }
            else {
                onEndTransitionFn();
            }
        }
        else if( !classie.has( overlay, 'close' ) ) {
            classie.add( overlay, 'open' );
            classie.add( container, 'overlay-open' );
        }
    }

    triggerBttn.addEventListener( 'click', toggleOverlay );
    closeBttn.addEventListener( 'click', toggleOverlay );

})();

代码取决于classie.js

但现在我收到了错误

  

未捕获的TypeError:undefined不是函数

就行:

triggerBttn.addEventListener( 'click', toggleOverlay );

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我发现一个简单的问题是他们没有在" closeBttn"之后定义其余的变量,这段代码阻止了变量的其余部分。

错误

closeBttn = overlay.querySelector( 'button.overlay-close' );

应该是

closeBttn = overlay.querySelector( 'button.overlay-close' ),

所以最终的代码将是

var triggerBttn = document.getElementById( 'trigger-navbar' ),
        navbar = document.querySelector( 'section.navbar' ),
        closeBttn = navbar.querySelector( 'a.navbar-close' ),
        navClick = navbar.querySelector( 'section.navbar nav ul li a' ),
        transEndEventNames = {
            'WebkitTransition': 'webkitTransitionEnd',
            'MozTransition': 'transitionend',
            'OTransition': 'oTransitionEnd',
            'msTransition': 'MSTransitionEnd',
            'transition': 'transitionend'
        },
        transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
        support = { transitions : Modernizr.csstransitions };

        function toggleOverlay() {
        if( classie.has( navbar, 'open' ) ) {
            classie.remove( navbar, 'open' );
            classie.add( navbar, 'close' );
            var onEndTransitionFn = function( ev ) {
                if( support.transitions ) {
                    if( ev.propertyName !== 'visibility' ) return;
                    this.removeEventListener( transEndEventName, onEndTransitionFn );
                }
                classie.remove( navbar, 'close' );
            };
            if( support.transitions ) {
                navbar.addEventListener( transEndEventName, onEndTransitionFn );
            }
            else {
                onEndTransitionFn();
            }
        }
        else if( !classie.has( navbar, 'close' ) ) {
            classie.add( navbar, 'open' );
        }
    }