用ASP+CSS實(shí)現(xiàn)隨機(jī)背景

字號:


    隨機(jī)背景--當(dāng)你每次進(jìn)入該頁面時,從已指定的圖片文件夾中,隨機(jī)選取一個圖片作為背景顯示。這里介紹的方法是用ASP+CSS來實(shí)現(xiàn)的。
    ASP--來自ASP101
    以下是引用片段:
    Const IMGS_DIR = "/images"
    '設(shè)定圖片文件夾的地址,隨機(jī)顯示該文件夾內(nèi)任一張圖片
    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區(qū),不能放在外部CSS文件里。