當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JQuery resize()用法及代碼示例


jQueryresize()method 是一種內置方法,在瀏覽器窗口更改其大小時使用,resize() 方法會在元素大小更改時觸發事件,允許您根據新尺寸執行操作或應用樣式。

用法:

$(selector).resize(function)

參數:該方法接受單個參數函數這是可選的。它用於指定調用調整大小事件時要運行的函數。

示例 1:在此示例中,我們將使用resize()方法增加文本的大小。

HTML


<!DOCTYPE html>
<html>
<head>
    <title>The resize method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <!-- jQuery code to show the working of this method -->
    <script>
        x = 0;
        $(document).ready(function () {
            $(window).resize(function () {
                $("p").text(x += 1);
            });
        });
    </script>
    <style>
        div {
            width: 150px;
            height: 100px;
            padding: 20px;
            border: 2px solid green;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div>
        <!-- press "ctrl" and "+" key together
            and see the effect -->
        Welcome to GfG!
        <br>
        <p>0</p>
        times.
    </div>
</body>
</html>

輸出:

示例 2:在此示例中,我們將使用resize()方法減小文本的大小。

HTML


<!DOCTYPE html>
<html>
<head>
    <title>The resize method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <!-- jQuery code to show the working of this method -->
    <script>
        x = 0;
        $(document).ready(function () {
            $(window).resize(function () {
                $("p").text(x -= 1);
            });
        });
    </script>
    <style>
        div {
            width: 150px;
            height: 100px;
            padding: 20px;
            border: 2px solid green;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div>
        <!-- press "ctrl" and "-" key together
            and see the effect -->
        Welcome to GfG!
        <br>
        <p>0</p>
        times.
    </div>
</body>
</html>

輸出:



相關用法


注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 jQuery resize() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。