使用Dojo的行为功能来识别滚动事件

时间:2013-10-06 14:05:53

标签: events dojo scroll behavior

我无法弄清楚如何将函数分配给Dojo中的滚动事件。 我已经看到这个other post关于使用dojo的连接,但我一直无法使用它,即使我可以,我的项目尽可能地使用行为,所以我真的想要得到它先工作......

对我来说令人费解的是,我在dojo的网站上看了一下,我看到滚动事件的唯一参考只是提到它们。以http://dojotoolkit.org/reference-guide/1.9/quickstart/events.html#connecting-to-a-dom-event为例,有一个可以连接到的事件列表,滚动不在它们上面。鼠标滚轮向上+向下,但这并未涵盖可能导致滚动发生的所有可能操作。

之前我使用过jQuery的scroll事件,这很简单。事实上,我在Dojo中弄清楚滚动事件时遇到了这么多麻烦。

这是我尝试过的:

var myBehavior = {
    window : {
    scroll: function(e) {
        console.log("i'm scrolling");
        }
    }
    };
behavior.add(myBehavior);
behavior.apply();

加载没有任何问题,但滚动不会给我控制台日志消息。

我也试过

dojo.connect(window,'scroll',this,function() {
        console.log("scrolling away");
    })

我几乎从链接的帖子中得到了,我也没有收到控制台日志消息。 我开始怀疑自己是否错过了一个密钥库?

我认为我需要的不仅仅是必要的: - dojo / _base / lang - dojo /查询 - dojo / _base / event - 道场/上 - 道场/行为 - 道场/窗口

是否有人建议我如何开始在Dojo中注册这些滚动事件?谢谢

2 个答案:

答案 0 :(得分:2)

我为窗口滚动事件创建了一个jsfiddle演示dojo/on的用法。

HTML:

<body>
    <div style="height:1000px;">
        Bacon ipsum dolor sit amet meatball drumstick jerky strip steak kielbasa shoulder, short ribs filet mignon prosciutto swine meatloaf ribeye tongue turkey andouille. Hamburger bacon fatback, short ribs pork loin cow chicken. Capicola venison andouille kielbasa cow. Short ribs kevin rump meatloaf jowl prosciutto leberkas. Pork belly shankle doner spare ribs, rump pork chop sausage tenderloin. Brisket swine pig, fatback beef ribs shank tri-tip flank ham venison strip steak filet mignon rump frankfurter ribeye.

Fatback turkey sausage tongue doner strip steak boudin shoulder chuck beef ribs chicken bacon brisket. Turkey biltong bacon cow short ribs fatback. Sausage brisket pork loin corned beef pork chop shank jowl capicola leberkas strip steak prosciutto beef tri-tip. Jowl frankfurter beef ribs, swine tail doner tri-tip t-bone. Jowl pork loin sausage venison salami shankle kevin strip steak boudin doner bresaola filet mignon ham drumstick ribeye. Turkey pork belly kielbasa ribeye salami, strip steak ham hock jerky shoulder tenderloin shankle spare ribs beef ribs filet mignon. Short loin fatback kevin, biltong tail shoulder ball tip.

Pork loin t-bone ground round tongue turducken tail shoulder chicken sausage pancetta hamburger, kielbasa tri-tip strip steak short ribs. Pork chop prosciutto tri-tip, jerky brisket leberkas andouille ham hock tongue shoulder fatback meatball. Pig short loin beef spare ribs, rump meatball short ribs doner kielbasa ball tip swine bacon kevin. Hamburger tail frankfurter ham hock tongue, jerky pork belly bacon shank salami prosciutto ground round doner jowl turducken. Prosciutto pork chop drumstick ham hock meatloaf. Drumstick ham hock ball tip salami cow.

Shankle hamburger sirloin sausage tongue pork chop flank, doner pig tri-tip pancetta shoulder. Venison ball tip beef biltong tail, corned beef pastrami andouille frankfurter kevin pork meatball chicken. Ground round shankle sirloin capicola kevin spare ribs ham hock fatback strip steak short ribs leberkas. Venison flank tenderloin tri-tip salami pork capicola drumstick brisket short ribs sirloin chuck boudin kielbasa cow.

Short ribs corned beef shankle meatball, tri-tip t-bone sirloin cow leberkas chicken jerky. Short loin drumstick pork loin biltong sausage prosciutto rump flank hamburger pork chop meatball capicola turkey leberkas cow. Andouille ribeye boudin ham chicken. T-bone rump meatball drumstick hamburger pork chop, flank sausage tongue andouille fatback.

Does your lorem ipsum text long for something a little meatier? Give our generator a try… it’s tasty!
    </div>
</body>

JS:

require(['dojo/on','dojo/domReady!'],function(on,domReadyPlugin){

    on(window,'scroll',function(evt){
        console.log(evt);

    });
});

每次用户在窗口中滚动时,结果都会打印到控制台。如果你继续遇到问题,请发布更多代码,以便我们可以了解它,这应该是一件简单的事情。

答案 1 :(得分:0)

使用“onscroll”代替“滚动”,请参阅this jsfiddle

dojo.connect(window,'onscroll',this,function() {
        console.log("scrolling away");
    })