简体   繁体   English

为什么JavaScript代码无法在codepen.io中工作

[英]Why won't the JavaScript code work out of codepen.io

So there is some code from codepen.io http://codepen.io/karolpodlesny/pen/npKqu . 因此,有一些来自codepen.io的代码http://codepen.io/karolpodlesny/pen/npKqu It is uploaded here: http://fredricarms.com/javatestindex.html . 它在此处上传: http : //fredricarms.com/javatestindex.html

Now, the HTML, CSS and JavaScript for making the boxes expand and do all the cool things, are working how they are supposed to, being in separate files, so is the modernizr. 现在,用于制作包装盒的HTML,CSS和JavaScript可以扩展并完成所有很酷的事情,它们应该工作在单独的文件中,而Modernizr也是如此。 I also the know that the js is being called because in the boxlayout.js I wrote some code to bring up and alert box and it worked just fine. 我也知道正在调用js,因为在boxlayout.js中,我编写了一些代码来调出警告框,并且效果很好。 So I am guessing that codepen fixes the code so it runs perfectly. 因此,我猜想Codepen会修复代码,使其完美运行。 I just don't know what is wrong with the code in the boxlayout.js that is not working on my server. 我只是不知道在我的服务器上无法使用的boxlayout.js中的代码出了什么问题。 Please help and thank you so much. 请帮助,非常感谢。 Below is the code in the boxlayout js file. 以下是boxlayout js文件中的代码。

var Boxlayout = (function() {
    var $el = $( '#bl-main' ),
        $sections = $el.children( 'section' ),
        // works section
        $sectionWork = $( '#bl-work-section' ),
        // work items
        $workItems = $( '#bl-work-items > li' ),
        // work panels
        $workPanelsContainer = $( '#bl-panel-work-items' ),
        $workPanels = $workPanelsContainer.children( 'div' ),
        totalWorkPanels = $workPanels.length,
        // navigating the work panels
        $nextWorkItem = $workPanelsContainer.find( 'nav > span.bl-next-work' ),
        // if currently navigating the work items
        isAnimating = false,
        // close work panel trigger
        $closeWorkItem = $workPanelsContainer.find( 'nav > span.bl-icon-close' ),
        transEndEventNames = {
            'WebkitTransition' : 'webkitTransitionEnd',
            'MozTransition' : 'transitionend',
            'OTransition' : 'oTransitionEnd',
            'msTransition' : 'MSTransitionEnd',
            'transition' : 'transitionend'
        },
        // transition end event name
        transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
        // support css transitions
        supportTransitions = Modernizr.csstransitions;

    function init() {
        initEvents();
    }

    function initEvents() {

        $sections.each( function() {

            var $section = $( this );

            // expand the clicked section and scale down the others
            $section.on( 'click', function() {

                if( !$section.data( 'open' ) ) {
                    $section.data( 'open', true ).addClass( 'bl-expand bl-expand-top' );
                    $el.addClass( 'bl-expand-item' );   
                }

            } ).find( 'span.bl-icon-close' ).on( 'click', function() {

                // close the expanded section and scale up the others
                $section.data( 'open', false ).removeClass( 'bl-expand' ).on( transEndEventName, function( event ) {
                    if( !$( event.target ).is( 'section' ) ) return false;
                    $( this ).off( transEndEventName ).removeClass( 'bl-expand-top' );
                } );

                if( !supportTransitions ) {
                    $section.removeClass( 'bl-expand-top' );
                }

                $el.removeClass( 'bl-expand-item' );

                return false;

            } );

        } );

        // clicking on a work item: the current section scales down and the respective work panel slides up
        $workItems.on( 'click', function( event ) {

            // scale down main section
            $sectionWork.addClass( 'bl-scale-down' );

            // show panel for this work item
            $workPanelsContainer.addClass( 'bl-panel-items-show' );

            var $panel = $workPanelsContainer.find("[data-panel='" + $( this ).data( 'panel' ) + "']");
            currentWorkPanel = $panel.index();
            $panel.addClass( 'bl-show-work' );

            return false;

        } );

        // navigating the work items: current work panel scales down and the next work panel slides up
        $nextWorkItem.on( 'click', function( event ) {

            if( isAnimating ) {
                return false;
            }
            isAnimating = true;

            var $currentPanel = $workPanels.eq( currentWorkPanel );
            currentWorkPanel = currentWorkPanel < totalWorkPanels - 1 ? currentWorkPanel + 1 : 0;
            var $nextPanel = $workPanels.eq( currentWorkPanel );

            $currentPanel.removeClass( 'bl-show-work' ).addClass( 'bl-hide-current-work' ).on( transEndEventName, function( event ) {
                if( !$( event.target ).is( 'div' ) ) return false;
                $( this ).off( transEndEventName ).removeClass( 'bl-hide-current-work' );
                isAnimating = false;
            } );

            if( !supportTransitions ) {
                $currentPanel.removeClass( 'bl-hide-current-work' );
                isAnimating = false;
            }

            $nextPanel.addClass( 'bl-show-work' );

            return false;

        } );

        // clicking the work panels close button: the current work panel slides down and the section scales up again
        $closeWorkItem.on( 'click', function( event ) {

            // scale up main section
            $sectionWork.removeClass( 'bl-scale-down' );
            $workPanelsContainer.removeClass( 'bl-panel-items-show' );
            $workPanels.eq( currentWorkPanel ).removeClass( 'bl-show-work' );

            return false;

        } );

    }

    return { init : init };
})();

Your code relies entirely on jQuery, however you haven't included jQuery on your live site. 您的代码完全依赖jQuery,但是您尚未在现场站点中包含jQuery。 On your CodePen example you're using jQuery 1.9.1: 在您的CodePen示例中,您使用的是jQuery 1.9.1:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">

You can continue to use the Google-hosted version, or if you want to host it yourself you can download jQuery from jQuery's own website . 您可以继续使用Google托管的版本,或者如果您想自己托管它,则可以从jQuery自己的网站下载jQuery。

The problem is that you need to initialize your BoxLayout once your document has finished loading (all those jQuery variables you're using inside your library won't be defined because their equivalent dom elements are not yet rendered), that's why you need to init everything when dom is ready. 问题在于,文档加载完成后,您需要初始化BoxLayout(由于尚未渲染等效的dom元素,因此不会定义您正在库中使用的所有jQuery变量),这就是为什么需要初始化dom准备就绪时的一切。

Add this line of code to the bottom of your boxlayout.js file: 将以下代码行添加到boxlayout.js文件的底部:

$(document).ready(function() {
    Boxlayout.init();
});

Just for testing purposes, open up the web developer console on your website, and run this js code: 仅出于测试目的,在您的网站上打开Web开发人员控制台,然后运行以下js代码:

Boxlayout.init();

You'll see everything will work just fine. 您会看到一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM