如何在滚动时将jQuery fadeIn添加到Angular应用程序中
How to add jQuery fadeIn on scroll to Angular application
让我在序言中说,我意识到这是一种可怕的做法。然而,在AngularJS项目中,是否可以使用jQuery在滚动中淡入图像
我工作的最后期限很紧,需要让这个Angular项目看起来更好。我现在没有时间用Angular的方式来做这件事,所以快速和肮脏就足够了。
我现在的位置:
我有一个控制器,LandingCtrl
angular.module('appName.controllers', [])
.controller('LandingCtrl', ['$scope', function($scope) {
}]);
我有一个视图,里面有一堆div,我想在用户到达它们时淡出。
单独使用jQuery,它将非常直接,如下所示:http://jsfiddle.net/tcloninger/e5qaD/
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').each( function(i){
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object ){
$(this).animate({'opacity':'1'},500);
}
});
});
});
然而,当我试图在我的控制器中使用作用域为所讨论视图的jQuery函数时,我会遇到各种各样的问题。
有什么方法可以正确地做到这一点吗?
提前谢谢。
如果你要使用jquery,你可以(几乎)坚持指令中的内容:
angular
.module('app', [])
.directive('scrollContainer', scrollContainer)
function scrollContainer($window) {
return {
restrict: 'A',
scope: true,
link: function(scope, element, attrs) {
angular.element($window).bind("scroll", function() {
$('.hideme').each( function(i){
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object ){
$(this).animate({'opacity':'1'},500);
}
});
});
}
}
}
<body ng-app="app">
<div id='container' scroll-container>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
</div>
</body>
Plunker似乎坏了,所以我在代码笔上贴了一个工作示例
http://codepen.io/anon/pen/GgBzPg
相关文章:
- 如何使用Angular动态添加iframe-src
- 如何在Angular中的表的所有单元格中添加链接
- 如何在Angular单元测试中从另一个控制器的rootScope将方法添加到rootScope中
- 通过jQuery添加ng样式属性,angular不更新
- 向动态生成的DOM添加Angular自定义指令
- 如何在Angular.js中循环动态添加Fields并获取数据并将其发送到服务器
- 向Angular作用域对象添加对象数组——TypeError
- Angular js-尚未添加PUT请求到服务的URL
- 将鼠标悬停在Angular JS中的一个元素上,并将Class添加到另一个元素中
- 使用decorator添加Angular指令
- 可以't在plunkr中添加angular应用程序依赖项
- 无法在Plunker编辑器中添加Angular.js脚本
- 添加angular-leaf -指令后构建失败
- 在angular引导之后添加angular属性
- 动态添加Angular UI Datepicker
- 流星1.4.1.1在添加angular模板时抛出错误
- 用javascript给子节点添加angular属性
- 添加angular模块到mean.io包
- 在已经存在的应用中添加angular应用并传输数据
- Generator-angular-fullstack不能添加angular-chart.js