JqueryUI - 顯示



本章將討論`show()`方法,這是用於管理 jQueryUI 視覺效果的方法之一。`show()`方法使用指示的效果顯示專案。

`show()`方法使用指定的效果切換包裝元素的可見性。

語法

`show()`方法具有以下語法:

.show( effect [, options ] [, duration ] [, complete ] )
序號 引數和描述
1

effect

這是一個字串,指示要用於轉換的效果。這是一個字串,表示在調整元素可見性時要使用的效果。效果列在下表中。

2

options

此引數的型別為物件,指示特定於效果的設定和緩動。此外,每個效果都有自己的一組選項,這些選項可以在下表`jQueryUI 效果`中描述的多個效果中指定。

3

duration

此引數的型別為數字或字串,並確定動畫執行的時長。其預設值為`400`。

4

complete

這是一個回撥方法,當此元素的效果完成時,將為每個元素呼叫。

jQueryUI 效果

下表描述了可與 effects() 方法一起使用的各種效果:

序號 效果和描述
1

blind

以百葉窗的方式顯示或隱藏元素:透過上下移動下邊緣,或根據指定的`direction`和`mode`左右移動右邊緣。

2

bounce

使元素看起來在垂直或水平方向上反彈,可以選擇顯示或隱藏元素。

3

clip

透過將元素的對邊移動到一起直到它們在中間相遇,或反過來顯示或隱藏元素。

4

drop

透過使元素看起來掉到頁面上或從頁面上掉下來來顯示或隱藏元素。

5

explode

透過將元素分割成多個以徑向方向移動的碎片來顯示或隱藏元素,就像內爆到頁面中或從頁面中爆炸一樣。

6

fade

透過調整其不透明度來顯示或隱藏元素。這與核心淡入淡出效果相同,但沒有選項。

7

fold

透過調整內外相反的邊框,然後對另一組邊框執行相同的操作來顯示或隱藏元素。

8

highlight

透過在顯示或隱藏元素時暫時更改其背景顏色來引起對元素的注意。

9

puff

在調整其不透明度的同時,在適當位置擴充套件或收縮元素。

10

pulsate

在確保元素按指定顯示或隱藏之前,開啟和關閉元素的不透明度。

11

scale

按指定百分比擴充套件或收縮元素。

12

shake

上下或左右搖晃元素。

13

size

將元素調整為指定的寬度和高度。類似於scale,但目標大小的指定方式不同。

14

slide

移動元素,使其看起來像滑到頁面上或滑出頁面。

15

transfer

動畫化一個瞬態輪廓元素,使元素看起來轉移到另一個元素。輪廓元素的外觀必須透過 CSS 規則為 ui-effects-transfer 類或指定為選項的類來定義。

示例

以下示例演示了`show()`方法的用法。

帶抖動效果的顯示

以下示例演示了帶`shake`效果的`show()`方法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI show Example</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .toggler { width: 500px; height: 200px; }
         #button { padding: .5em 1em; text-decoration: none; }
         #effect { width: 240px; height: 135px; padding: 0.4em; position: relative; }
         #effect h3 { margin: 0; padding: 0.4em; text-align: center; }
      </style>
      
      <script>
         $(function() {
            // run the currently selected effect
            function runEffect() {
               // run the effect
               $( "#effect" ).show( "shake", {times: 10,distance: 100}, 1000, callback);
            };
            
            //callback function to bring a hidden box back
            function callback() {
               setTimeout(function() {
                  $( "#effect:visible" ).removeAttr( "style" ).fadeOut();
               }, 1000 );
            };
            $( "#button" ).click(function() {
               runEffect();
               return false;
            });
            $( "#effect" ).hide();
         });
      </script>
   </head>
   
   <body>
      <div class = "toggler">
         <div id = "effect" class = "ui-widget-content ui-corner-all">
            <h3 class = "ui-widget-header ui-corner-all">Show</h3>
            <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.
            </p>
         </div>
      </div>
      <a href = "#" id = "button" class = "ui-state-default ui-corner-all">Run Effect</a>
   </body>
</html>

讓我們將以上程式碼儲存在一個名為`showexample.htm`的 HTML 檔案中,並在支援 javascript 的標準瀏覽器中開啟它,您還應該看到以下輸出。現在,您可以使用結果:

單擊`新增類`和`刪除類`按鈕以檢視類對框的影響。

帶百葉窗效果的顯示

以下示例演示了帶`blind`效果的`show()`方法的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI show Example</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .toggler { width: 500px; height: 200px; }
            #button { padding: .5em 1em; text-decoration: none; }
            #effect { width: 240px; height: 135px; padding: 0.4em; position: relative; }
            #effect h3 { margin: 0; padding: 0.4em; text-align: center; }
      </style>
      
      <script>
         $(function() {
            // run the currently selected effect
            function runEffect() {
               // run the effect
               $( "#effect" ).show( "blind", {times: 10,distance: 100}, 1000, callback);
            };
            
            //callback function to bring a hidden box back
            function callback() {
               setTimeout(function() {
                  $( "#effect:visible" ).removeAttr( "style" ).fadeOut();
               }, 1000 );
            };
            
            // set effect from select menu value
            $( "#button" ).click(function() {
               runEffect();
               return false;
            });
            $( "#effect" ).hide();
         });
      </script>
   </head>
   
   <body>
      <div class = "toggler">
         <div id = "effect" class = "ui-widget-content ui-corner-all">
            <h3 class = "ui-widget-header ui-corner-all">Show</h3>
            <p>
               Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.
            </p>
         </div>
      </div>
      <a href = "#" id = "button" class = "ui-state-default ui-corner-all">Run Effect</a>
   </body>
</html>

讓我們將以上程式碼儲存在一個名為`showexample.htm`的 HTML 檔案中,並在支援 javascript 的標準瀏覽器中開啟它,您還應該看到以下輸出。現在,您可以使用結果:

廣告
© . All rights reserved.