2013-02-08 82 views
0

因此,我正在尝试创建一个可重用的函数,该页面上的每个.featured-image都使用该函数。如果我不使用主干事件,而且我只写出注释掉的代码。我如何获得模拟注释代码的事件imageOver和imageOut?如何将我的鼠标当前目标传递给timelineMax函数

app.newsroomPageElementView = Backbone.View.extend({ 

events: { 
     'mouseenter .featured-image': 'imageOver', 
     'mouseleave .featured-image': 'imageOut' 
    }, 

     initialize: function() { 
     $(".featured-image").each(function(index, element){ 
     var tl = new TimelineLite({paused:true}); 
     tl.to(element, 0.2, {opacity:.9, scale:.9}) 
     .to(element, 0.2, {backgroundColor:"#004", color:"orange"}, "-=0.1") 
     element.animation = tl; 
     }) 

    // This part works if i don't use imageOver and imageOut 

    // $("li").hover(over, out); 

    // function over(){ 
    // this.animation.play(); 
    // } 

    // function out(){ 
    // this.animation.reverse(); 
    // } 

     }, 

     imageOver: function (e) { 
      // What goes here? 
     }, 
     imageOut: function (e) { 
      // What goes here? 
     } 

    }); 

回答

1
imageOver: function (event) { 
     var target = event.currentTarget; 
     // What goes here? 
     target.animation.play(); 
    }, 
    imageOut: function (event) { 
     var target = event.currentTarget; 
     // What goes here? 
     target.animation.reverse(); 
    } 
1

使用事件哈希您可以通过event对象访问事件的目标,并通过this

imageOver: function (event) { 
     $(event.target).animation.play(); 
    }, 
+0

不确定仍然可以访问视图实例。我认为我的工作虽然。 – Zuriel 2013-02-08 21:00:40

相关问题