Using HTML+TIME Transitions | DHTML文档教程 |
It is now possible to create visual transitions with HTML+TIME (Timed Interactive Multimedia Extensions) easily without any knowledge of scripting. Click the Show Me button to see an example of HTML+TIME transitions.
internet explorer 6 implements the inline transitions module of the smil 2.0 transition effects modules through the time2 behavior of html+time. you only have to learn one new html+time element called t:transitionfilter and a few of the t:transitionfilter attributes to get started. this article introduces you to these necessary components, and you will be adding exciting transitions to your web pages in no time.
As seen in the Show Me earlier, many different types of media can have transitions applied to them including images, video, and other elements such as the DIV element. As long as the element has renderable content, it can have a transition applied to it. HTML+TIME media elements, such as t:media and t:IMG, are automatically available for HTML+TIME transitions to be applied. All other elements must have layout for the transition to work on the element. put simply, "having layout" means that an object has a defined height and width. some objects, like form controls and the table and td elements, have layout by default. all other objects that can use transitions gain layout by setting the height or width property, setting the position property to absolute, specifying a value for the zoom property, or setting the contenteditable property to true.
specific elements that cannot have html+time transitions applied to them include:
now that it is clear what kind of content can use transitions, the next step is to learn about how to transition this content.
The t:TRANSITIONFILTER element applies a transition to renderable content. Several associated attributes of the t:TRANSITIONFILTER specify the characteristics of the transition. The following example shows how to use several attributes of the t:TRANSITIONFILTER and their effects. Experiment with the attribute values in this example. An introduction to the t:TRANSITIONFILTER element and important attributes of the t:TRANSITIONFILTER follows in the sections ahead.
As already stated, the t:TRANSITIONFILTER is the element that applies a transition to renderable content. The t:TRANSITIONFILTER can apply to content in one of two ways.
<t:MEDIA ... > <t:TRANSITIONFILTER ... > </t:MEDIA>
<t:TRANSITIONFILTER TARGETELEMENT="oDiv" ... /> . . . <t:MEDIA ID="oDiv" ... />
The only difference between these two techniques is organization. If you use the TARGETELEMENT, you can place the t:TRANSITIONFILTER anywhere in the document and all of the t:TRANSITIONFILTER elements in the document can be grouped together. Placing the t:TRANSITIONFILTER inside of the element that is to use a transition makes it clearer what t:TRANSITIONFILTER applies to what content.
A t:TRANSITIONFILTER cannot apply to more than one element. However, children of that element can transition along with the parent.
It is important to note that in order for a child element to transition along with the parent, the child element must have the default value of static for the position property. A value of absolute or relative, on the other hand, takes the child element out of flow from the parent and it does not transition with the parent. For example, the inner DIV in the following snippet does not transition with the parent.
<DIV CLASS="TIME" STYLE="width:200px" ... > <t:TRANSITIONFILTER ... > <DIV STYLE="position:relative; top:30px">Inner DIV</DIV> </t:DIV>
Many attributes of the t:TRANSITIONFILTER enable you to specify the characteristics of the transition. The following sections introduce these attributes and show how they influence the transition.
The type attributes specifies what kind of transition will occur. the type attribute can either specify a transition value specific to html+time or a css transition. in the case of using a value specific to html+time, the type attribute specifies the family of transitions to apply. for each html+time defined type value, one or more values of the subtype attribute is available. this subtype value modifies the effect of the basic transition type specified by the type value. for example, if you specify the transition to be of type barwipe, there are two subtypes available: lefttoright and toptobottom. if lefttoright is specified, then the transition progresses from the left to the right. if toptobottom is specified, then the media element transitions from the top of the element to the bottom. the following example demonstrates this.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <TITLE>Using Different SUBTYPE Attribute Values</TITLE> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <DIV CLASS="time" ID="oDiv1" STYLE="padding:20; width:370; background-color:#ffffcc; font-family:arial; font-size:18pt; border:4px solid gold"> <t:TRANSITIONFILTER BEGIN="oDiv1.begin;" DUR="3" TYPE="barWipe" SUBTYPE = "leftToRight"/> SUBTYPE = "leftToRight" </DIV> <BR><BR> <DIV CLASS="time" ID="oDiv2" STYLE="padding:20; width:370; background-color:#ffffcc; font-family:arial; font-size:18pt; border:4px solid gold"> <t:TRANSITIONFILTER BEGIN="oDiv2.begin;" DUR="3" TYPE="barWipe" SUBTYPE = "topToBottom"/> SUBTYPE = "topToBottom" </DIV> </BODY> </HTML>
the default value for the type attribute is barwipe. generally, the subtype attribute does not need to be included, in which case, a default value is used.
To find what SUBTYPE values are available for the given TYPE values, see the TYPE reference. In addition, the example in the what is needed to apply a transition section also demonstrates the different transition type values and the possible subtype values that are specifically defined for html+time.
as already mentioned, cascading style sheets (css) transitions can also be specified as values for the type attribute in t:transitionfilter elements. the following example shows how to use a css transition to create a wheel transition.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <DIV CLASS="time" ID="oDiv1" STYLE="padding:20; width:700px; height:100px; background-color:#ffffcc; font-family:arial; font-size:14pt; border:4px solid gold"> <t:TRANSITIONFILTER BEGIN="oDiv1.begin;" DUR="3" TYPE="progid:DXImageTransform.Microsoft.Wheel(spokes=8)"/> TYPE="progid:DXImageTransform.Microsoft.Wheel(spokes=8)" </DIV> </BODY> </HTML>
in the preceding example, there are a number of html+time attributes, such as begin and dur, that control the progression and timing of the transition. these properties are examples of attributes that are common to all html+time transitions. these attributes and others will be explored in greater detail in later sections of this article. notice that the css transition has a property of spokes that is set to a value of 8. this is an example of a property that is specific to the css transition being used. for a complete list of css transitions, see visual filters and transitions reference and for an overview on css transitions in general see introduction to filters and transitions. in addition, the example shown in the What Is Needed To Apply a Transition section demonstrates many of the CSS transitions being used with HTML+TIME. The examples in this article only use HTML+TIME specific transitions. However, it is important to note that the code used in this article to control HTML+TIME specific transitions can also be used with CSS transitions.
You can apply more than one t:TRANSITIONFILTER and, therefore, more than one type of transition, to a given object. The following example shows how to apply multiple transitions simultaneously to renderable content.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <LINK REL="stylesheet" href="../common/samples/samples.css" type="text/css"> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <t:TRANSITIONFILTER BEGIN="oDiv.begin" TYPE="barWipe" DUR="5" TARGETELEMENT="oDiv"/> <t:TRANSITIONFILTER BEGIN="oDiv.begin" TYPE="fade" DUR="5" TARGETELEMENT="oDiv"/> <DIV CLASS="time" DUR="8" ID="oDiv" STYLE="width:535px;">This div has fade and barwipe types applied.</DIV> </BODY> </HTML>
The two t:TRANSITIONFILTER elements in the preceding example both apply to the same media element (in this case a DIV) at the same time. Of course, these t:TRANSITIONFILTER elements could have been started on the time line at any time relative to one another, which could cause a variety of visual effects. In addition to the TYPE attribute, the t:TRANSITIONFILTER elements each have the BEGIN and DUR attributes. These are among many important attributes that are discussed in the sections ahead.
The BEGIN element determines when to begin the active period of the t:TRANSITIONFILTER. The duration of this active period is set by the DUR property (see DUR later in this article). Because the active period of the t:TRANSITIONFILTER element is finite, it is important to coordinate the active period of the t:TRANSITIONFILTER with the active period of the element that has the t:TRANSITIONFILTER applied to it. For example, the following code snippets show a t:TRANSITIONFILTER beginning at the same time as the element that it is applied to.
Begin relative to the element.
<t:MEDIA ID="oMedia" ...> <t:TRANSITIONFILTER BEGIN="oMedia.begin" ... /> </t:MEDIA>
Begin at the same absolute time.
<t:MEDIA BEGIN="3" ... > <t:TRANSITIONFILTER BEGIN="3" ... /> </t:MEDIA>
Begin relative to an event.
<t:MEDIA BEGIN="oButton.click" ... > <t:TRANSITIONFILTER BEGIN="oButton.click" ... /> </t:MEDIA>
For a complete list of possible values for the BEGIN attribute, see the begin documentation.
if the active periods of the t:transitionfilter and the applied content do not overlap, then no transition occurs. for example, no transition would be visible in the following example.
<t:MEDIA BEGIN="O" DUR="3" ... > <t:TRANSITIONFILTER BEGIN="4" ... /> </t:MEDIA>
In the preceding example, the media element begins at zero seconds and is active for three seconds. The t:TRANSITIONFILTER that is applied to the media element does not begin until four seconds so no transition would occur.
It is common to begin a t:TRANSITIONFILTER element at the beginning or close to the end of the active period of a transition element in order to transition the media in or out. However, if the t:TRANSITIONFILTER begins in the middle of the active period of a timed element or the active periods of the t:TRANSITIONFILTER element and timed element partially overlap, a variety of generally undesirable results can occur. For more information, see mode later in this article. one way to ensure that the transition begins at the correct time is to have the parent element that is to be transitioned have a timecontainer attribute value of par. an example of this would be the following:
<t:MEDIA TIMECONTAINER="par" BEGIN="4" DUR="indefinite" ... > <t:TRANSITIONFILTER ... /> </t:MEDIA>
As you can see, the t:TRANSITIONFILTER element does not even need to include the BEGIN attribute because it is on its parent time line and starts as soon as its parent.
the dur attribute determines the duration of the t:transitionfilter element. the longer the duration of the t:transitionfilter element, the slower the transition proceeds. the default value for the dur attribute is 1s (1 second). The following example shows how to make two identical DIV elements transition in when a button is clicked. The first DIV has a t:TRANSITIONFILTER applied to it with a DUR equal to one second, while the next DIV has a t:TRANSITIONFILTER applied to it with a DUR equal to three seconds. Because of this, the first DIV transitions three times as fast as the second one.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <INPUT TYPE="button" ID="oBeginRace" VALUE="Begin Race"> <DIV BEGIN="oBeginRace.click" DUR="8" CLASS="time" ID="oDiv1" STYLE="width:600px;height:100px;background-color:gray"> First DIV <t:TRANSITIONFILTER BEGIN="oBeginRace.click" TYPE="barWipe" DUR="1"/> </DIV> <DIV BEGIN="oBeginRace.click" DUR="8" CLASS="time" ID="oDiv2" STYLE="width:600px;height:100px;background-color:gray"> Second DIV <t:TRANSITIONFILTER BEGIN="oBeginRace.click" TYPE="barWipe" DUR="3"/> </DIV> </BODY> </HTML>
The mode attribute specifies whether the transition element transitions in or out. the legal value of in indicates that the transition content becomes more visible as the transition progress increases, and out indicates that the transition content becomes less visible as the transition progress increases.
usually when the mode attribute is set to the value of in, which is the default, the transition is tied to the beginning of a content element's active period. in this way, the media element appears to transition into view. typically a t:transitionfilter with a mode value of out is applied to the end of an element's active period. this makes the media transition out of view. the following example shows a div element that transitions in, stays visible for two seconds, and then transitions out.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <DIV STYLE="width:270px" CLASS="time" DUR="8" ID="oDiv"> <t:TRANSITIONFILTER BEGIN="oDiv.begin" DUR="3" TYPE="clockWipe"/> <t:TRANSITIONFILTER BEGIN="oDiv.end-3" DUR="3" MODE="out" TYPE="fade"/> This DIV Transitions In, Stays For Two Seconds, And Then Transitions Out. </DIV> </BODY> </HTML>
as can be seen from the previous example, the mode attribute does not automatically tie the transitionfilter to the beginning or end of the media. authors can use the begin attribute on the transitionfilter to indicate the begin time for the transitionfilter.
you can apply a transition to a media element at any time during its active period. if mode is set to in and the t:transitionfilter is applied to the timed element in the middle of its active period, then the timed element disappears for a moment and transitions in. if mode is set to out, then the media element transitions out, be not visible for a moment, and then immediately reappear. If MODE is set to in and the t:TRANSITIONFILTER is applied to the timed element in the middle of its active period, then the timed element disappears for a moment and then transitions in. If MODE is set to out, then the media element transitions out, is not visible for a moment, and then immediately reappears. The following example shows the effect of transitioning out a media element in the middle of the media element active period.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <LINK REL="stylesheet" href="../common/samples/samples.css" type="text/css"> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <DIV STYLE="width:270px" CLASS="time" DUR="8" ID="oDiv"> <t:TRANSITIONFILTER BEGIN= "oDiv.begin" DUR="3" TYPE="clockWipe"/> <!-- The out TRANSITIONFILTER begins immediately --> <t:TRANSITIONFILTER BEGIN="oDiv.begin" DUR="3" MODE="out" TYPE="fade"/> This DIV transitions out in the middle of its active period. </DIV> </BODY> </HTML>
there are a number of attributes that enable control over the progress of the transition using the t:transitionfilter. these include the from, to, by, calcmode, and VALUES attributes. All of these attributes are optional. They are covered in the sections that follow.
The FROM attribute specifies the starting progress value of the transition. Legal values are real numbers in the range of 0.0-1.0. For example, to begin a transition with the destination content to make a transition in by 30%, you would specify a value for FROM of 0.3.
The TO and BY attributes specify the amount of progress through the transition at which to end progress of the transition. Both of these attributes have legal values in the range of 0.0-1.0. The by attribute specifies the relative offset value of the progress from the value of the from attribute. for example, if from has a value of 0.2 and by has a value of 0.5, then the transition starts at a progress of 20% and ends at a progress of 70%. in contrast, the to attribute specifies the end of progress directly. if the from attribute is set to a value of 0.2 and the to attribute is set to a value of 0.5, then the transition starts at a progress of 20% and ends at a progress of 50%.
the following example shows how to use the from and to attributes to begin a transition 50% through its progress and end progress at 80%.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <LINK REL="stylesheet" href="../common/samples/samples.css" type="text/css"> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <DIV STYLE="width:270px;height:200px;" CLASS="time" DUR="8" ID="oDiv"> <t:TRANSITIONFILTER BEGIN="oDiv.begin" DUR="5" TYPE="ellipseWipe" FROM="0.6" TO="0.8"/> This transition begins at 50% progress and ends at 80% because FROM is set to 0.5 and TO is set to 0.8 </DIV> </BODY> </HTML>
As the previous sample shows, once the transition finishes, the transitioning element rapidly displays. In order to lock the appearance of the element at the value specified by the TO attribute, set the fill attribute to a value of freeze inside of the t:transitionfilter element. the following example is similar to the preceding one except that the fill attribute is set inside of the t:transitionfilter element.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <LINK REL="stylesheet" href="../common/samples/samples.css" type="text/css"> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <DIV STYLE="width:270px;height:200px;" CLASS="time" DUR="8" ID="oDiv"> <t:TRANSITIONFILTER BEGIN="oDiv.begin" FILL="freeze" DUR="5" TYPE="ellipseWipe" FROM="0.6" TO="0.8"/> This transition begins at 50% progress and ends at 80% because FROM is set to 0.5 and TO is set to 0.8 </DIV> </BODY> </HTML>
the values attribute specifies a semicolon-delimited list of one or more values indicating the progress of the transition. this attribute can provide more precise control over the progress of a transition than a combination of the from, to, and by attributes and overrides those attributes if they are specified.
the transition effects of from, to, by, and values can be modified with the calcmode attribute. the calcmode attribute specifies the interpolation mode for the progress of the t:transitionfilter. there are two possible values for calcmode: discrete and linear. when the discrete value is specified, the progress of the transition jumps from one value to the next without any interpolation. this gives the transition a progress that appears to shake. the default value of linear and the transition progresses smoothly from one value to the next. the following is an example that shows two identical time elements that have the same values attribute but one has a calcmode value of linear and the other has discrete.
<HTML XMLNS:t = "urn:schemas-microsoft-com:time"> <HEAD> <STYLE> .time {behavior: url(#default#time2);} </STYLE> <?import namespace = t urn = "urn:schemas-microsoft-com:time" implementation = "#default#time2" /> </HEAD> <BODY> <t:TRANSITIONFILTER BEGIN="oImage1.begin" TYPE="barWipe" DUR="5" TARGETELEMENT="oImage1" VALUES=".1;.17;.27;.37;.47;.56;.65;.71;.82;.92;1.0" CALCMODE="discrete"/> <t:TRANSITIONFILTER BEGIN="oImage2.begin" TYPE="barWipe" DUR="5" TARGETELEMENT="oImage2" VALUES=".1;.17;.27;.37;.47;.56;.65;.71;.82;.92;1.0" CALCMODE="linear"/> <INPUT TYPE="button" ID="oStartButton" VALUE="Start Transition"> <P> <IMG SRC="img.gif" CLASS="time" begin="oStartButton.click" ID="oImage1" STYLE="width:400px;"> <P> <IMG SRC="img.gif" CLASS="time" begin="oStartButton.click" ID="oImage2" STYLE="width:400px;"> </BODY> </HTML>
the repeatcount and repeatdur attributes enable the transition to repeat itself a specified number of times. the repeatcount attribute specifies a number of times that the transition repeats. this number does not have to be a whole number, and if it is not, the transition progresses a fractional amount, as in the following example. the repeatdur attribute specifies the amount of time in which the transition repeats. therefore, the number of times the media element repeats is equal to the time specified by repeatdur divided by the value of the dur attribute of the t:transitionfilter. any remainder is expressed as a partial transition. it is important to note that neither repeatcount nor repeatdur can extend the active duration of a media element. when the duration of the media element expires, the media element disappears even if the repeatcount or repeatdur attributes specify more repetitions.
the following example shows how to use the repeatcount attribute to repeat a transition on a time element. the repeatcount attribute inside the t:transitionfilter element is set to indefinite. however, the time element that follows only repeats for the duration of its active period (in this case, seven seconds).
. . . <!-- The FOR and TO attributes used to make only outer DIV transition. --> <t:TRANSITIONFILTER REPEATCOUNT="indefinite" TYPE="ellipseWipe" DUR="3" FROM="0.28" TO="1.0" TARGETELEMENT="oDiv1"/> <IMG SRC="img.gif" CLASS="time" ID="oDiv1" DUR="7" STYLE="width:400px;"> . . .
By using the components outlined in this article, you are ready to use transitions in your Web pages. However, this is only the starting point. You can make your transitions even more interactive using scripting. The t:TRANSITIONFILTER, along with all of its associated attributes, is accessible through the Document Object Model (DOM). Combining scripting with all of the other features of HTML+TIME provides even more flexibility than the examples in this article. Of course, there are other ways to create transitions, but perhaps the greatest power of this specific technology is its simplicity and compact nature. This makes HTML+TIME transitions a good choice for simple transition applications.