我正在尝试从封装在视图中的ScrollContainer中传递鼠标单击,但是不知何故。我刚刚开始使用Famo.us,所以也许我在这里遗漏了一些明显的东西。我可以直接从ScrollContainer中包含的曲面进行管道传输,但是我不确定这是否是这样做的方式。有任何想法吗 ?

链接到可运行的jsFiddle演示:

http://jsfiddle.net/nqxz596f/

和代码本身:

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);
});

最佳答案

在撰写本文时,ScrollContainer没有它自己的EventHandlers。它使用event handlers from the ScrollView that it containsscrollcontainer.scrollview)。 ScrollContainer具有一个ContainerSurface,它可以订阅scrollcontainer.scrollview事件,并且是DOM元素,用于隐藏滚动视图的溢出。

The below code in a new jsFiddle显示设置ContainerSurface的正确方法。您不需要mainview来传递事件。

您有两种选择:


将表面事件通过管道传递到scrollview事件输出(如下所示),并在scrollview中侦听事件
或将表面事件通过管道传输到容器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);

09-18 12:45