jQuery UI 实例 - 特效(Effect)

红太狼 2024-03-23 10:44 126阅读 0赞

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

如需了解更多有关 .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. var options = {};
  25. // 一些特效带有必需的参数
  26. if ( selectedEffect === "scale" ) {
  27. options = { percent: 0 };
  28. } else if ( selectedEffect === "transfer" ) {
  29. options = { to: "#button", className: "ui-effects-transfer" };
  30. } else if ( selectedEffect === "size" ) {
  31. options = { to: { width: 200, height: 60 } };
  32. }
  33. // 运行特效
  34. $( "#effect" ).effect( selectedEffect, options, 500, callback );
  35. };
  36. // 回调函数
  37. function callback() {
  38. setTimeout(function() {
  39. $( "#effect" ).removeAttr( "style" ).hide().fadeIn();
  40. }, 1000 );
  41. };
  42. // 根据选择菜单值设置特效
  43. $( "#button" ).click(function() {
  44. runEffect();
  45. return false;
  46. });
  47. });
  48. </script>
  49. </head>
  50. <body>
  51. <div class="toggler">
  52. <div id="effect" class="ui-widget-content ui-corner-all">
  53. <h3 class="ui-widget-header ui-corner-all">特效(Effect)</h3>
  54. <p>
  55. Etiam libero neque, luctus a, eleifend nec, semper at, lorem. Sed pede. Nulla lorem metus, adipiscing ut, luctus sed, hendrerit vitae, mi.
  56. </p>
  57. </div>
  58. </div>
  59. <select name="effects" id="effectTypes">
  60. <option value="blind">百叶窗特效(Blind Effect)</option>
  61. <option value="bounce">反弹特效(Bounce Effect)</option>
  62. <option value="clip">剪辑特效(Clip Effect)</option>
  63. <option value="drop">降落特效(Drop Effect)</option>
  64. <option value="explode">爆炸特效(Explode Effect)</option>
  65. <option value="fade">淡入淡出特效(Fade Effect)</option>
  66. <option value="fold">折叠特效(Fold Effect)</option>
  67. <option value="highlight">突出特效(Highlight Effect)</option>
  68. <option value="puff">膨胀特效(Puff Effect)</option>
  69. <option value="pulsate">跳动特效(Pulsate Effect)</option>
  70. <option value="scale">缩放特效(Scale Effect)</option>
  71. <option value="shake">震动特效(Shake Effect)</option>
  72. <option value="size">尺寸特效(Size Effect)</option>
  73. <option value="slide">滑动特效(Slide Effect)</option>
  74. <option value="transfer">转移特效(Transfer Effect)</option>
  75. </select>
  76. <a href="#" id="button" class="ui-state-default ui-corner-all">运行特效</a>
  77. </body>
  78. </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. var i = 0,
  25. width = 100,
  26. height = 100;
  27. $.each( $.easing, function( name, impl ) {
  28. var graph = $( "<div>" ).addClass( "graph" ).appendTo( "#graphs" ),
  29. text = $( "<div>" ).text( ++i + ". " + name ).appendTo( graph ),
  30. wrap = $( "<div>" ).appendTo( graph ).css( 'overflow', 'hidden' ),
  31. canvas = $( "<canvas>" ).appendTo( wrap )[ 0 ];
  32. canvas.width = width;
  33. canvas.height = height;
  34. var drawHeight = height * 0.8,
  35. cradius = 10;
  36. ctx = canvas.getContext( "2d" );
  37. ctx.fillStyle = "black";
  38. // 绘制背景
  39. ctx.beginPath();
  40. ctx.moveTo( cradius, 0 );
  41. ctx.quadraticCurveTo( 0, 0, 0, cradius );
  42. ctx.lineTo( 0, height - cradius );
  43. ctx.quadraticCurveTo( 0, height, cradius, height );
  44. ctx.lineTo( width - cradius, height );
  45. ctx.quadraticCurveTo( width, height, width, height - cradius );
  46. ctx.lineTo( width, 0 );
  47. ctx.lineTo( cradius, 0 );
  48. ctx.fill();
  49. // 绘制底线
  50. ctx.strokeStyle = "#555";
  51. ctx.beginPath();
  52. ctx.moveTo( width * 0.1, drawHeight + .5 );
  53. ctx.lineTo( width * 0.9, drawHeight + .5 );
  54. ctx.stroke();
  55. // 绘制顶线
  56. ctx.strokeStyle = "#555";
  57. ctx.beginPath();
  58. ctx.moveTo( width * 0.1, drawHeight * .3 - .5 );
  59. ctx.lineTo( width * 0.9, drawHeight * .3 - .5 );
  60. ctx.stroke();
  61. // 绘制 easing
  62. ctx.strokeStyle = "white";
  63. ctx.beginPath();
  64. ctx.lineWidth = 2;
  65. ctx.moveTo( width * 0.1, drawHeight );
  66. $.each( new Array( width ), function( position ) {
  67. var state = position / width,
  68. val = impl( state, position, 0, 1, width );
  69. ctx.lineTo( position * 0.8 + width * 0.1,
  70. drawHeight - drawHeight * val * 0.7 );
  71. });
  72. ctx.stroke();
  73. // 点击时动态改变
  74. graph.click(function() {
  75. wrap
  76. .animate( { height: "hide" }, 2000, name )
  77. .delay( 800 )
  78. .animate( { height: "show" }, 2000, name );
  79. });
  80. graph.width( width ).height( height + text.height() + 10 );
  81. });
  82. });
  83. </script>
  84. </head>
  85. <body>
  86. <div id="graphs"></div>
  87. </body>
  88. </html>

发表评论

表情:
评论列表 (有 0 条评论,126人围观)

还没有评论,来说两句吧...

相关阅读