我正在尝试从封装在视图中的ScrollContainer管道鼠标点击,但不知怎的,没有发生任何事情。我刚开始玩Famo.us所以也许我错过了一些明显的东西。我可以直接从ScrollContainer中包含的表面进行管道,但我不确定是否可以这样做。有什么想法吗?
链接到runnable jsFiddle demo:
代码本身:
define('main', function (require, exports, module) {
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var Transform = require('famous/core/Transform');
var View = require('famous/core/View');
var Scrollview = require('famous/views/ScrollContainer');
var context = Engine.createContext();
var mainview = new View({
size: [500, 500]
});
var scrollview = new Scrollview({
scrollview: {direction:0, size: [undefined, 300]}
});
var surfaces = [];
scrollview.sequenceFrom(surfaces);
for (var i = 0; i < 40; i++) {
var surface = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 200],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
surface.pipe(scrollview);
surfaces.push(surface);
}
mainview._eventInput.on('click', function(){
console.log('click');
});
scrollview.pipe(mainview);
mainview.add(scrollview);
context.add(mainview);
});
答案 0 :(得分:1)
在撰写本文时,ScrollContainer没有它自己的EventHandler。它使用event handlers from the ScrollView that it contains(scrollcontainer.scrollview
)。 ScrollContainer有一个ContainerSurface,它从scrollcontainer.scrollview
的事件中订阅,并且是DOM元素,用于隐藏scrollview的溢出。
The below code in a new jsFiddle显示了设置ContainerSurface的正确方法。您不需要mainview
来管理您的活动。
您有几个选择:
surface.pipe(scrollcontainer.container);
并侦听容器中的事件scrollcontainer.container.on('click', ...
更正代码:
define('main', function (require, exports, module) {
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var Transform = require('famous/core/Transform');
var View = require('famous/core/View');
var ScrollContainer = require('famous/views/ScrollContainer');
var Scrollview = require('famous/views/Scrollview');
var Utility = require('famous/utilities/Utility');
var context = Engine.createContext();
var scrollcontainer = new ScrollContainer({
scrollview: {
direction: Utility.Direction.Y
}
});
scrollcontainer.container.setSize([500, 500]);
var surfaces = [];
for (var i = 0; i < 40; i++) {
var surface = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 200],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
surface.pipe(scrollcontainer.scrollview._eventOutput);
surfaces.push(surface);
}
scrollcontainer.scrollview.on('click', function (e) {
console.log('click ' + e.target.innerText);
});
scrollcontainer.sequenceFrom(surfaces);
context.add(scrollcontainer);
});
备选方案:
for (var i = 0; i < 40; i++) {
var surface = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 200],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
surface.pipe(scrollcontainer.container);
surfaces.push(surface);
}
scrollcontainer.container.on('click', function (e) {
console.log('click ' + e.target.innerText);
});
<强> jsFiddle to show event pipe to a View 强>
for (var i = 0; i < 40; i++) {
var surface = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 200],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
surface.pipe(scrollcontainer.container);
surfaces.push(surface);
}
var mainview = new View({size: [500, 500]});
mainview.on('click', function (e) {
console.log('click ' + e.target.innerText);
});
scrollcontainer.sequenceFrom(surfaces);
scrollcontainer.container.pipe(mainview._eventOutput);
context.add(scrollcontainer).add(mainview);