CSS3之过渡及过渡事件

CSS3之过渡及过渡事件

1.      Transition过渡

 

l  transition-property  要运动的样式  (all || [attr] ||none)

l  transition-duration 运动时间

l  transition-delay 延迟时间

l  transition-timing-function 运动形式

•      ease:(逐渐变慢)默认值

•      linear:(匀速)

•      ease-in:(加速)

•      ease-out:(减速)

•      ease-in-out:(先加速后减速)

•      cubic-bezier 贝塞尔曲线( x1,y1, x2, y2 )http://matthewlein.com/ceaser/

l  实例:幻灯片效果

 

 

例1:过渡全部属性

<style>

.box{width:100px;height:100px;background:red; transition:500ms;}

.box:hover{background:blue;width:200px;height:200px;}

</style>

</head>

<body>

<divclass="box"></div>

</body>

 

 

 

 

例2:过渡宽度

<style>

.box{width:100px;height:100px;background:red; transition:500ms width;}

.box:hover{background:blue;width:200px;height:200px;}

</style>

</head>

<body>

<divclass="box"></div>

</body>

 

 

例3:加入过渡形式

<style>

.box{width:100px;height:100px;background:red;transition:5s width cubic-bezier(0.145,1.295,0.000,1.610);}

.box:hover{width:500px;}

</style>

</head>

<body>

<divclass="box"></div>

</body>

 

 

例4:多样式过渡

<style>

.box{width:100px;height:100px;background:red;transition:1s width,2s height,3s background;}

.box:hover{width:500px;height:300px;background:blue;}

</style>

</head>

<body>

<divclass="box"></div>

</body>

 

例5:延时时间过渡(高度延时1s,背景延时3S)

<style>

.box{width:100px;height:100px;background:red;transition:1s width,2s 1s height,3s 3s background;}

.box:hover{width:500px;height:300px;background:blue;}

</style>

</head>

<body>

<divclass="box"></div>

</body>

 

 

 

过渡完成事件

Webkit内核:obj.addEventListener('webkitTransitionEnd',function(){},false);

firefox:obj.addEventListener('transitionend',function(){},false);

 

例1:

<body>

<divclass="box" id="box"></div>

<script>

varoBox=document.getElementById("box");

oBox.οnclick=function()

{

       this.style.width=this.offsetWidth+100+"px";

};

addEnd(oBox,function(){

       alert("end");

});

functionaddEnd(obj,fn)

{

       obj.addEventListener('WebkitTransitionEnd',fn,false);

       obj.addEventListener('transitionend',fn,false);

}

</script>

 

 

过渡完成后弹出end

 

 

例2:

每改变一次样式,触发一次end事件

 

<style>

.box{width:100px;height:100px;background:red;transition:1s width,2s height;}

</style>

</head>

<body>

<divclass="box" id="box"></div>

<script>

varoBox=document.getElementById("box");

oBox.οnclick=function()

{

       this.style.width=this.offsetWidth+100+"px";

       this.style.height=this.offsetHeight+100+"px";

};

addEnd(oBox,function(){

       alert("end");

});

functionaddEnd(obj,fn)

{

       obj.addEventListener('WebkitTransitionEnd',fn,false);

       obj.addEventListener('transitionend',fn,false);

}

</script>

</body>

 

弹出2end,width改变触发一次,height改变触发一次

 

 

例3:

 

小bug:

 

<style>

.box{width:100px;height:100px;background:red;transition:1s width;}

</style>

</head>

<body>

<divclass="box" id="box"></div>

<script>

varoBox=document.getElementById("box");

oBox.οnclick=function()

{

       this.style.width=this.offsetWidth+100+"px";

};

addEnd(oBox,function(){

       this.style.width=this.offsetWidth+100+"px";

});

functionaddEnd(obj,fn)

{

       obj.addEventListener('WebkitTransitionEnd',fn,false);

       obj.addEventListener('transitionend',fn,false);

}

</script>

 

点击后,宽度会不断的增加:

首先,点击后,宽度增加100,改变了样式,触发transition,接着触发transitionend,这又改变了宽度,接着再触发transition,以此循环,不断变宽

 

 

解决方法:

Transition结束后,取消事件

<body>

<divclass="box" id="box"></div>

<script>

varoBox=document.getElementById("box");

oBox.οnclick=function()

{

       this.style.width=this.offsetWidth+100+"px";

       addEnd(oBox,end);

};

function end()

{

       this.style.width=this.offsetWidth+100+"px";

       removeEnd(this,end);

}

functionaddEnd(obj,fn)

{

       obj.addEventListener('WebkitTransitionEnd',fn,false);

       obj.addEventListener('transitionend',fn,false);

}

functionremoveEnd(obj,fn)

{

       obj.removeEventListener('WebkitTransitionEnd',fn,false);

       obj.removeEventListener('transitionend',fn,false);

}

</script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值