为什么自定义事件对我不起作用? (
我一直在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于这一行:
当调用 pushIn 时,this 引用悬停的
元素,而不是对象推动者。因此,调用
this.fireEvent('in')
将触发旁注:当触发 mouse(over|out) 事件时,
this
已经引用了 DOM 对象,因此您不必使用.bind(el)< 显式绑定它/代码>。
对您的代码进行以下操作:
DOM 元素设置动画
jsf 示例
The problem is with this line:
When pushIn is called, this refers to the
<li>
element that was hovered, and not an object of Pusher. So callingthis.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:
<li>
DOM elementexample on jsf