强大 页面转换插件 jQuery smoothState

  • 源码大小:84.81KB
  • 所需积分:1积分
  • 源码编号:19JP-3507
  • 浏览次数:719次
  • 最后更新:2023年06月21日
  • 所属栏目:动画
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

在构建应用程序或网站时,页面之间的转换是需要考虑的一个重要设计方面。从用户体验的角度来看,过渡是用来引导和引导访问者的眼睛的。它们还提供了整体体验的一致性,使每个页面都具有独特的个性

SmoothState是一个逐步增强的jQuery插件,它在页面之间导航时为您的网站添加平滑和可配置的转换。SmoothState重量轻且简单,但具有大量功能:

特征:

  • 使用AJAX获取页面。
  • 自动更新URL和浏览历史记录。
  • 可与CSS3动画和第三方动画库配合使用。
  • 允许在将鼠标悬停在链接上时预取页面内容。
  • 也适用于锚定链接。

参见:

  • JavaScript中的10个最佳页面转换插件

如何使用它:

1.加载smoothState jQuery插件的缩小版。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/jquery.smoothState.min.js"></script>

2.将页面内容添加到具有uniue ID的包装器元素中。

  1. <div id="wrapper">
  2. Page Content
  3. </div>

3.使用以下回调将您自己的CSS或JavaScript支持的转换添加到页面。

  1. const options = {
  2.  
  3. // before loading a page
  4. onBefore: function($currentTarget, $container) {},
  5.  
  6. // when starting loading a page
  7. onStart: {
  8. // How long this animation takes
  9. duration: 0,
  10. // A function that dictates the animations that take place
  11. render: function ($container) {}
  12. },
  13.  
  14. // when loading a page
  15. onProgress: {
  16. // How long this animation takes
  17. duration: 0,
  18. // A function that dictates the animations that take place
  19. render: function ($container) {}
  20. },
  21.  
  22. // when the page is ready
  23. onReady: {
  24. duration: 0,
  25. render: function ($container, $newContent) {
  26. // Update the HTML on the page
  27. $container.html($newContent);
  28. }
  29. },
  30.  
  31. // after the page is loaded
  32. onAfter: function($container, $newContent) {}
  33. }

4.初始化smoothState。

  1. smoothState = $('#wrapper').smoothState(options).data('smoothState');

5.更多插件选项。

  1. /** If set to true, smoothState will log useful debug information instead of aborting */
  2. debug: false,
  3.  
  4. /** jQuery selector to specify which anchors smoothState should bind to */
  5. anchors: 'a',
  6.  
  7. /** Regex to specify which href smoothState should load. If empty, every href will be permitted. */
  8. hrefRegex: '',
  9.  
  10. /** jQuery selector to specify which forms smoothState should bind to */
  11. forms: 'form',
  12.  
  13. /** If set to true, smoothState will store form responses in the cache. */
  14. allowFormCaching: false,
  15.  
  16. /** Minimum number of milliseconds between click/submit events. Events ignored beyond this rate are ignored. */
  17. repeatDelay: 500,
  18.  
  19. /** A selector that defines what should be ignored by smoothState */
  20. blacklist: '.no-smoothState',
  21.  
  22. /** If set to true, smoothState will prefetch a link's contents on hover */
  23. prefetch: false,
  24.  
  25. /** The name of the event we will listen to from anchors if we're prefetching */
  26. prefetchOn: 'mouseover touchstart',
  27.  
  28. /** jQuery selector to specify elements which should not be prefetched */
  29. prefetchBlacklist: '.no-prefetch',
  30.  
  31. /** The response header field name defining the request's final URI. Useful for resolving redirections. */
  32. locationHeader: 'X-SmoothState-Location',
  33.  
  34. /** The number of pages smoothState will try to store in memory */
  35. cacheLength: 0,
  36.  
  37. /** Class that will be applied to the body while the page is loading */
  38. loadingClass: 'is-loading',
  39.  
  40. /** Scroll to top after onStart and scroll to hash after onReady */
  41. scroll: true,
  42.  
  43. /**
  44. * A function that can be used to alter the ajax request settings before it is called
  45. * @param {Object} request - jQuery.ajax settings object that will be used to make the request
  46. * @return {Object} Altered request object
  47. */
  48. alterRequest: function (request) {
  49. return request;
  50. },
  51.  
  52. /**
  53. * A function that can be used to alter the state object before it is updated or added to the browsers history
  54. * @param {Object} state - An object that will be assigned to history entry
  55. * @param {string} title - The history entry's title. For reference only
  56. * @param {string} url - The history entry's URL. For reference only
  57. * @return {Object} Altered state object
  58. */
  59. alterChangeState: function (state, title, url) {
  60. return state;
  61. },

6.初始化smoothState。

  1. // load a page into the current container
  2. smoothState.load(url)
  3.  
  4. // fetch content from a specific page
  5. smoothState.fetch(url)
  6.  
  7. // clear the page from the cache
  8. smoothState.clear(url)
  9.  
  10. // restart CSS animations
  11. smoothState.restartCSSAnimations()

预览截图