首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取backbone视图中snap.svg对象的值

获取backbone视图中snap.svg对象的值
EN

Stack Overflow用户
提问于 2014-11-05 22:03:29
回答 2查看 133关注 0票数 0

在下面的代码中,我希望在单击svg符号时创建一个like按钮的功能。我在获取sPapersPolyFillsPolyEmpty的值时遇到了麻烦。到目前为止,它们返回的是未定义的。

我需要将symbolAnim函数集成到likeunlike函数中。我该怎么做呢?

代码语言:javascript
复制
var LikeButtonView = BaseButtonView.extend({ 

    template: _.template($('#like-button-test').html()),
    sPaper: null,
    sPolyFill: null,
    sPolyEmpty: null,

    initialize: function (options) {
      BaseButtonView.prototype.initialize.apply(this, [options]); // inherit from BaseButtonView
      this.butn = $("button.heart-icon", this.$el);
      this.sPaper = Snap(heartIcon.get(0));
      this.sPolyFill = sPaper.select('.symbol-solid');
      this.sPolyEmpty = sPaper.select('.symbol-empty');
    },

    like: function () {
        console.log("Like clicked");
        if (this.butn.hasClass("isLiked")) {
            this.unlike();
        } else if (this.butn.hasClass("unliked")){
            this.butn.removeClass("unLiked");
            this.butn.addClass("isLiked");
            this.addBlur();
        }
    },

    unlike: function () {
        this.butn.removeClass('isLiked');
        this.butn.addClass("unLiked");
        this.addBlur(); 
    },

    symbolAnim: function(heartIcon, isLiked) {

        if (isLiked === false) {
            sPolyFill.animate({ transform: 't9,0' }, 300, mina.easeinout);
            sPolyEmpty.animate({ transform: 't-9,0' }, 300, mina.easeinout);
        } else {
            sPolyFill.animate({ transform: 't0,0'}, 300, mina.easeinout);
            sPolyEmpty.animate({ transform: 't0,0' }, 300, mina.easeinout);
        }
    }

});
EN

回答 2

Stack Overflow用户

发布于 2014-11-05 23:17:28

好的,我通过以this.butn为目标获得了这个对象

代码语言:javascript
复制
initialize: function (options) {
      BaseButtonView.prototype.initialize.apply(this, [options]); // inherit from BaseButtonView
      this.butn = $("button.heart-icon", this.$el);
      this.svgNode = this.butn.find("svg").get(0);
      this.sPaper = Snap(this.svgNode);
      this.sPolyFill = this.sPaper.select('.symbol-solid');
      this.sPolyEmpty = this.sPaper.select('.symbol-empty');
      console.log(this.sPaper);

现在我需要在likeunlike函数中实现symbolAnim的功能

票数 0
EN

Stack Overflow用户

发布于 2014-11-06 00:21:06

对于我的like按钮函数,它在事件中被触发为"click selector": "like",我想出了以下几点。然而,这只起作用一次。因此,当我第一次单击该按钮时,它会添加liked类并删除unliked类,这是html元素的缺省设置。然后,当再次单击它时,它将删除liked类并添加unliked类。到目前为止,它的工作与预期的一样。问题是,当我试图再次点击它,再次喜欢它时,什么也没有发生。它不再调用like函数,类仍然是unliked

导致此问题的原因是什么?我如何解决它?

代码语言:javascript
复制
like: function () {     
    if (this.butn.hasClass("liked")) {
        this.unlike();
    } else if (this.butn.hasClass("unliked")) {
        this.controller();
        console.log("Like clicked");
    }
},

controller: function () {
    this.butn.removeClass("unliked");
    this.butn.addClass("liked");
    this.sPolyFill.animate({ transform: 't9,0' }, 300, mina.easeinout);
    this.sPolyEmpty.animate({ transform: 't-9,0' }, 300, mina.easeinout);
},

unlike: function () {
    this.butn.removeClass('liked');
    this.butn.addClass("unLiked");
    this.sPolyFill.animate({ transform: 't0,0'}, 300, mina.easeinout);
    this.sPolyEmpty.animate({ transform: 't0,0' }, 300, mina.easeinout);
    console.log("Unliked"); 
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26759129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档