2014-01-13 113 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