2014-01-13 150 views
-1

我有一個問題,我想建立一個Ajax的一頁,但我有2個不同的JS文件,因爲當我有我的代碼在1文件它不工作。jQuery麻煩與AJAX

我怎麼能結合這些JS,它並沒有在一個的.js文件的麻煩

(function($) { 

    $.fn.visible = function(partial) { 

     var $t   = $(this), 
      $w   = $(window), 
      viewTop  = $w.scrollTop(), 
      viewBottom = viewTop + $w.height(), 
      _top   = $t.offset().top, 
      _bottom  = _top + $t.height(), 
      compareTop = partial === true ? _bottom : _top, 
      compareBottom = partial === true ? _top : _bottom; 

     return ((compareBottom <= viewBottom) && (compareTop >= viewTop)); 

    }; 

})(jQuery); 

var win = $(window); 
var allMods = $(".module"); 

// Already visible modules 
allMods.each(function(i, el) { 
    var el = $(el); 
    if (el.visible(true)) { 
     el.addClass("already-visible"); 
    } 
}); 

win.scroll(function(event) { 

    allMods.each(function(i, el) { 
     var el = $(el); 
     if (el.visible(true)) { 
      el.addClass("come-in"); 
     } 
    }); 

}); 

與這一個至極的另一個頁面

var win = $(window); 
var allMods = $(".var"); 

// Already visible modules 
allMods.each(function(i, el) { 
    var el = $(el); 
    if (el.visible(true)) { 
     el.addClass("already-visible"); 
    } 
}); 

win.scroll(function(event) { 

    allMods.each(function(i, el) { 
     var el = $(el); 
     if (el.visible(true)) { 
      el.addClass("come-in-var"); 
     } 
    }); 
}); 

我怎麼能結合它規定?謝謝!

+0

當你將它們合併時會出現什麼錯誤? –

+0

這兩個動畫不工作:(一個第一個作品... – andre34

回答

1

變量allMods導致該問題。由於它指向兩個不同的選擇器。嘗試下面的代碼。

基於類別module/var addClass come-in/come-in-var

var allMods = $(".module, .var"); 
win.scroll(function(event) { 
    allMods.each(function(i, el) { 
     var el = $(el); 
     if (el.visible(true)) { 
      el.addClass(el.hasClass('module') ? "come-in" : "come-in-var"); 
     } 
    }); 
}); 
+0

不,它不工作:/ – andre34

+0

var win = $(窗口);哦,我刪除了這一行......非常感謝你的工作! – andre34

+0

太棒了:) –

0

allMods變量與第一個文件衝突。嘗試添加範圍到你的第二個文件(在你的第一個文件中,你也有範圍,但不是完整的文件);範圍界定將改變你的變量的範圍,並且不會再有衝突。

(function($) { // Scoped 
var win = $(window); 
var allMods = $(".var"); 

// Already visible modules 
allMods.each(function(i, el) { 
    var el = $(el); 
    if (el.visible(true)) { 
     el.addClass("already-visible"); 
    } 
}); 

win.scroll(function(event) { 

    allMods.each(function(i, el) { 
     var el = $(el); 
     if (el.visible(true)) { 
      el.addClass("come-in-var"); 
     } 
    }); 
}); 
})(jQuery); 
+0

oh okey謝謝你的解釋:) – andre34