jQuery之換美女照片(圖片僅供欣賞)

隨機轉換圖片

*   分析:
    *         一、給開始按鈕綁定點擊事件
    *               1.1定義循環計時器
    *               1.2切換小相框的src屬性
    *                   定義數組:存放圖片資源路徑
    *                   生成隨機數,獲得圖片索引
    *         二、給結束按鈕綁定點擊事件
    *            1.1中止定時器
    *            1.2給大相框設置src屬性
     */
    //定義數組:存放圖片資源路徑
複製代碼
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>jquery案例之抽獎</title>
    <script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
    <script>
        var imgs = [
            "../img/man00.jpg",
            "../img/man01.jpg",
            "../img/man02.jpg",
            "../img/man03.jpg",
            "../img/man04.jpg",
            "../img/man05.jpg",
            "../img/man06.jpg"
        ];
        var startId; //定義定時器id
        var index;//定義隨機角標
        // 入口函數
        $(function () {
            //處理按鈕是否可使用的效果
            $("#startID").prop("disabled",false);
            $("#stopID").prop("disabled",true);
            // 一、給開始按鈕綁定點擊事件
            $("#startID").click(function () {
                //處理按鈕是否可使用的效果
                $("#startID").prop("disabled",true);
                $("#stopID").prop("disabled",false);
               // 1.1定義循環計時器,20毫秒執行一次
                startId = setInterval(function () {
                //1.2生成隨機數,獲得圖片索引 0-6
                    index = Math.floor(Math.random()*7);//0-0.999  0-6.999  向下取整  0-6
                    //1.3定義小相框的src屬性
                    $("#img1ID").prop("src",imgs[index]);
                },20);
            });
            // 二、給結束按鈕綁定點擊事件
            $("#stopID").click(function () {
                //處理按鈕是否可使用的效果
                $("#startID").prop("disabled",false);
                $("#stopID").prop("disabled",true);
                //1.1中止定時器
                clearInterval(startId);
                //1.2給大相框設置src屬性
                $("#img2ID").prop("src",imgs[index])
            });
        });

    </script>
</head>
<body>

<!-- 小像框 -->
<div style="border-style:dotted;width:160px;height:100px">
    <img id="img1ID" src="../img/man00.jpg" style="width:160px;height:100px"/>
</div>

<!-- 大像框 -->
<div
        style="border-style:double;width:800px;height:500px;position:absolute;left:500px;top:10px">
    <img id="img2ID" src="../img/man00.jpg" width="800px" height="500px"/>
</div>

<!-- 開始按鈕 -->
<input
        id="startID"
        type="button"
        value="點擊開始"
        style="width:150px;height:150px;font-size:22px"
        onclick="imgStart()">

<!-- 中止按鈕 -->
<input
        id="stopID"
        type="button"
        value="點擊中止"
        style="width:150px;height:150px;font-size:22px"
        onclick="imgStop()">


<script language='javascript' type='text/javascript'>
    //準備一個一維數組,裝用戶的像片路徑
    var imgs = [
        "../img/man00.jpg",
        "../img/man01.jpg",
        "../img/man02.jpg",
        "../img/man03.jpg",
        "../img/man04.jpg",
        "../img/man05.jpg",
        "../img/man06.jpg"
    ];

</script>
</body>
</html>
複製代碼

寫在最後 推薦本身的GitHub地址: github.com/Lmobjectjavascript

歡迎star
複製代碼
相關文章
相關標籤/搜索