function pointerMove(elm){
var p = $(elm);
var offset = p.offset();
var vleft = offset.left+"px"
$(elm).animate({bottom: "+=15px"}, vleft);
$(elm).animate({bottom: "-=15px"}, vleft, function(elm){
console.log(elm);
pointerMove(elm);
});
}
ผมเขียน โคดไว้สำหรับรันภาพหลายๆ ตัวครับแต่มันไม่ยอมส่งค่า elm ในส่วนของ
call back ไม่รู้ว่าทำไม มีข้อเสนอมั้ยครับ
Complete Function
If supplied, the complete callback function is fired once the animation is complete. This can be useful for stringing different animations together in sequence. The callback is not sent any arguments, but [this] is set to the DOM element being animated. If multiple elements are animated, the callback is executed once per matched element, not once for the animation as a whole.
เปลี่ยนบรรทัดนี้
$(elm).animate({bottom: "-=15px"}, vleft, function(elm){
ไปเป็น
$(elm).animate({bottom: "-=15px"}, vleft, function(){
ตัด elm ออกไป เนื่องจาก elm มีประกาศไว้ด้านนอก anonymous function อยู่แล้ว เมื่อประกาศซ้ำอีกครั้งใน anonymous function จะทำให้ error ครับเนื่องจากขอบเขตของตัวแปรมันขัดแย้งกันครับ และ anonymous function ที่สร้างขึ้นสามารถเรียกใช้ตัวแปรที่อยู่นอกขอบเขตได้เลยครับ (มีขอบเขตการมองเห็นตัวแปร เท่ากับฟังก์ชั่น pointerMove + ตัวแปรที่ประกาศภายใน anonymous function)
$(elm).animate({bottom: "-=15px"}, vleft, function(elm){
console.log($(this));
pointerMove($(this));
});