简体   繁体   English

使用Ember.js临时将CSS类添加到DOM元素

[英]Temporarily add a CSS class to a DOM element with Ember.js

Background info: 背景信息:

I'm using Ember.js. 我正在使用Ember.js。

Here's my view code: 这是我的查看代码:

App.AddView = Ember.View.extend({
  classNameBindings: ['enter:enter:leave', 'dropped:dropped:leave'],
  enter: false,
   drop: function(event) {
     event.preventDefault();
     event.stopPropagation();
     this.set('dropped', 'true');
     this.set('text', 'Thanks');
   }
 });

And my Handlebars code: 还有我的把手代码:

<script type="text/x-handlebars" id="add">
  {{#view App.AddView id="drop"}}
      {{msg}}
  {{/view}}
</script>

When I drop an item on it, it say's "Thanks" and turns blue. 当我在上面放一个项目时,它说“谢谢”并变成蓝色。 Wonderful. 精彩。

Objective: 目的:

What I'd like to do, is to keep the "Thanks" and the blue color for a couple of seconds and then fade back to the regular color and message that are displayed on pageload. 我想做的是将“谢谢”和蓝色保持几秒钟,然后淡出回到页面加载时显示的常规颜色和消息。 I couldn't find examples for doing this in Ember.js and all my attempts to just use regular jQuery ways of doing things don't seem to work. 我在Ember.js中找不到执行此操作的示例,并且所有尝试仅使用常规jQuery的工作方式的尝试似乎都行不通。

The question: 问题:

How can I make actions (applying a class to an element, changing the text of an element, etc.) timeout-bound in Ember.js? 如何在Ember.js中进行超时限制的操作(将类应用于元素,更改元素的文本等)?

Class bindings will occur the moment the binding meets the condition. 类绑定将在绑定满足条件时发生。 In order to achieve a delayed/animated effect, you'll have to either use CSS3 animations (a keyframe animation might work), or use JQuery. 为了获得延迟/动画效果,您将不得不使用CSS3动画(可能会使用关键帧动画)或使用JQuery。 There is nothing wrong with using JQuery for animations and other complex DOM manipulation. 使用JQuery进行动画和其他复杂的DOM操作没有任何问题。

From within a view you can access the scoped JQuery object with the this.$() method. 在视图中,您可以使用this.$()方法访问范围限定的JQuery对象。 You could do something like the following: 您可以执行以下操作:

App.AddView = Ember.View.extend({  
   classNames: ['defaultState'],
   drop: function(event) {
     event.preventDefault();
     event.stopPropagation();

     this.$().animate({ backgroundColor: "#7cc9ea"}, 400);
     this.set('text', 'Thanks');

     Ember.run.later(this, function(){
       this.$().animate({ backgroundColor: "#FFFFFF"}, 400);
       this.set('text', '');
     }, 500);
   }
 });

In order to animate the background color, you will need the jquery-color plugin. 为了设置背景颜色的动画,您将需要jquery-color插件。 Hope this helps! 希望这可以帮助! :) :)

Edit: If you want to animate the background or otherwise manipulate the DOM of a nested element, you can access it using a selector like you would normally; 编辑:如果要设置背景动画或以其他方式操纵嵌套元素的DOM,则可以像通常一样使用选择器来访问它; just remember it starts from the view's element as a the root node. 只要记住它是从视图的元素作为根节点开始的。 eg this.$('#myNestedButton') 例如this.$('#myNestedButton')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM