HBCMS更多的中文手册参考:

How to Make a Slide Show Using HTML+TIME Transitions

DHTML文档教程

This tutorial shows you how to create a slide show where each slide transitions into view when the user clicks a "back" or "forward" button. Some scripting is used to transition the appropriate slide into view when the user clicks a button. This is an example of integrating scripting with HTML+TIME (Timed Interactive Multimedia Extensions) transitions to make applications utilizing transitions more dynamic. Click the following Show Me button to see the slide show that we create in this tutorial.

This feature requires Microsoft?Internet Explorer 6 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

prerequisites

this article assumes you know how to use introduction to dhtml behaviors, specifically, the time2 behavior of html+time. this article does not go into great detail on how to add a behavior to your page nor does it cover how to declare a namespace and use custom tags, as required by the time2 behavior. these topics are covered in the html+time overview and spice up your web pages with html+time. it is advisable to have some understanding of html+time transitions; for an overview, see using html+time transitions. in addition, you are expected to be familiar with dynamic html(动态html,即dhtml).

a quick look at the code

to start, have a quick look at the code of the entire sample. later on, we explain this code in greater detail, but for now, it might be useful to have a preview of what is needed to make the tutorial sample.

<HTML xmlns:t = "urn:schemas-microsoft-com:time">
<HEAD>
<TITLE>Slide Show Using HTML+TIME Transitions</TITLE>
<STYLE>
.time    {behavior: url(#default#time2);}
#oDiv1
{
font-size:28pt;
font-family: arial;
font-weight:bold;
color: #000000;
background-color: #ffffcc;
border:3px solid gold;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
padding:20px
}
#oDiv2
{
font-family: arial;
font-weight:bold;
font-size: 28pt;
background-color: #e4e4e4;
border:3px solid #666666;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
padding:20px;
color:red
}
#oDiv3
{
color: white;
font-family: arial;
font-weight:bold;
font-size: 28pt;
background-color: #3366CC;
border:3px solid #666666;
padding: 20;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
}
</STYLE>
<SCRIPT>
function fnGo(direction)
{
// divCollection holds the collection of DIVS in the slide show.
var divCollection = oWrapperDiv.childNodes;
var ColLength = divCollection.length;
for(i=0; i<ColLength; i++)
{
if (divCollection(i).style.zIndex == 2)
{
if (direction == "forward" && i!=2)
{
var next = i + 1;
}
else if (direction == "back" && i!=0)
{
var next = i - 1;
}
else
break;
// Loop below sets all DIVS to low z-index.
for(j=0; j<ColLength; j++)
{
divCollection(j).style.zIndex = 0;
}
// Last DIV is set to next highest z-index.
divCollection(i).style.zIndex = 1;
// The DIV that is to transition in is set to highest z-index.
divCollection(next).style.zIndex = 2;
var transitionFilterCol = divCollection(next).childNodes;
var nextTransitionFilter = new Object();
// Create a reference to the next TRANSITIONFILTER.
nextTransitionFilter = transitionFilterCol(0);
var nextDiv = new Object();
// Create a reference to the next DIV.
nextDiv = divCollection(next);
// Begin the next transitionFilter.
nextTransitionFilter.beginElement();
// Begin the next DIV
nextDiv.beginElement();
break;
}
}
}
</SCRIPT>
<?import namespace = t urn = "urn:schemas-microsoft-com:time"
implementation = "#default#time2" />
</HEAD>
<BODY TOPMARGIN=0 LEFTMARGIN=0 BGPROPERTIES="FIXED" LINK="#000000" VLINK="#808080" ALINK="#000000">
<BLOCKQUOTE CLASS="body">
<BODY>
<DIV ID="oWrapperDiv">
<DIV ID="oDiv1" CLASS="time" STYLE="z-index:2">
<t:TRANSITIONFILTER ID="oTran1" BEGIN="indefinite" TYPE="ellipseWipe" DUR="1"/>
The First One
</DIV>
<DIV ID="oDiv2" CLASS="time" BEGIN="indefinite" STYLE="z-index:1">
<t:TRANSITIONFILTER id="oTran2" BEGIN="indefinite" TYPE="fanWipe" DUR="1"/>
The Second One
</DIV>
<DIV ID="oDiv3" CLASS="time" BEGIN="indefinite" STYLE="z-index:0">
<t:TRANSITIONFILTER ID="oTran3" BEGIN="indefinite" TYPE="pushWipe" DUR="2"/>
The Last One
</DIV>
</DIV>
<BUTTON ID="oForward" onclick="fnGo('forward');" STYLE="position:absolute; top:460;left:100;">Forward</BUTTON><br>
<BUTTON ID="oForward" onclick="fnGo('back');" STYLE="position:absolute; top:460;">Back</BUTTON><br>
</BODY>
</HTML>
This feature requires Microsoft?Internet Explorer 6 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

much of the preceding code is inside of the STYLE block. This formatting code is important for this sample to render correctly but is separated from the functional elements for clarity. The rest of the code consists of the elements that are part of the slide show and one Microsoft JScript?function that provides the slideshow functionality. This tutorial walks through each of the following steps to create the sample.

  1. Write the miscellaneous code needed for the sample to work properly.
    • Write the code that is needed to instantiate the HTML+TIME behavior.
    • Write the formatting code that will be needed by the menu in a STYLE tag.
  2. Create the div elements that are the slides in the sample.
  3. create the buttons that control when to transition from one slide to the next.
  4. create the function that facilitates the transition from one slide to the next.

setting the stage

to use html+time elements and use the time2 behavior, the following code is needed.

<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>
.
.
.
</BODY>
</HTML>

For more information about creating an XML namespace and referencing the time2 behavior, see authoring html+time.

next, we add the formatting code inside of the style tags. all of the elements in this sample derive their formatting from here. by separating formatting code from rendered elements, the functional code is simplified.

<STYLE>
.time    {behavior: url(#default#time2);}
#oDiv1
{
font-size:28pt;
font-family: arial;
font-weight:bold;
color: #000000;
background-color: #ffffcc;
border:3px solid gold;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
padding:20px
}
#oDiv2
{
font-family: arial;
font-weight:bold;
font-size: 28pt;
background-color: #e4e4e4;
border:3px solid #666666;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
padding:20px;
color:red
}
#oDiv3
{
color: white;
font-family: arial;
font-weight:bold;
font-size: 28pt;
background-color: #3366CC;
border:3px solid #666666;
padding: 20;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
}
</STYLE>

Setting Up the Slides

The slides of this sample consist of three DIV elements that are absolutely positioned over one another. Each of these DIV elements have t:transitionfilter elements applied to them to transition the div elements into view. all of these divs are contained within the owrapperdiv?b>div element.

<DIV ID="oWrapperDiv">
<DIV ID="oDiv1" CLASS="time" STYLE="z-index:2">
<t:TRANSITIONFILTER ID="oTran1" BEGIN="indefinite" TYPE="ellipseWipe" DUR="1"/>
The first one
</DIV>
<DIV ID="oDiv2" CLASS="time" BEGIN="indefinite" STYLE="z-index:1">
<t:TRANSITIONFILTER id="oTran2" BEGIN="indefinite" TYPE="fanWipe" DUR="1"/>
The Second One
</DIV>
<DIV ID="oDiv3" CLASS="time" BEGIN="indefinite" STYLE="z-index:0">
<t:TRANSITIONFILTER ID="oTran3" BEGIN="indefinite" TYPE="pushWipe" DUR="2"/>
The last one
</DIV>
</DIV>

Since the elements are activated dynamically, the begin attributes are set to indefinite. all of these divs have an assigned zindex such that the first div is rendered on top of the other two.

setting up the buttons

the buttons control which slide is displayed by activating the fngo() function.

<BUTTON ID="oForward" onclick="fnGo('forward');" STYLE="position:absolute; top:460;left:100;">Forward</BUTTON><br>
<BUTTON ID="oForward" onclick="fnGo('back');" STYLE="position:absolute; top:460;">Back</BUTTON><br>

Let's take a look at the sample so far. Right now, the layered DIV elements inside of the container DIV are rendered with the first DIV on top of the other two. The slide show control buttons are rendered also. However, clicking the buttons causes an error because the buttons call the fnGo() function that you have not created yet. This function provides the functionality of the slide show. The next section walks you through the creation of this function.

<HTML xmlns:t = "urn:schemas-microsoft-com:time">
<HEAD>
<TITLE>Sample</TITLE>
<STYLE>
.time    {behavior: url(#default#time2);}
#oDiv1
{
font-size:28pt;
font-family: arial;
font-weight:bold;
color: #000000;
background-color: #ffffcc;
border:3px solid gold;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
padding:20px
}
#oDiv2
{
font-family: arial;
font-weight:bold;
font-size: 28pt;
background-color: #e4e4e4;
border:3px solid #666666;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
padding:20px;
color:red
}
#oDiv3
{
color: white;
font-family: arial;
font-weight:bold;
font-size: 28pt;
background-color: #3366CC;
border:3px solid #666666;
padding: 20;
position:absolute;
top:20px;
left:20px;
height:300px;
width:400px;
}
</STYLE>
<?import namespace = t urn = "urn:schemas-microsoft-com:time"
implementation = "#default#time2" />
</HEAD>
<BODY TOPMARGIN=0 LEFTMARGIN=0 BGPROPERTIES="FIXED" LINK="#000000" VLINK="#808080" ALINK="#000000">
<BLOCKQUOTE CLASS="body">
<BODY>
<DIV ID="oWrapperDiv">
<DIV ID="oDiv1" CLASS="time" STYLE="z-index:2">
<t:TRANSITIONFILTER ID="oTran1" BEGIN="indefinite" TYPE="ellipseWipe" DUR="1"/>
The First One
</DIV>
<DIV ID="oDiv2" CLASS="time" BEGIN="indefinite" STYLE="z-index:1">
<t:TRANSITIONFILTER id="oTran2" BEGIN="indefinite" TYPE="fanWipe" DUR="1"/>
The Second One
</DIV>
<DIV ID="oDiv3" CLASS="time" BEGIN="indefinite" STYLE="z-index:0">
<t:TRANSITIONFILTER ID="oTran3" BEGIN="indefinite" TYPE="pushWipe" DUR="2"/>
The Last One
</DIV>
</DIV>
<BUTTON ID="oForward" onclick="fnGo('forward');" STYLE="position:absolute; top:460;left:100;">Forward</BUTTON><br>
<BUTTON ID="oForward" onclick="fnGo('back');" STYLE="position:absolute; top:460;">Back</BUTTON><br>
</BODY>
</HTML>
This feature requires Microsoft?Internet Explorer 6 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

setting up the function

this function assesses which div is currently on top and causes the next div to transition in. there is only one parameter for the function that indicates if the next div to transition in appears before or after the div currently on top.

<script>
function fnGo(direction)
{
// divCollection holds the collection of DIVS in the slide show.
var divCollection = oWrapperDiv.childNodes;
var ColLength = divCollection.length;
for(i=0; i<ColLength; i++)
{
if (divCollection(i).style.zIndex == 2)
{
if (direction == "forward" && i!=2)
{
var next = i + 1;
}
else if (direction == "back" && i!=0)
{
var next = i - 1;
}
else
break;
// Loop below sets all DIVS to low z-index.
for(j=0; j<ColLength; j++)
{
divCollection(j).style.zIndex = 0;
}
// The DIV that is to transition in is set to highest z-index.
divCollection(next).style.zIndex = 2;
// Last DIV is set to next highest z-index.
divCollection(i).style.zIndex = 1;
var transitionFilterCol = divCollection(next).childNodes;
var nextTransitionFilter = new Object();
// Create a reference to the next TRANSITIONFILTER.
nextTransitionFilter = transitionFilterCol(0);
// Begin the next transitionFilter.
nextTransitionFilter.beginElement();
var nextDiv = new Object();
// Create a reference to the next DIV.
nextDiv = divCollection(next);
// Begin the next DIV
nextDiv.beginElement();
break;
}
}
}
</script>

Stepping Through the Function

  1. First, a collection of the slide show DIV elements are created by accessing the direct child nodes of the container DIV (oWrapperDiv), the length of the collection is determined, and a loop is used to go through each member of the collection

    function fnGo(direction)
    {
    // divCollection holds the collection of DIVS in the slide show.
    var divCollection = oWrapperDiv.childNodes;
    var ColLength = divCollection.length;
    for(i=0; i<ColLength; i++)
    {
    .
    .
    .
    }
    }
    
  2. An IF statement checks to see if the collection object (divCollection(i)) is the DIV that is currently rendered on top of the other slides.

    function fnGo(direction)
    {
    // divCollection holds the collection of DIVS in the slide show.
    var divCollection = oWrapperDiv.childNodes;
    var ColLength = divCollection.length;
    for(i=0; i<ColLength; i++)
    {
    if (divCollection(i).style.zIndex == 2)
    {
    .
    .
    .
    }
    }
    }
    
  3. The direction variable passed as a parameter from the button determines if the variable next is incremented or decremented. This in turn determines if the next higher or next lower member of the collection is the next slide show DIV to transition in.

    function fnGo(direction)
    {
    // divCollection holds the collection of DIVS in the slide show.
    var divCollection = oWrapperDiv.childNodes;
    var ColLength = divCollection.length;
    for(i=0; i<ColLength; i++)
    {
    if (divCollection(i).style.zIndex == 2)
    {
    if (direction == "forward" && i!=2)
    {
    var next = i + 1;
    }
    else if (direction == "back" && i!=0)
    {
    var next = i - 1;
    }
    else
    break;
    .
    .
    .
    }
    }
    }
    
  4. All of the slide show DIVs are set to a zIndex value of 0, the DIV that is next to transition in is set to the highest zIndex (two) of the slide show DIVs, and the last DIV is assigned the second highest zIndex value (one) of the DIVs. The reason why the last DIV is assigned a zIndex of one is so that this DIV is visible as the next DIV transitions in.

    function fnGo(direction)
    {
    // divCollection holds the collection of DIVS in the slide show.
    var divCollection = oWrapperDiv.childNodes;
    var ColLength = divCollection.length;
    for(i=0; i<ColLength; i++)
    {
    if (divCollection(i).style.zIndex == 2)
    {
    if (direction == "forward" && i!=2)
    {
    var next = i + 1;
    }
    else if (direction == "back" && i!=0)
    {
    var next = i - 1;
    }
    else
    break;
    // Loop below sets all DIVS to low z-index.
    for(j=0; j<ColLength; j++)
    {
    divCollection(j).style.zIndex = 0;
    }
    // The DIV that is to transition in is set to highest z-index.
    divCollection(next).style.zIndex = 2;
    // Last DIV is set to next highest z-index.
    divCollection(i).style.zIndex = 1;
    .
    .
    .
    }
    }
    }
    
  5. Both the next DIV to transition in and the t:TRANSITIONFILTER that applies to it are begun on the time line using the beginelement method. this is when the transition starts. note that a reference to the correct t:transitionfilter element is retrieved by using the first member of the childnodes collection of that div.

    function fnGo(direction)
    {
    var divCollection = oWrapperDiv.childNodes;
    var ColLength = divCollection.length;
    for(i=0; i<ColLength; i++)
    {
    if (divCollection(i).style.zIndex == 2)
    {
    if (direction == "forward" && i!=2)
    {
    var next = i + 1;
    }
    else if (direction == "back" && i!=0)
    {
    var next = i - 1;
    }
    else
    break;
    for(j=0; j<ColLength; j++)
    {
    divCollection(j).style.zIndex = 0;
    }
    divCollection(next).style.zIndex = 2;
    divCollection(i).style.zIndex = 1;
    // Create a collection of the child nodes of the next element
    // to transition in.
    var transitionFilterCol = divCollection(next).childNodes;
    var nextTransitionFilter = new Object();
    // Create a reference to the next TRANSITIONFILTER. This
    // assumes that the TRANSITIONFILTER is the first child element.
    nextTransitionFilter = transitionFilterCol(0);
    // Begin the next transitionFilter.
    nextTransitionFilter.beginElement();
    var nextDiv = new Object();
    // Create a reference to the next DIV.
    nextDiv = divCollection(next);
    // Begin the next DIV
    nextDiv.beginElement();
    break;
    }
    }
    }
    

Related Topics