avalon icon indicating copy to clipboard operation
avalon copied to clipboard

avalon动画

Open RubyLouvre opened this issue 11 years ago • 25 comments

avalon  动画指令, 是对现有JS动画库或CSS3动画的高级封装。avalon核心库本身不是提供任何动画功能。但凭借巧妙的设计,它能很好实现我们要的一切效果,甚至比jQuery实现的更简单更炫目!

avalon 的动画指令 ms-effect="effectName",当effectName里面不包含{{}}时,属性值 就是我们要的特效名;否则,就会跑进内部的解析器,将{{}}里面的变量分析出来,与其他部分结合成我们要的特效名。

这个特效名,其实是一组动画的名字,包括进入动画(entter),离开动画(leave)

动画的实现可以通过CSS3或JS钩子函数 调用jQuery等库实现。

如果你在动画开始时或结束后执行某些操作 ,那么,这就完成了。

 <script src="avalon.js"></script>
 <link rel="stylesheet" href="animate.css" type="text/css">
        <script>
            var vm = avalon.define({
                $id: "test",
                toggle: true,
                click: function () {
                    vm.toggle = !vm.toggle
                }
            })
        </script>
        <style>
            .aaa{
                width:200px;
                height: 40px;
                background:#D2E0E6;
            }
        </style>
    <div ms-controller="test">
        <div class="aaa animated"  ms-effect='toggle'  ms-visible="toggle">xxxx</div>
        <button type="button" ms-click="click">btn</button>
  </div>

 有关动画的调用就是 ms-effect='toggle',与添加一个带动画效果的类名,它是来自animated.css库。

如果需要回调,我们就需要用到avalon.effect方法来注册该特效要的回调或类名了。。。

RubyLouvre avatar Jul 06 '15 09:07 RubyLouvre

支持、很好、很小、很强单!

abbleto avatar Jul 06 '15 13:07 abbleto

支持,avalon想要大发展少不了后端兼前端程序员的支持。我正在给小伙伴门推荐avalon。谁有学校资源,给在校学生也推荐推荐。 在去哪度假部,我就使用avalon开发一个了报表工具,开发效率贼高。

Lihuanghe avatar Jul 09 '15 01:07 Lihuanghe

赞

xiongbuer-ai avatar Jul 09 '15 07:07 xiongbuer-ai

顶一下

MaxMooc avatar Jul 09 '15 07:07 MaxMooc

顶一个

PeakTai avatar Jul 09 '15 08:07 PeakTai

顶起来

ascoders avatar Jul 09 '15 08:07 ascoders

顶一下

jailecoeu avatar Jul 09 '15 08:07 jailecoeu

赞 !!!

lmh2072005 avatar Jul 09 '15 09:07 lmh2072005

回复可见?

RockNHawk avatar Jul 12 '15 01:07 RockNHawk

我逢人便推荐 avalon,我们公司全面使用 avalon

RockNHawk avatar Jul 12 '15 01:07 RockNHawk

image

RubyLouvre avatar Jul 16 '15 16:07 RubyLouvre

说说avalon未来的发展思路?

andyforever avatar Jul 17 '15 02:07 andyforever

transtion动画 首先元素必须添加一个带transition-duration的类名,或者在内联style指定transition-duration (这个可能有厂商前缀,如-webkit,-o-) 然后定义一组类名, 通常包括两个,一个表示进入时触发的动画,另一个是表示离开时触发的动画 如果是repeat可以支持更多动画效果 如果你的动画没有在avalon.effects上注册,这两个类名,默认是在动画名后加上"-enter","-leave" 如你的动画名是flip ,那么框架在进入时添加flip-enter类名, 离开时添加flip-leave 如果你要注册动画,其代码如下:

avalon.effect("bounce", {
    enterClass: "bounce-in",
    leaveClass: "bounce-out"
})

这样在运行动画时,框架为你元素添加上的是.bounce-in, .bounce-out,而不是.bounce-enter, .bounce-leave 如果你想随机执行各种动画效果, 这两个配置项可以改成函数

var ani = ["rotate-in", "flip", "zoom-in", "roll", "speed", "elastic"]
var lastIndex = NaN
function getRandomAni() {
    while (true) {
        var index = ~~Math.random() * 6
        if (index != lastIndex) {
            lastIndex = index
            return ani[index]
        }
    }
}
avalon.effect("bounce", {
    enterClass: getRandomAni,
    leaveClass: getRandomAni
})

animation动画 首先元素必须添加一个带animation-duration的类名,或者在内联style指定animation-duration 其他流程一样

RubyLouvre avatar Aug 06 '15 13:08 RubyLouvre

CSS3动画演示效果

<!DOCTYPE html>
<html>
    <head>
        <title>CSS3动画演示效果</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="avalon.js"></script>
        <script src="jquery.js"></script>
        <script>
            avalon.effect("expand", {

                leave: function (elem, done) {
                    $(elem).hide(300, done)//提供动画效果,如果在CSS中指定了具有动画效果的同名的类名,则不会执行这个
                },
                enter: function (elem, done) {
                    $(elem).show(300, done)//提供动画效果,如果在CSS中指定了具有动画效果的同名的类名,则不会执行这个
                },
                beforeLeave: function () {//在动画前做一些事
                    console.log("beforeLeave")
                },
                afterLeave: function () {//在动画后做一些事
                    console.log("afterLeave")
                },
                cancelLeave: function () {
                }
            })
            var array = []
            var vm = avalon.define({
                $id: "test",
                hide: function () {
                    avalon.effect.remove(this, this.parentNode, function (el) {
                        array.push(el)
                        console.log("已经被移除了", el)
                    })
                },
                add: function () {
                    var el = array.shift()
                    if (el) {
                        console.log(el && el.className)
                        avalon.effect.append(el, document.body, function (el) {
                            console.log("添加", el)
                        })
                    }
                }
            })
        </script>
        <style>
            .expand {
                transition: all .3s ease;
                height: 30px;
                padding: 10px;
                background-color: #eee;
                overflow: hidden;
            }
            .expand-enter,
            .expand-leave {
                height: 0;
                padding: 0 10px;
                opacity: 0;
            }
            .bounce{
                height: 30px;
                padding: 10px;
                background-color: #eee;
                -webkit-animation-duration: .5s;
                -webkit-animation-duration: .5s;
            }
            .bounce-enter {
                animation-name:  bounce-in;
                -webkit-animation-name:  bounce-in;
            }
            .bounce-leave {
                animation-name:  bounce-out;
                -webkit-animation-name:  bounce-out;
            }
            @keyframes bounce-in {
                0% {
                    opacity: 0;
                    height: 0;
                    padding: 0 10px;
                }
                100% {
                    opacity: 1;
                    height: 30px;
                    padding: 10px;
                }
            }
            @keyframes bounce-out {
                0% {
                    opacity: 1;
                    height: 30px;
                    padding: 10px;
                }
                100% {
                    opacity: 0;
                    height: 0;
                    padding: 0 10px;
                }
            }
            @-webkit-keyframes bounce-in {
                0% {
                    opacity: 0;
                    height: 0;
                    padding: 0 10px;
                }
                100% {
                    opacity: 1;
                    height: 30px;
                    padding: 10px;
                }
            }
            @-webkit-keyframes bounce-out {
                0% {
                    opacity: 1;
                    height: 30px;
                    padding: 10px;
                }
                100% {
                    opacity: 0;
                    height: 0;
                    padding: 0 10px;
                }
            }
        </style>
    </head>
    <body ms-controller="test">
        <h1 >CSS3动画演示效果</h1>
        <div ms-click="hide" class="expand" ms-effect="expand">点我执行动画</div>
        <div ms-click="hide"  class="bounce" ms-effect="bounce">点我执行动画</div>
        <button ms-click="add" type="button">添加元素</button>
    </body>
</html>

RubyLouvre avatar Aug 06 '15 13:08 RubyLouvre

javascript动画演示

<!DOCTYPE html>
<html>
    <head>
        <title>javascript动画演示</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="avalon.js"></script>
        <script src="jquery.js"></script>

        <script>
            avalon.effect("expand", {

                leave: function (elem, done) {
                    $(elem).hide(300, done)//提供动画效果,如果在CSS中指定了具有动画效果的同名的类名,则不会执行这个
                },
                enter: function (elem, done) {
                    $(elem).show(300, done)//提供动画效果,如果在CSS中指定了具有动画效果的同名的类名,则不会执行这个
                },
                beforeLeave: function () {//在动画前做一些事
                    console.log("beforeLeave")
                },
                afterLeave: function () {//在动画后做一些事
                    console.log("afterLeave")
                },
                cancelLeave: function () {
                }
            })
            var array = []
            var vm = avalon.define({
                $id: "test",
                hide: function () {
                    avalon.effect.remove(this, this.parentNode, function (el) {
                        array.push(el)
                        console.log("已经被移除了", el)
                    })
                },
                add: function () {
                    var el = array.shift()
                    if (el) {
                        console.log(el && el.className)
                        avalon.effect.append(el, document.body, function (el) {
                            console.log("添加", el)
                        })
                    }
                }
            })
        </script>
        <style>
            .jsani{
                height: 30px;
                padding: 10px;
                background-color: #eee;
                overflow: hidden;
            }
        </style>
    </head>
    <body ms-controller="test">
        <h1 >javascript动画演示</h1>
        <div ms-click="hide" class="jsani" ms-effect="expand">点我执行动画</div>
        <div ms-click="hide"  class="jsani" ms-effect="expand">点我执行动画</div>
        <button ms-click="add" type="button">添加元素</button>
    </body>
</html>

RubyLouvre avatar Aug 06 '15 13:08 RubyLouvre

 与著名的animate.css配合使用

<!DOCTYPE html>
<html>
    <head>
        <title> avalon.effect.remove与avalon.effect.before</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="avalon.js"></script>
        <link rel="stylesheet" href="animate.css" type="text/css">
        <script>
            avalon.effect("xxx", {
                leaveClass: "slideOutLeft",
                enterClass: "slideInLeft"

            })
            avalon.effect("yyy", {
                enterClass: "zoomInDown",
                leaveClass: "zoomOutUp"
            })
            var array = []
            var vm = avalon.define({
                $id: "test",
                hide: function () {
                    avalon.effect.remove(this, this.parentNode, function (el) {
                        array.push(el)
                        console.log("已经被移除了", el)
                    })
                },
                add: function () {
                    var el = array.shift()
                    if (el) {
                        avalon.effect.before(el, document.body, function (el) {
                            console.log("添加", el)
                        })
                    }
                }
            })
        </script>
        <style>
            .box{
                display: inline-block;
                width:200px;
                height:200px;
                background-color: #eee;
            }
        </style>
    </head>
    <body ms-controller="test">
        <h1 >avalon.effect.remove与avalon.effect.before</h1>
        <div ms-click="hide" class="animated box" ms-effect="xxx">点我执行动画</div>
        <div ms-click="hide"  class="animated box" ms-effect="yyy">点我执行动画</div>
        <button ms-click="add" type="button">添加元素</button>
    </body>
</html>

RubyLouvre avatar Aug 06 '15 13:08 RubyLouvre


<!DOCTYPE html>
<html>
    <head>
        <title>ms-if的动画演示</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="avalon.js"></script>
        <link rel="stylesheet" href="animate.css" type="text/css">
        <script>
            avalon.effect("xxx", {
                leaveClass: "slideOutLeft",
                enterClass: "slideInLeft"

            })
            avalon.effect("yyy", {
                enterClass: "zoomInDown",
                leaveClass: "zoomOutUp"
            })
            var array = []
            var vm = avalon.define({
                $id: "test",
                toggle:true,
                click: function(){
                    vm.toggle = !vm.toggle
                }
            })
        </script>
        <style>
            .box{
                display: inline-block;
                width:200px;
                height:200px;
                background-color: #eee;
            }
        </style>
    </head>
    <body ms-controller="test">
        <h1 >ms-if的动画演示</h1>
        <div ms-if="toggle" class="animated box" ms-effect="xxx">点我执行动画</div>
        <div ms-if="toggle" class="animated box" ms-effect="yyy">点我执行动画</div>
        <button ms-click="click" type="button">添加元素</button>
    </body>
</html>

RubyLouvre avatar Aug 06 '15 15:08 RubyLouvre

Good,据我们项目实际对动画的需求来看,非常需要加上触发开关控制,SPA页面上有很多 section,当我从list上点击某个item时进入 detail,那么 list section 就要执行退出动画,detail 就要执行进入动画。

目前我们自己扩展的 avalon 动画是这样用的:

ms-animate="boolExpr1 ? fadeIn : fadeOut"
ms-animate="boolExpr2 ? fadeIn(600) : fadeOut(100)"
ms-animate="boolExpr3 ? fadeIn(600) : hide"
ms-animate="boolExpr4 ? show : fadeOut"

ms-animate="boolExpr ? animate({marginTop:60}) : alert(document.title)" 
表达式里面可以写 js 代码,有了更多的可能性。
ms-animate-groupName="boolExpr ? enterEffect : leaveEffect" 
支持动画分组,如果同时触发动画,需要等前面不同分组的动画完成后才会执行,而同组的动画则是并行执行,典型的应用场景是:在 Single Page Application 里,列表页的所有动画元素通过(ms-animate-list)统一设定为 list 分组,点击列表后弹出的明细页面的所有动画元素通过(ms-animate-detail)指定为 detail 分组。

不过也碰到一些棘手问题。 就是 avalon 通知的顺序是不确定的,因此对于两个 ms-animate 正好取同一属性,但是却一取正一取反(对应结果是一进入一退出),两个 ms-animate 都会触发,但顺序却是不确定的。 正常顺序是列表页应先执行退出的动画,然后详细页再执行进入的动画。而实际是 avalon 根据元素在 html 中的排列顺序倒序通知,也就是顺序不确定,这对于列表页和详细也之间传场动画是致命的。

我的解决办法是,对于退出动画,立即执行,不等待前面的队列,算是基本解决了这个问题。但是赶紧这样的方式不够好,还得再想想,能用就好,先不纠结吧,放到后面再解决。 目前的大致思路是动画的执行顺序不能依赖 avalon 的通知顺序,动画需要有自己的执行顺序规则,如:最后显示的,因最先隐藏/显示在用户界面前面的,应最先被隐藏。

RockNHawk avatar Aug 07 '15 01:08 RockNHawk

ms-if 只有enter动画; ms-include同时有enter, leave动画; ms-visible视元素的隐藏显示情况采用enter或leave动画;

ms-repeat, ms-each,ms-with视元素的插入或移除情况,使用enter或leave动画,此外还有move动画;

此外还有stagger时间延迟配置项

RubyLouvre avatar Aug 07 '15 02:08 RubyLouvre

目前项目里动画的简易解决方案是使用avalon.state.config里的onLoad回调来实现全局state视图切换的入场动画

var view = document.querySelector('[avalonctrl=' + root.currentState + ']');
view && view.classList.add('YourAnimationName');

基本够用~ 期待更强大更优雅的方式!!!

IndexXuan avatar Aug 07 '15 02:08 IndexXuan