在Famo.us中转换颜色(或非转换/不透明度CSS属性)的正确方式
Proper way to transition color (or non-transform/opacity CSS properties) in Famo.us?
我正试图在Famo.us中获得一个正方形来改变点击时的颜色,并进行转换。我目前正在使用CSS类:
var square = new Surface({
size: [200, 200],
content: 'Hello.',
properties: {
lineHeight: '200px',
textAlign: 'center'
}
});
square.on('click', function() {
square.addClass('active');
});
以及造型(用Stylus书写):
.famous-container
.famous-surface
background: rgba(200, 200, 200, 0.5)
transition: background 0.3s ease
&.active
background: rgba(200, 255, 200, 0.5)
感觉不对,我不能利用Snap/SpringTransition之类的东西。有更好的方法吗?
最好的方法是使用RenderController对象。
RenderController允许您通过选择的过渡隐藏和显示不同的可渲染项目。
看看这个发布在Famo.us Github账户下的例子(为您做了一些修改)。
https://github.com/Famous/examples/blob/master/src/examples/views/RenderController/example.js
var Engine = require("famous/core/Engine");
var Modifier = require("famous/core/Modifier");
var Surface = require("famous/core/Surface");
var RenderController = require("famous/views/RenderController");
var mainContext = Engine.createContext();
var renderController = new RenderController();
var surfaces = [];
var counter = 0;
surfaces.push(new Surface({
content: "Surface 1",
size: [200, 200],
properties: {
backgroundColor: "green",
lineHeight: "200px",
textAlign: 'center'
}
}));
surfaces.push(new Surface({
content: "Surface: 2",
size: [200, 200],
properties: {
backgroundColor: "red",
lineHeight: "200px",
textAlign: 'center'
}
}));
renderController.show(surfaces[0]);
Engine.on("click", function() {
var next = (counter++ + 1) % surfaces.length;
this.show(surfaces[next]);
}.bind(renderController));
mainContext.add(new Modifier({origin: [.5, .5]})).add(renderController);
修正了:在两个曲面的末尾都添加了缺失的括号。push(…)调用。
我能想到的最简单的方法是使用Surface的setProperties方法更新backgroundColor。
在曲面的渲染方法中执行此操作,该方法在每个渲染记号上都会调用。请记住返回渲染等级库(等级库id)。
不要使用字符串或十六进制值来表示颜色,而是使用接受数值的rgb()或rgba()。数值是使用get方法从可转换对象中获得的。
在"点击"事件处理程序中使用Transitionable的set方法切换数值。
在设置平滑动画效果的"可过渡"值(持续时间和缓和曲线)时,请记住传递tween选项。
这是代码:
var bgColorRed = new Transitionable(0);
var bgColorGreen = new Transitionable(255);
var bgColorBlue = new Transitionable(0);
var colorTweenTime = 500;
var clicked = false;
var square = new Surface({
size: [200, 200],
content: 'Hello.',
properties: {
lineHeight: '200px',
textAlign: 'center'
}
});
square.render = function() {
var red = Math.ceil(bgColorRed.get()),
green = Math.ceil(bgColorGreen.get()),
blue = Math.ceil(bgColorBlue.get());
this.setProperties({
backgroundColor: 'rgb(' + red + ', ' + green + ', ' + blue + ')'
});
return this.id;
};
square.on('click', function() {
if (clicked) {
bgColorRed.set(0, { duration: colorTweenTime, curve: 'easeIn' });
bgColorGreen.set(255, { duration: colorTweenTime, curve: 'easeIn' });
} else {
bgColorRed.set(255, { duration: colorTweenTime, curve: 'easeIn' });
bgColorGreen.set(0, { duration: colorTweenTime, curve: 'easeIn' });
}
clicked = !clicked;
});
jsFiddle在这里可用:http://jsfiddle.net/mcr85/6fx9jo9e/
相关文章:
- 可以转换显示属性吗?如果没有,什么'这是最好的解决方案
- 手柄'img'单击事件并插入'alt'使用jQuery将属性转换为文本框
- Javascript将数组中对象的属性转换为字符串
- 如何在不使用css转换的情况下将jQuery animate与css3属性相结合
- 如何将CSS背景位置属性从百分比转换为像素
- 如何将返回一串数字的 JSON 属性转换为日期
- 使用多个 css 类将 css 转换属性应用于 DOM 对象时遇到问题
- 使用 JavaScript 更新转换属性中的 rotate() 属性
- 如何使用更多次 css 转换属性
- Babel React 转换:属性值预期的字符串类型,但得到空
- jQuery 如何将 CSS 转换属性设置为任何对象
- 使用 jQuery 2.1+ 更改 css 会忽略转换属性
- 使用javascript解析SVG转换属性
- mozilla css转换属性dosn'不起作用
- CSS中转换属性的动态值
- 意外值翻译解析转换属性
- Css3过渡+转换属性在早期版本的IE中不工作
- -translate的webkit转换属性
- 用于CSS3动画和转换属性的Javascript代码
- XML3D css转换vs自定义转换属性