JqueryUI - show() 显示方法

本章将讨论 show() 方法,这是用于管理 jQueryUI 视觉效果的方法之一。 show() 方法使用指示的效果显示项目。

show() 方法使用指定的效果切换包装元素的可见性。

语法

show() 方法的语法如下 −

.show( effect [, options ] [, duration ] [, complete ] )
序号 参数 & 说明
1

effect

这是一个字符串,指示用于过渡的效果。这是一个字符串,表示调整元素可见性时要使用的效果。 效果如下表所示。

2

options

这是 Object 类型,表示特定于效果的设置和 缓动。 此外,每个效果都有自己的一组选项,可以在 jQueryUI 效果 表中描述的多个效果中共同指定。

3

duration

这是 Number 或 String 类型,确定动画将运行多长时间。 它的默认值为 400

4

complete

这是当每个元素的效果完成时为每个元素调用的回调方法。


jQueryUI 效果

下表描述了可以与 effects() 方法一起使用的各种效果 −

序号 效果 & 说明
1

blind

以百叶窗的方式显示或隐藏元素:通过向下或向上移动底部边缘,或向右或向左移动右边缘,具体取决于指定的方向模式

2

bounce

使元素看起来在垂直或水平方向上弹跳,可选择显示或隐藏元素。

3

clip

通过将元素的相对边界移动到一起直到它们在中间相遇来显示或隐藏元素,反之亦然。

4

drop

通过使元素看起来像是落在页面上或落在页面上来显示或隐藏元素。

5

explode

通过将元素拆分为沿径向移动的多个部分来显示或隐藏元素,就像内爆到页面或从页面爆炸一样。

6

fade

通过调整不透明度来显示或隐藏元素。 这与核心淡入淡出效果相同,但没有选项。

7

fold

通过向内或向外调整相对的边框来显示或隐藏元素,然后对另一组边框执行相同操作。

8

highlight

通过在显示或隐藏元素时立即更改其背景颜色来引起对元素的注意。

9

puff

在调整不透明度的同时扩展或收缩元素。

10

pulsate

在确保元素按指定显示或隐藏之前,调整元素的不透明度。

11

scale

按指定百分比扩展或收缩元素。

12

shake

前后摇动元素,垂直或水平。

13

size

将元素的大小调整为指定的宽度和高度。 除了如何指定目标大小外,与缩放类似。

14

slide

移动元素,使其看起来像是滑入或滑出页面。

15

transfer

动画瞬态轮廓元素,使该元素看起来像是转移到另一个元素。 outline 元素的外观必须通过 ui-effects-transfer 类或指定为选项的类的 CSS 规则定义。


示例

下面的例子演示了 show() 方法的使用。

显示抖动效果

下面的例子演示了 show() 方法和 shake 效果。

<!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>

让我们将以上代码保存在一个 HTML 文件 showexample.htm 中,并在支持 javascript 的标准浏览器中打开它,您应该会看到以下输出。 现在,您可以查看结果 −

单击Add Class添加类和Remove Class 删除类按钮以查看类对框的影响。

显示 blind 效果

以下示例演示了如何使用具有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>

让我们将以上代码保存在一个 HTML 文件 showexample.htm 中,并在支持 javascript 的标准浏览器中打开它,您应该会看到以下输出。 现在,您可以查看结果 −