0

這裏的問題是,在我的Rails應用程序中,我編寫了兩個咖啡腳本文件,都需要window.onscroll fn。我的兩個咖啡文件在Rails中相互干擾

當我一次一個實現它們時,兩者都可以正常工作,但是當我包含這兩個參數時,腳本停止工作。

這是第一個腳本

rightBarControl = -> 
    windowHeight = $(window).height() 
    scrollHeight = $(window).scrollTop() 
    rightBarWidth = $('#index_top_confession_div').width() 
    #20% of .main width 
    rightBarHeight = $('#index_top_confession_div').outerHeight() 
    rightBarOffset = $('#index_confessions').offset().left + $('#index_confessions').outerWidth() 
    rightBarTop = 75 
    #30 because .head is 30px high 
    if windowHeight - 75 < rightBarHeight 
    #Again including 30 because of .head 
    rightBarTop = windowHeight - rightBarHeight 
    if windowHeight + scrollHeight - 75 >= rightBarHeight 
    $('#index_top_confession_div').css 
     position: 'fixed' 
     left: rightBarOffset 
     top: rightBarTop 
    else 
    $('#index_top_confession_div').css 
     position: 'static' 
     left: rightBarOffset 
     top: rightBarTop 
    return 

$('#search').addClass 'form-control' 
$(window).scroll rightBarControl 
#Run control on window scroll 
$(window).resize rightBarControl 

第二個腳本文件..

$(document).ready -> 
    if $('.pagination').length 
    $(window).scroll -> 
     console.log('hey'); 
     url = undefined 
     url = $('.pagination .next_page').attr('href') 
     if url and $(window).scrollTop() > $(document).height() - $(window).height() - 200 
     $('.pagination').html '<img src = \'/uploads/loader/loader.gif\' alt=\'loading...\'/>' 
     return $.getScript(url) 
     return 
    return $(window).scroll() 
    return 

回答

0

資產管道將所有的CSS和Javascript文件合併成一個大文件。

當你在兩個咖啡文件夾中都有$(window).scroll()時,兩個函數都會運行。

解決方法很少。一種方法是編輯您的layout.html.erb以將控制器名稱作爲主體classid。然後你就可以在你的CoffeeScript檢查到正確的網頁上運行的正確JS

另一種方法是,確保你總是針對由idclass的元素,而不是避免使用像bodywindow

網站範圍內的目標
+0

嗯..點是在同一頁面上運行..一個控制右邊欄和一個控制無限滾動 –