jQuery:FadeIn/To 和 SlideDown 新元素

发布于 2025-01-05 01:57:15 字数 461 浏览 1 评论 0原文

这是我用来将新行推送到容器的一行代码:

this.$el.append(new ItemView(item).render().el);

其中 itemBackbone.js 模型,render() 创建和/或修改对象,el 是 html 元素。 (在渲染完成之前,该对象永远不会显示)

我如何保留** new ItemView(item).render() **并将其存储在变量中,然后淡入淡出并将其滑入(我的容器的底部)?

编辑

请记住,this.$el 是容器元素。

Here is the one line of code I use to push a new row to my container:

this.$el.append(new ItemView(item).render().el);

Where item is a Backbone.js model, render() creates and/or modifies the object and el is the html element. (The object is never displayed until rendering is complete)

How can I keep** new ItemView(item).render() **and store it in a variable, then fade and slide it into (the bottom) of my container?

Edit

Please keep in mind that this.$el is the container element.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奢欲 2025-01-12 01:57:15
var rendered = new ItemView(item).render();

$(rendered.el).appendTo(this.$el).hide().fadeIn().slideDown();
var rendered = new ItemView(item).render();

$(rendered.el).appendTo(this.$el).hide().fadeIn().slideDown();
反目相谮 2025-01-12 01:57:15

将属性设置为元素 style="display: none;"因此,当您附加它时,它将不可见。
然后添加 exec fadein 函数后,您的元素将可见。

我希望这会有所帮助。

Set attribute to your element style="display: none;" so when you append it it wont be visible.
Then after add exec fadein function and your element will be visible.

I hopt this helps.

夏末的微笑 2025-01-12 01:57:15

在我看来,问题在于让视图的渲染方法返回视图。

你的视图可能看起来像这样:

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html);   

          return this;          

       }  
    });

然后在某个地方你有一个方法来创建视图并将其附加到 DOM。

 var $main = $('#main'); //main area in the DOM eg: <div id="main"></div>
 $main.append(new Somveview({model: item}).render().$el);

问题是必须调用 render() 才能访问视图的元素......
想想看,当模型发生更改时,视图将再次渲染。
这意味着如果您想添加任何过渡效果(例如淡入淡出或滑动),您可能会将它们添加到视图的渲染方法中。

在视图渲染方法中添加一些效果。

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html).hide().slideDown(600);   

          return this;          

       }  
    });

这是可行的,对模型的任何更改都会导致视图渲染,我们将看到向下滑动的效果。但这只是因为视图已经附加到 DOM 中了!!!问题是,我们注意到页面加载时不会发生幻灯片效果。因此,作为补丁,我们在创建视图并将其附加到 DOM 的位置复制效果。

页面加载时效果没有发生的原因是,在我们将视图元素附加到 DOM 之前,效果已经发生了。

但为什么要重复这个效果,对我来说稍微改变一下视图是有意义的。

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html);   

          //no longer returning the views object from the render method. 

       }  
    });

创建视图并将其附加到 DOM

 var $main = $('#main'); //main area in the DOM eg: <div id="main"></div>
 var someview = new Somveview({model: item}); //create the view
 $main.append(someview.$el); // add the view's element to the DOM
 someview.render(); // Rendering after we have appended to the DOM !!

In my opinion the problem is with having the view's render method return the view.

Your view probably looks something like this:

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html);   

          return this;          

       }  
    });

Then somewhere you have a method that creates the view and attaches it to the DOM.

 var $main = $('#main'); //main area in the DOM eg: <div id="main"></div>
 $main.append(new Somveview({model: item}).render().$el);

The problem is that render() must be called to gain access to the view's element...
Think about this, when a change is made on the model the view is going to render again.
This means if you want to add any transition effects such as fading or sliding your likely going to add them in the view's render method.

Add some effects into the views render method.

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html).hide().slideDown(600);   

          return this;          

       }  
    });

This works, any change to the model will cause the view to render and we will see the slide down effect. But only because the view has already been appended to the DOM!!! THE PROBLEM, we notice that the slideDown effect does not happen on page load. So, as a patch, we duplicate the effect in the place where we are creating the view and appending it to the DOM.

The reason why the effect does not occur on page load is because the effect has already happened before we are appending the view's element to the DOM.

But why duplicate the effect, it makes sense to me to change the view a little.

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html);   

          //no longer returning the views object from the render method. 

       }  
    });

Create the view and append it to the DOM

 var $main = $('#main'); //main area in the DOM eg: <div id="main"></div>
 var someview = new Somveview({model: item}); //create the view
 $main.append(someview.$el); // add the view's element to the DOM
 someview.render(); // Rendering after we have appended to the DOM !!
梦太阳 2025-01-12 01:57:15

这似乎也有效

this.$el.append($(new ItemView(item).render().el).hide().fadeIn());

This also appears to work

this.$el.append($(new ItemView(item).render().el).hide().fadeIn());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文