教你用ASP CSS实现网页随机背景
随机背景--当你每次进入该页面时,从已指定的图片文件夹中,随机选取一个图片作为背景显示。这里介绍的方法是用ASP CSS来实现的。
ASP源代码:
ConstIMGS_DIR="/images[/img] '设定图片文件夹的地址,随机显示该文件夹内任一张图片
DimobjFSO,objFolderObject,objFileCollection,objFile
DimintFileNumberToUse,intFileLooper
DimobjImageFileToUse
DimstrImageSrcText
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
SetobjFolderObject=objFSO.GetFolder(Server.MapPath(IMGS_DIR))
SetobjFSO=Nothing
SetobjFileCollection=objFolderObject.Files
SetobjFolderObject=Nothing
Randomize()
intFileNumberToUse=Int(objFileCollection.Count*Rnd) 1
intFileLooper=1
ForEachobjFileinobjFileCollection
IfintFileLooper=intFileNumberToUseThen
SetobjImageFileToUse=objFile
ExitFor
EndIf
intFileLooper=intFileLooper 1
Next
SetobjFileCollection=Nothing
strImageSrcText=IMGS_DIR&objImageFileToUse.Name
SetobjImageFileToUse=Nothing
CSS源代码:
#pic{
width:400px;
height:300px;
background:url()no-repeat;
margin:2emauto;
}
上面代码要加在head区,不能放在外部CSS文件里。