Dùng mootools thay đổi vị trí 2 elements
Trong bài này bạn sẽ biết cách bổ sung hàm vào mootool (1.2) và sử dụng hàm đó
Bổ sung hàm vào mootools:
Element.implement({
swap: function(other,speed) {
//Lấy vị trí
position1 = this.getCoordinates();
position2 = other.getCoordinates();
//Vị trí của element 1
this.setStyles({
top: position1.top + 'px',
left: position1.left + 'px',
position: 'absolute'
});
//Vị trí của element 2
other.setStyles({
top: position2.top + 'px',
left: position2.left + 'px',
position: 'absolute'
});
//Hiệu ứng chuyển vị trí (chuyển style)
//fx
var myFx = new Fx.Morph(this,{
duration: speed
}).start({
'top': position2.top + 'px',
'left': position2.left + 'px'
});
//fx
var myFx2 = new Fx.Morph(other,{
duration: speed
}).start({
'top': position1.top + 'px',
'left': position1.left + 'px'
});
}
});
Sử dụng:
window.addEvent('domready', function() {
//Chuyển vị trí của element khi nút 'swap' được click
$('swap').addEvent('click',function(e) {
$('element1').swap($('element2'),300);
});
});
Bạn có thể click liên tục vào ví dụ và sẽ thấy 2 elements nhập làm 1 luôn. Tại sao?

. Hiện mình đang sống tại Hà Nội. Sở thích: làm website và giúp đỡ mọi người phát triển website theo chiều hướng tốt đẹp hơn.