2016-09-15 77 views
-1

目前我有下面的JS。我正在嘗試運行slideout.close();事件,其中smoothstate onStart。我的代碼在下面,我目前在控制檯中出現錯誤。有人能幫助我嗎?函數沒有正確觸發

感謝。

$(document).ready(function() { 
 
    slideout(); 
 
}); 
 

 
function slideout() { 
 
    var slideout = new Slideout({ 
 
     'panel': document.getElementById('slideout-content'), 
 
     'menu': document.getElementById('slideout-nav'), 
 
     'padding': 256, 
 
     'tolerance': 70, 
 
     'side': 'right' 
 
    }); 
 

 
    $('.mobile-nav__icon').on('click', function() { 
 
     slideout.toggle(); 
 
    }); 
 
} 
 

 
$(function(){ 
 
    'use strict'; 
 
    var options = { 
 
    prefetch: true, 
 
    cacheLength: 2, 
 
    onStart: { 
 
     duration: 860, 
 
     render: function ($container) { 
 
     $container.addClass('is-exiting'); 
 
     smoothState.restartCSSAnimations(); 
 
     slideout.close(); 
 
     } 
 
    }, 
 
    onReady: { 
 
     duration: 0, 
 
     render: function ($container, $newContent) { 
 
     $container.removeClass('is-exiting'); 
 
     $container.html($newContent); 
 
     } 
 
    }, 
 
    onAfter: function() { 
 
     slideout(); 
 
    } 
 
    }, 
 
    smoothState = $('#animate-wrapper').smoothState(options).data('smoothState'); 
 
});

+0

如果你指定什麼錯誤實際上是 – Li357

+0

'的script.js我很偉大:29未捕獲TypeError:slideout.close不是函數' – Ryan

+0

'slideout'是函數,而不是對象。也許你正試圖訪問本地的? – Li357

回答

1

您已經創建了一個名爲slideout全局函數,在其中你有一個局部變量叫slideout那就是指Slideout對象的一個​​ - 你不能訪問本地來自其他功能的變量。當您嘗試使用slideout.close()正在尋找.close()方法函數

一個修復方法是更改​​變量或函數的名稱,並將變量設置爲全局變量,以便您可以隨時訪問它。但是添加更多全局變量很麻煩。我認爲將所有代碼合併到單個文檔就緒處理程序中應該沒有問題,因此所有內容都在同一個作用域中,而不需要任何全局變量(您仍然需要爲變量使用不同的名稱)。

我無法測試以下,因爲我沒有什麼Slideout是,但是:

$(document).ready(function() { 
    'use strict'; 

    var slideout; // variable that is local to the doc ready handler function 
       // and accessible to all code within that handler 

    function initSlideout() { // function renamed 
    slideout = new Slideout({ // assign to variable declared above 
     'panel': document.getElementById('slideout-content'), 
     'menu': document.getElementById('slideout-nav'), 
     'padding': 256, 
     'tolerance': 70, 
     'side': 'right' 
    }); 
    } 
    initSlideout(); 

    $('.mobile-nav__icon').on('click', function() { 
    slideout.toggle(); 
    }); 

    var options = { 
    prefetch: true, 
    cacheLength: 2, 
    onStart: { 
     duration: 860, 
     render: function ($container) { 
     $container.addClass('is-exiting'); 
     smoothState.restartCSSAnimations(); 
     slideout.close(); 
     } 
    }, 
    onReady: { 
     duration: 0, 
     render: function ($container, $newContent) { 
     $container.removeClass('is-exiting'); 
     $container.html($newContent); 
     } 
    }, 
    onAfter: function() { 
     initSlideout(); 
    } 
    }, 
    smoothState = $('#animate-wrapper').smoothState(options).data('smoothState'); 
}); 
+0

現在一切似乎都奏效,但是我只需要滑出即可在smoothstate完成後重新初始化。這是工作之前,但現在這一點不工作。 'onAfter:function(){slide}(); }' – Ryan

+0

哦,對不起,我沒注意到。 (在我的回答中,我說你似乎沒有在其他地方調用'slideout()',但實際上你是這樣做的。)答案已更新。 – nnnnnn

+0

謝謝,非常感謝。 – Ryan