切换(Toggle)


jQuery UI 实例 - 切换(Toggle)

使用自定义效果来显示或隐藏匹配的元素。

如需了解更多有关 .toggle() 方法的细节,请查看 API 文档 .toggle()。

.toggle() 演示

点击按钮预览特效。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 特效 - .toggle() 演示</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   .toggler {
  12.     width: 500px;
  13.     height: 200px;
  14.   }
  15.   #button {
  16.     padding: .5em 1em;
  17.     text-decoration: none;
  18.   }
  19.   #effect {
  20.     position: relative;
  21.     width: 240px;
  22.     height: 135px;
  23.     padding: 0.4em;
  24.   }
  25.   #effect h3 {
  26.     margin: 0;
  27.     padding: 0.4em;
  28.     text-align: center;
  29.   }
  30.   </style>
  31.   <script>
  32.   $(function() {
  33.     // 运行当前选中的特效
  34.     function runEffect() {
  35.       // 从中获取特效类型
  36.       var selectedEffect = $( "#effectTypes" ).val();
  37.  
  38.       // 大多数的特效类型默认不需要传递选项
  39.       var options = {};
  40.       // 一些特效带有必需的参数
  41.       if ( selectedEffect === "scale" ) {
  42.         options = { percent: 0 };
  43.       } else if ( selectedEffect === "size" ) {
  44.         options = { to: { width: 200, height: 60 } };
  45.       }
  46.  
  47.       // 运行特效
  48.       $( "#effect" ).toggle( selectedEffect, options, 500 );
  49.     };
  50.  
  51.     // 根据选择菜单值设置特效
  52.     $( "#button" ).click(function() {
  53.       runEffect();
  54.       return false;
  55.     });
  56.   });
  57.   </script>
  58. </head>
  59. <body>
  60.  
  61. <div class="toggler">
  62.   <div id="effect" class="ui-widget-content ui-corner-all">
  63.     <h3 class="ui-widget-header ui-corner-all">切换(Toggle)</h3>
  64.     <p>
  65.       Etiam libero neque, luctus a, eleifend nec, semper at, lorem. Sed pede. Nulla lorem metus, adipiscing ut, luctus sed, hendrerit vitae, mi.
  66.     </p>
  67.   </div>
  68. </div>
  69.  
  70. <select name="effects" id="effectTypes">
  71.   <option value="blind">百叶窗特效(Blind Effect)</option>
  72.   <option value="bounce">反弹特效(Bounce Effect)</option>
  73.   <option value="clip">剪辑特效(Clip Effect)</option>
  74.   <option value="drop">降落特效(Drop Effect)</option>
  75.   <option value="explode">爆炸特效(Explode Effect)</option>
  76.   <option value="fold">折叠特效(Fold Effect)</option>
  77.   <option value="highlight">突出特效(Highlight Effect)</option>
  78.   <option value="puff">膨胀特效(Puff Effect)</option>
  79.   <option value="pulsate">跳动特效(Pulsate Effect)</option>
  80.   <option value="scale">缩放特效(Scale Effect)</option>
  81.   <option value="shake">震动特效(Shake Effect)</option>
  82.   <option value="size">尺寸特效(Size Effect)</option>
  83.   <option value="slide">滑动特效(Slide Effect)</option>
  84. </select>
  85.  
  86. <a href="#" id="button" class="ui-state-default ui-corner-all">运行特效</a>
  87.  
  88.  
  89. </body>
  90. </html>