javascript - 当元素在 AngularJS 中可见时运行一些 DOM 操作

标签 javascript html css angularjs

是否可以在某些元素可见时优雅地运行一些代码,而在它隐藏时运行另一段代码?我想在单页应用程序中到处应用可滑动滚动区域,并且只有在元素可见时才有可能。

最佳答案

Angular 1.1.4 在框架中添加了动画支持。参见 here获取更多信息。

您可以使用 css3 转换来实现您的效果,但根据您问题中的信息我不能完全确定。或者,您可以在 Javascript 中创建将在 ng-show/ng-hide 上运行的动画:

<div data-ng-hide="hidden == true" data-ng-animate="'myAnimation'">
  ...
</div>
<div data-ng-show="hidden == false" data-ng-animate="'myAnimation'">
  ...
</div>

...

//you can inject stuff!
myModule.animation('myAnimation-show', ['$rootScope', function($rootScope) {
  return { 
    setup : function(element) {
      //this is called before the animation
    },
    start : function(element, done, memo) {
      //this is where the animation is expected to be run
    }
  };
}]);

myModule.animation('myAnimation-hide', ['$rootScope', function($rootScope) {
  return { 
    setup : function(element) {
      //this is called before the animation
    },
    start : function(element, done, memo) {
      //this is where the animation is expected to be run
    }
  };
}]);

编辑:

查看示例 here .

关于javascript - 当元素在 AngularJS 中可见时运行一些 DOM 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573129/

相关文章:

jquery - Bootstrap hover 和 click 事件在一起使用时发生冲突以同时具有菜单的 hover 和 click 事件

javascript - Angularjs中如何将字符串转换为对象

javascript - 如何使用 jQuery 将文件作为文本字符串加载到某个 var 中?

javascript - 将 div 元素添加到我的页面时,所有其他可拖动项目都会移动吗?

html - 如何获取html页面的 "Text"? (网络浏览器 - 德尔福)

css - 文本上的透明 div

javascript - 显示 ajax 调用错误消息的最佳方式?

javascript - AJAX:表单提交以发表评论

javascript - 使用相对位置和 top 属性来定位 Flexbox 的子元素(right 属性有效,但 top 无效)

javascript - 如何设置侧边栏的对齐方式