为什么自定义事件对我不起作用? (

发布于 2024-08-30 08:33:29 字数 1772 浏览 10 评论 0原文

我一直在学习 Mootools,但在触发自定义事件时遇到问题。我确信这一定是简单的事情,但我一生都看不到它。

我编写了一个简单的类来使用 Fx.Tween 微移一些列表项。它工作得很好,除了自定义事件没有被触发,无论我尝试什么。

<script type="text/javascript">
    var Pusher = new Class({
        Implements: [Events,Options],
        options: {
            elements: []
        },

        initialize: function(options){
            this.setOptions(options);
            this.attachListeners(this.options.elements);
        },

        attachListeners: function(elements){
            $$(elements).each(function(el){
                $(el).addEvent('mouseover', this.pushIn.bind(el))
                     .addEvent('mouseout', this.pushOut.bind(el));
            }, this);
        },

        pushIn: function(){
            this.fireEvent('in');
            this.set('tween', {duration: 'short'});
            this.tween('paddingLeft', '50px');
        },

        pushOut: function(){
            this.fireEvent('out');
            this.set('tween', {duration: 'short'});
            this.tween('paddingLeft', '0px');            
        }
    });

    window.addEvent('domready', function(){
        var p = new Pusher({
            elements: $$('li')
        });
        p.addEvent('in', function(){ alert('in'); });
        p.addEvent('out', function(){ alert('out'); });
    });
</script>

在 HTML 中:

<ul id="mylist">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>

我还尝试了以下操作:

window.addEvent('domready', function(){
    var p = new Pusher({
        elements: $$('li'),
        onIn: function(){ alert('in'); },
        onOut: function(){ alert('out'); }
    });
});

我做错了什么?

I've been learning Mootools, but I'm having problems firing custom events. I'm sure it must be something simple, but I can't see it for the life of me.

I wrote a simple class to nudge some list items using Fx.Tween. It works perfectly, except that the custom events aren't being triggered, no matter what I try.

<script type="text/javascript">
    var Pusher = new Class({
        Implements: [Events,Options],
        options: {
            elements: []
        },

        initialize: function(options){
            this.setOptions(options);
            this.attachListeners(this.options.elements);
        },

        attachListeners: function(elements){
            $(elements).each(function(el){
                $(el).addEvent('mouseover', this.pushIn.bind(el))
                     .addEvent('mouseout', this.pushOut.bind(el));
            }, this);
        },

        pushIn: function(){
            this.fireEvent('in');
            this.set('tween', {duration: 'short'});
            this.tween('paddingLeft', '50px');
        },

        pushOut: function(){
            this.fireEvent('out');
            this.set('tween', {duration: 'short'});
            this.tween('paddingLeft', '0px');            
        }
    });

    window.addEvent('domready', function(){
        var p = new Pusher({
            elements: $('li')
        });
        p.addEvent('in', function(){ alert('in'); });
        p.addEvent('out', function(){ alert('out'); });
    });
</script>

And in the HTML:

<ul id="mylist">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>

I've also tried the following:

window.addEvent('domready', function(){
    var p = new Pusher({
        elements: $('li'),
        onIn: function(){ alert('in'); },
        onOut: function(){ alert('out'); }
    });
});

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绳情 2024-09-06 08:33:29

问题在于这一行:

this.pushIn.bind(el)

当调用 pushIn 时,this 引用悬停的

  • 元素,而不是对象推动者。因此,调用 this.fireEvent('in') 将触发
  • 元素上的事件,而不是类对象上的事件。 DOM 事件对象包含指向触发事件的元素的目标。使用这个目标属性,我们可以获取触发事件的元素,然后对其进行动画处理。
  • 旁注:当触发 mouse(over|out) 事件时,this 已经引用了 DOM 对象,因此您不必使用 .bind(el)< 显式绑定它/代码>。

    对您的代码进行以下操作:

    1. 将事件处理程序绑定到 Pusher 的对象,
    2. 使用 event.target 为
    3. DOM 元素设置动画

    jsf 示例

    attachListeners: function(elements){
        $(elements).each(function(el){
            $(el).addEvent('mouseover', this.pushIn.bind(this))
                 .addEvent('mouseout', this.pushOut.bind(this))
        }, this);
    },
    
    pushIn: function(event) {
        this.fireEvent('in');
        var item = event.target;
        item.set('tween', {duration: 'short'});
        item.tween('paddingLeft', '50px');
    },
    
    pushOut: function(event){
        var item = event.target;
        this.fireEvent('out');
        item.set('tween', {duration: 'short'});
        item.tween('paddingLeft', '0px');            
    }
    

    The problem is with this line:

    this.pushIn.bind(el)
    

    When pushIn is called, this refers to the <li> element that was hovered, and not an object of Pusher. So calling this.fireEvent('in') will fire the event on the <li> element, instead of your class' object. The DOM event object contains the target which refers to the element that triggered the event. Using this target property, we can get the element that triggered the event, and then animate it.

    Side note: when the mouse(over|out) event is triggered, this already refers to the DOM object, so you don't have to bind it explicitly with .bind(el).

    Made the following things to your code:

    1. bound event handlers to object of Pusher
    2. use event.target to animate the <li> DOM element

    example on jsf

    attachListeners: function(elements){
        $(elements).each(function(el){
            $(el).addEvent('mouseover', this.pushIn.bind(this))
                 .addEvent('mouseout', this.pushOut.bind(this))
        }, this);
    },
    
    pushIn: function(event) {
        this.fireEvent('in');
        var item = event.target;
        item.set('tween', {duration: 'short'});
        item.tween('paddingLeft', '50px');
    },
    
    pushOut: function(event){
        var item = event.target;
        this.fireEvent('out');
        item.set('tween', {duration: 'short'});
        item.tween('paddingLeft', '0px');            
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文