Element.Event.Pseudos
This demo will show you how to use the provided :once
pseudo event and how to create a custom pseudo event.
Only fires once:
Click me and I'll just fire once and no more!
:alt
Pseudo
Hold the Alt
key and click a block to toggle the .active
class.
Multiple Pseudos
This demo uses the
keydown:relay(textarea):keys(enter):once
event:
Press the enter key
:pause
Pseudo
This Pseudo Event prevents the event function from firing immediately after
every keystroke. :pause
instead waits for a pause in keystrokes of
a optional amount of time (in milliseconds) to pass before firing. This is for
example very useful when you want to validate the field when the user is
ready typing, or if you do some Requests in an efficient way.
Documentation References:
#container {
border: 1px solid #999;
width: 145px;
overflow: hidden;
}
.item {
height: 40px;
width: 40px;
margin: 4px;
float: left;
background-color: #78BA91;
cursor: pointer;
}
.active {
background-color: #6B7B95;
}
#multiplePseudoText textarea {
background: #f8f8f8;
border: 1px solid #d6d6d6;
border-left-color: #e4e4e4;
border-top-color: #e4e4e4;
padding: 0.3em;
margin-top: 10px;
font: 2em sans-serif;
}
<h2>Only fires once:</h2>
<a href="#" id="clickOnce">Click me and I'll just fire once and no more!</a>
<h2><code>:alt</code> Pseudo</h2>
<p>Hold the <code>Alt</code> key and click a block to toggle the <code>.active</code> class.</p>
<div id="container" >
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<h2>Multiple Pseudos</h2>
This demo uses the <code>keydown:relay(textarea):keys(enter):once</code> event:
<div id="multiplePseudoText">
<textarea>Press the enter key</textarea>
</div>
<h2><code>:pause</code> Pseudo</h2>
<p>
This Pseudo Event prevents the event function from firing immediately after
every keystroke. <code>:pause</code> instead waits for a pause in keystrokes of
a optional amount of time (in milliseconds) to pass before firing. This is for
example very useful when you want to validate the field when the user is
ready typing, or if you do some Requests in an efficient way.
</p>
<input id="pauseEvent" type="text">
<img src="demos/Element.Event.Pseudos/spinner.gif" alt="Spinner" id="spinner">
window.addEvent('domready', function() {
// `:once` psuedo event provided by mootools-more
$('clickOnce').addEvent('click:once', function(event){
event.stop();
this.set('tween', {
transition: 'bounce:out',
duration: 'long'
}).tween('margin-left', 300);
});
// we can define our own pseudo events as well, for example to check for the alt key
DOMEvent.definePseudo('alt', function(split, fn, args){
// args[0] is the Event instance
if(args[0].alt) fn.apply(this, args);
});
// apply the psuedo event to some elements
$$('.item').addEvent('click:alt', function(){
this.toggleClass('active');
});
// use multiple pseudos
$('multiplePseudoText').addEvent('keydown:relay(textarea):keys(enter):once', function(event, el){
el.set('value', 'MooTools!!').highlight();
});
// pause pseudo event, you can define the pause, otherwise it will use the default (250)
var spinner = $('spinner').setStyle('opacity', 0).set('tween', {
link: 'chain'
});
$('pauseEvent').addEvent('keydown:pause(200)', function(){
spinner.get('tween').cancel();
spinner.fade(1).pauseFx(400).fade(0);
});
});