破釜沉舟论坛 » WEB 开发 » ASP+CSS实现随机背景

2006-6-20 20:02 想想♀睡着了
ASP+CSS实现随机背景

  随机背景--当你每次进入该页面时,从已指定的图片文件夹中,随机选取一个图片作为背景显示。这里介绍的方法是用ASP+CSS来实现的。
ASP--来自ASP101
以下是引用片段:
Const IMGS_DIR = "/images"  
'设定图片文件夹的地址,随机显示该文件夹内任一张图片
Dim objFSO, objFolderObject, objFileCollection, objFile
Dim intFileNumberToUse, intFileLooper
Dim objImageFileToUse
Dim strImageSrcText
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolderObject = objFSO.GetFolder(Server.MapPath(IMGS_DIR))
Set objFSO = Nothing
Set objFileCollection = objFolderObject.Files
Set objFolderObject = Nothing
Randomize()
intFileNumberToUse = Int(objFileCollection.Count * Rnd) + 1
intFileLooper = 1
For Each objFile in objFileCollection
If intFileLooper = intFileNumberToUse Then
Set objImageFileToUse = objFile
Exit For
End If
intFileLooper = intFileLooper + 1
Next
Set objFileCollection = Nothing
strImageSrcText = IMGS_DIR & objImageFileToUse.Name
Set objImageFileToUse = Nothing
CSS
以下是引用片段:
#pic{
   width: 400px;
   height: 300px;
   background: url(<%= strImageSrcText %>) no-repeat;
   margin: 2em auto;
}

上面代码要加在header区,不能放在外部CSS文件里。

2006-6-21 10:07 xiaocao-x
这是楼主原创吗?

2006-6-21 19:37 想想♀睡着了
我学的是VB,对ASP不懂.只是这两星期在自学ASP.找到了这个资料,试了一下觉得没蛮好,就发上来了

页: [1]
查看完整版本: ASP+CSS实现随机背景


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.