特效(Effect)


jQuery UI 实例 - 特效(Effect)

对一个元素应用动画特效。

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

.effect() 演示

点击按钮预览特效。

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

Easing 演示

本实例使用 HTML Canvas 元素,绘制了 jQuery UI 提供的所有 easings。 点击每个图可查看该 easing 的行为。。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 特效 - Easing 演示</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.   .graph {
  12.     float: left;
  13.     margin-left: 10px;
  14.   }
  15.   </style>
  16.   <script>
  17.   $(function() {
  18.     if ( !$( "<canvas>" )[0].getContext ) {
  19.       $( "<div>" ).text(
  20.         "您的浏览器不支持 canvas,本演示需要在支持 canvas 的浏览器下进行。"
  21.       ).appendTo( "#graphs" );
  22.       return;
  23.     }
  24.  
  25.     var i = 0,
  26.       width = 100,
  27.       height = 100;
  28.  
  29.     $.each( $.easing, function( name, impl ) {
  30.       var graph = $( "<div>" ).addClass( "graph" ).appendTo( "#graphs" ),
  31.         text = $( "<div>" ).text( +++ ". " + name ).appendTo( graph ),
  32.         wrap = $( "<div>" ).appendTo( graph ).css( 'overflow', 'hidden' ),
  33.         canvas = $( "<canvas>" ).appendTo( wrap )[ 0 ];
  34.  
  35.       canvas.width = width;
  36.       canvas.height = height;
  37.       var drawHeight = height * 0.8,
  38.         cradius = 10;
  39.         ctx = canvas.getContext( "2d" );
  40.       ctx.fillStyle = "black";
  41.  
  42.       // 绘制背景
  43.       ctx.beginPath();
  44.       ctx.moveTo( cradius, 0 );
  45.       ctx.quadraticCurveTo( 0, 0, 0, cradius );
  46.       ctx.lineTo( 0, height - cradius );
  47.       ctx.quadraticCurveTo( 0, height, cradius, height );
  48.       ctx.lineTo( width - cradius, height );
  49.       ctx.quadraticCurveTo( width, height, width, height - cradius );
  50.       ctx.lineTo( width, 0 );
  51.       ctx.lineTo( cradius, 0 );
  52.       ctx.fill();
  53.  
  54.       // 绘制底线
  55.       ctx.strokeStyle = "#555";
  56.       ctx.beginPath();
  57.       ctx.moveTo( width * 0.1, drawHeight + .5 );
  58.       ctx.lineTo( width * 0.9, drawHeight + .5 );
  59.       ctx.stroke();
  60.  
  61.       // 绘制顶线
  62.       ctx.strokeStyle = "#555";
  63.       ctx.beginPath();
  64.       ctx.moveTo( width * 0.1, drawHeight * .3 - .5 );
  65.       ctx.lineTo( width * 0.9, drawHeight * .3 - .5 );
  66.       ctx.stroke();
  67.  
  68.       // 绘制 easing
  69.       ctx.strokeStyle = "white";
  70.       ctx.beginPath();
  71.       ctx.lineWidth = 2;
  72.       ctx.moveTo( width * 0.1, drawHeight );
  73.       $.each( new Array( width ), function( position ) {
  74.         var state = position / width,
  75.           val = impl( state, position, 0, 1, width );
  76.         ctx.lineTo( position * 0.8 + width * 0.1,
  77.           drawHeight - drawHeight * val * 0.7 );
  78.       });
  79.       ctx.stroke();
  80.  
  81.       // 点击时动态改变
  82.       graph.click(function() {
  83.         wrap
  84.           .animate( { height: "hide" }, 2000, name )
  85.           .delay( 800 )
  86.           .animate( { height: "show" }, 2000, name );
  87.       });
  88.  
  89.       graph.width( width ).height( height + text.height() + 10 );
  90.     });
  91.   });
  92.   </script>
  93. </head>
  94. <body>
  95.  
  96. <div id="graphs"></div>
  97.  
  98.  
  99. </body>
  100. </html>