新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > Web开发 > Asp教程 > 正文:ASP/DHTML Image Preloader

ASP/DHTML Image Preloader

新客网 XKER.COM 2003-07-11 来源: 收藏本文
Tutorials & Code Snips: Graphics & Charts: Images\\


This ASP Script recurses through a directory tree and loads images into a DHTML preloader.

First off, big thanks to Brian from Script Asylum for letting me use his DHTML site preloader. This
version will be even less work, because all you do is tell the ASP to drill down through a directory
structure looking for images, and it will place all the image names into an array, and off it goes.
The setup for this is incredibly simple. First off, open Preloader.asp, and change the following variables:
- boolRecurse: Tell the script to drill down through subdirectories within the folder you choose
(True/False)
- strVirtualRoot: The folder that contains all the images

<%
boolRecurse = True ' Recurse through subdirectories? True/False
strVirtualRoot = "../../Images" ' Directory
strRootFolder = Server.MapPath(strVirtualRoot) ' Grab the directory
intSize = 0

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strRootFolder)

strOutput = TraverseFolder(objFolder, strVirtualRoot, boolRecurse)
strOutput = mid(strOutput, 1, Len(strOutput)-2)

Set objFSO = Nothing
Set objFolder = Nothing

Function TraverseFolder(objFolder, strVirtualRoot, boolRecurse)
strOutput = ""

arrImages = Array("gif", "jpg", "png", "jpeg")

'Only process directories that do NOT start with
'an underscore.
    If Not Left(objFolder.Name, 1) = "_" then
          Dim objFile, strPath, strFileName, strFileSize, strExtension
          
          'Iterate through each file in the folder
          For Each objFile in objFolder.Files
              ' Obtain the extension of the current file
              strPath = objFile.Path
              strFileName = objFile.Name
              intFileSize = objFile.Size
              strExtension = Ucase(Right(strPath, Len(strPath) - InStrRev(strPath, ".")))
              

              ' See if file is an image
              For x = LBound(arrImages) to UBound(arrImages)
                    If strExtension = Ucase(arrImages(x)) then
                        strOutput = strOutput & "'" & strVirtualRoot & "/" & strFileName & "', "
                        intSize = intSize + intFileSize
                    End If
              Next
          Next

          If boolRecurse then
              'Recurse through the folder's subdirectories
              For Each objSubFolder in objFolder.SubFolders
                    strOutput = strOutput & TraverseFolder(objSubFolder, strVirtualRoot & "/" &
objSubFolder.Name, boolRecurse)
              Next
          End If
          TraverseFolder = strOutput
    End If
End Function
%>


Second step is to change the look and feel variables of the site preloader, in Progressbar.asp.
Also be sure to change the action variable, this is a function which will perform an action when all the
images are loaded, e.g. go to another page, pop up an alert, etc.

<!--#include file="preloader.asp"-->
// Progressbar - Version 2.5
// Author: Brian Gosselin of http://scriptasylum.com
// PUT THE NAMES OF ALL YOUR IMAGES THAT NEED TO BE "CACHED" IN THE "imagenames" ARRAY.
// DONT FORGET THE COMMA BETWEEN EACH ENTRY, OR THE TICK MARKS AROUND EACH NAME.
// WHEN ALL THE IMAGES ARE DONE LOADING, THE "imagesdone" VARIABLE IS SET TO "TRUE"

var imagenames=[<%=strOutput%>];

var yposition = 50; // POSITION OF LOAD BAR FROM TOP OF WINDOW, IN PIXELS
var loadedcolor = '#AAAAAA' ; // PROGRESS BAR COLOR
var unloadedcolor = 'lightgrey'; // BGCOLOR OF UNLOADED AREA
var barheight = 20; // HEIGHT OF PROGRESS BAR IN PIXELS (MIN 20)
var barwidth = 400; // WIDTH OF THE BAR IN PIXELS
var bordercolor = 'black'; // COLOR OF THE BORDER
var intSize = '<%=FormatNumber(intSize/1024, 0)%>'; // SIZE IN BYTES OF ALL THE IMAGES

// THE FUNCTION BELOW CONTAINS THE ACTION(S) TAKEN ONCE IMAGES ARE DONE LOADING.
// IF NO ACTION IS DESIRED, TAKE EVERYTHING OUT FROM BETWEEN THE CURLY BRACES ({})
// BUT LEAVE THE FUNCTION NAME AND CURLY BRACES IN PLACE.
// PRESENTLY, IT IS SET TO DO NOTHING, BUT CAN BE CHANGED EASILY.
// TO CAUSE A REDIRECT, INSERT THE FOLLOWING LINE IN IT:
document.location.href="http://redirect_page.html";
// Update: setCookie() used to "remember" that people have loaded the page, and bypass preloading.

var action=function()
{
setCookie();
}


Click here to test this script (Preload.html)
Download source (PreLoad.zip)

External Links
    http://scriptasylum.com
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐