css文字定时消失

html-css015

css文字定时消失,第1张

这是一款很酷的带定时消失功能的css3消息提示框插件。

HTML

html结构非常简单:用一个wrapper div作为包裹容器,里面放一个用于显示信息的p标签和一个用来做进度条的div。

Your settings have been saved successfully!

为了给包裹div设置不同的颜色,给它设置两个class:tn-box和tn-box-color-1。

CSS

.tn-box {

width: 360px

position: relative

margin: 0 auto 20px auto

padding: 25px 15px

text-align: left

border-radius: 5px

box-shadow:

0 1px 1px rgba(0,0,0,0.1),

inset 0 1px 0 rgba(255,255,255,0.6)

opacity: 0

cursor: default

display: none

}

.tn-box-color-1{

background: #ffe691

border: 1px solid #f6db7b

}

设置包裹div的初始dispaly:none,并给设置它的透明度为0。

进度条的样式如下:

.tn-progress {

width: 0

height: 4px

background: rgba(255,255,255,0.3)

position: absolute

bottom: 5px

left: 2%

border-radius: 3px

box-shadow:

inset 0 1px 1px rgba(0,0,0,0.05),

0 -1px 0 rgba(255,255,255,0.6)

}

动画开始前,进度条的宽度为0。

在demo中,使用一个checkbox来作为按钮触发消息提示框动画。

input.fire-check:checked ~ section .tn-box {

display: block

animation: fadeOut 5s linear forwards

}

input.fire-check:checked ~ section .tn-box .tn-progress {

animation: runProgress 4s linear forwards 0.5s

}

因为按钮的动作发生在消息提示框之前,所以可以使用一般通用选择器来完成按钮操作。

如果你想使用javascript来完成按钮动作,你可以像下面这样写样式:

.tn-box.tn-box-active {

display: block

animation: fadeOut 5s linear forwards

}

.tn-box.tn-box-active .tn-progress {

animation: runProgress 4s linear forwards 0.5s

}

tn-box-active是在按钮按下时被加到tn-box div中的class。

下面是消息框淡入淡出动画效果:

@keyframes fadeOut {

0% { opacity: 0}

10% { opacity: 1}

90% { opacity: 1transform: translateY(0px)}

99% { opacity: 0transform: translateY(-30px)}

100% { opacity: 0}

}

下面是进度条的动画效果:

@keyframes runProgress {

0% { width: 0%background: rgba(255,255,255,0.3)}

100% { width: 96%background: rgba(255,255,255,1)}

}

我们使它运动到进度条宽度的96%,颜色上使用RGBA。

你是指出现这个问题还是想 实现这个效果?

移动端的触摸事件有:

touchstart:触摸开始的时候触发

touchmove:手指在屏幕上滑动的时候触发

touchend:触摸结束的时候触发

而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点(用来实现多点触控):

touches:当前位于屏幕上的所有手指的列表。

targetTouches:位于当前DOM元素上手指的列表。

changedTouches:涉及当前事件手指的列表。

之类的 可以去w3c查一查

们在移动端一般使用zepto框架,与其说zepto是jquery的轻量级替代版,不如说是html5替代版

我们在js中会用到animate方法执行动画,这个家伙可是真资格的动画,完全是css一点点变化的!

而zepto则不然,使用的是HTML5/CSS3的方案,而CSS相关是不保存元素状态值的,也没办法保存,所以停止动画就成了一大问题

我们今天就一起来讨论下相关停止动画的方案,反正没有什么好的......

CSS3动画原理

在现有浏览器中,一般有两种模式(我只知道两种):

一种是js动画,他是动态改写元素的style实现动画,所以任意时间想停止动画都是没问题的,因为我们可以获得各个阶段的状态值

另一种就是CSS3动画了,至于CSS3动画的原理,我不知道但是可以说一点的就是——见代码

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2 <html xmlns="http://www.w3.org/1999/xhtml">

3 <head>

4 <title></title>

5 <script id="Script1" type="text/javascript" class="library" src="/js/sandbox/other/zepto.min.js"></script>

6 </head>

7 <body>

8 <div id="Div1" style="background-color: Orangewidth: 100pxheight: 100pxposition: absolute

9 left: 0border: 1px solid black">

10 </div>

11 </body>

12 <script src="../zepto.js" type="text/javascript"></script>

13 <script type="text/javascript">

14 var d = $('#d')

15 d.animate({

16 left: '100px'

17 }, 10000)

18

19 setTimeout(function () {

20 d.html('left: ' + d.css('left'))

21 }, 1)

22

23 </script>

24 </html>

http://sandbox.runjs.cn/show/xziwuir2

zepto的animate事实上马上就改变了style的值,所以我们在里面看到了left为100px,虽然他正在运动

而他动画的实现事实上使用的是CSS3的transition动画属性,我们这里来看看zepto的源码:

View Code

最后实际上是执行anim实现我们的动画,大家注意看这里

$(this).css(cssReset)

this.css(cssValues)

他事实上搞了个先设置动画属性,再给style属性给元素,所以会产生动画

到此,zepto实现动画原理我们大概知道了,现在问题就是如何停止他了,所以我们继续往下看

如何停止动画

我们先看看这个东西:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2 <html xmlns="http://www.w3.org/1999/xhtml">

3 <head>

4 <title></title>

5 <script id="Script2" type="text/javascript" class="library" src="/js/sandbox/other/zepto.min.js"></script>

6 </head>

7 <body>

8 <div id="Div2" style="background-color: Orangewidth: 100pxheight: 100pxposition: absolute

9 left: 0border: 1px solid black">

10 </div>

11 </body>

12 <script src="../zepto.js" type="text/javascript"></script>

13 <script type="text/javascript">

14 var d = $('#d')

15 d.animate({

16 left: '100px'

17 }, 10000)

18

19 setInterval(function () {

20 d.html('left: ' + d.css('left') + ' _ offsetLeft: ' + d[0].offsetLeft)

21 }, 1)

22

23 </script>

24 </html>

http://sandbox.runjs.cn/show/gdqezvdo

其中虽然left一开始就变了,我们惊奇的发现,offset这个家伙居然保存了我们的状态!!!

我和我的小伙伴都惊呆了,因为我之前一直以为什么状态都不能获得,于是我们为他加上mousedown事件,各位运动时候点击试试

我们这里这样干了下:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2 <html xmlns="http://www.w3.org/1999/xhtml">

3 <head>

4 <title></title>

5 <script id="Script3" type="text/javascript" class="library" src="/js/sandbox/other/zepto.min.js"></script>

6 </head>

7 <body>

8 <div id="Div3" style="background-color: Orangewidth: 100pxheight: 100pxposition: absolute

9 left: 0border: 1px solid black">

10 </div>

11 </body>

12 <script src="../zepto.js" type="text/javascript"></script>

13 <script type="text/javascript">

14 var d = $('#d')

15 d.animate({

16 left: '100px'

17 }, 10000)

18

19 setInterval(function () {

20 d.html('left: ' + d.css('left') + ' _ offsetLeft: ' + d[0].offsetLeft)

21 }, 1)

22

23 d.mousedown(function (e) {

24 console.log(d)

25 d.css('transition', 'left 0s linear')

26 d.css('left', d[0].offsetLeft + 'px')

27 })

28

29 </script>

30 </html>

于是我们发现,动画停止了,亲!他真的停止了!!!

PS:因为项目过程中,我这里要模仿类似iscroll的滚动效果,所以使用的最多的就是top或者translate3d(0, 0, 0)这种东西

结语

本来这里还想深入一点研究下的,但是现在时间有点来不及,事情有点多,暂时到这里了吧,具体的demo争取周末搞出来