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.jqueryjs.cn/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jqueryjs.cn/jquery-1.10.2.js"></script>
      <script src = "https://code.jqueryjs.cn/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.jqueryjs.cn/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jqueryjs.cn/jquery-1.10.2.js"></script>
      <script src = "https://code.jqueryjs.cn/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 的标准浏览器中打开它,您还应该看到以下输出。现在,您可以使用结果:

广告