javascript - Knockoutjs - Transitions between dynamic template switching? - Stack Overflow

KnockoutJS has been really great to use so far, but I'm new to the framework. I'm trying to c

KnockoutJS has been really great to use so far, but I'm new to the framework. I'm trying to create a tabbed sort of interface, e.g. 4 links and a mon display area. Clicking on a link takes advantage of Knockout's templating system and will switch the template. This has been working great, but I want to add some kind of animation in between the template switching.

How can I acplish this? I've read a little bit about beforeRemove/afterAdd, but this only seems to apply to observableArrays. I know KnockoutJS supports animations/custom bindings (I'm using them more straightforwardly for other elements on my page).

If my whole approach is incorrect, is there a better way to do a tabbed interface to easily get transitions?

Here is my code right now.

The HTML:

<div class="Page">
    <span data-bind="template: {name: current_page()}"></span>
</div>
<script type="text/html" id="Home">
    <!-- Home content -->
</script>
<script type="text/html" id="Tab1">
    <!-- Tab1 content -->
</script>

The Javascript (Knockout's ViewModel):

this.current_page  = ko.observable("Home")
//later on...
this.current_page("Tab1");

KnockoutJS has been really great to use so far, but I'm new to the framework. I'm trying to create a tabbed sort of interface, e.g. 4 links and a mon display area. Clicking on a link takes advantage of Knockout's templating system and will switch the template. This has been working great, but I want to add some kind of animation in between the template switching.

How can I acplish this? I've read a little bit about beforeRemove/afterAdd, but this only seems to apply to observableArrays. I know KnockoutJS supports animations/custom bindings (I'm using them more straightforwardly for other elements on my page).

If my whole approach is incorrect, is there a better way to do a tabbed interface to easily get transitions?

Here is my code right now.

The HTML:

<div class="Page">
    <span data-bind="template: {name: current_page()}"></span>
</div>
<script type="text/html" id="Home">
    <!-- Home content -->
</script>
<script type="text/html" id="Tab1">
    <!-- Tab1 content -->
</script>

The Javascript (Knockout's ViewModel):

this.current_page  = ko.observable("Home")
//later on...
this.current_page("Tab1");
Share Improve this question asked May 28, 2012 at 18:08 SharkCopSharkCop 1,1202 gold badges13 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You can use the afterRender property of the template binding:

<span data-bind="template: {name: current_page(), afterRender: animatePageChange }"></span> 

.. and then on your view model you can add whatever animation you fancy:

animatePageChange: function() { $('#content').hide(); $('#content').fadeIn(3000); }

I have put together a simple demo at http://jsfiddle/unklefolk/v3JMS/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744217251a4563611.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信