0

所以我正在尝试创建一个可重用的函数,我页面上的每个 .featured-image 都使用它。如果我不使用主干事件:并且我只是编写被注释掉的代码,它就可以工作。如何获得模仿注释代码的事件 imageOver 和 imageOut?

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?
        }

    });
4

2 回答 2

1

使用事件哈希,您可以通过event对象访问事件目标,并仍然通过访问视图实例this

  imageOver: function (event) {
      $(event.target).animation.play();
  },
于 2013-02-08T17:07:40.567 回答
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();
    }
于 2013-02-08T21:01:01.000 回答