You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.8 KiB

2 years ago
  1. ;(function ( $, window, document, undefined ) {
  2. $.fixed = function(element, options) {
  3. var defaults = {
  4. topSpace: 0,
  5. placeholder: true,
  6. onFixed: function() {},
  7. onStatic: function() {}
  8. }
  9. var plugin = this;
  10. plugin.settings = {}
  11. var $element = $(element),
  12. element = element;
  13. var wrapper, placeholder, wrapperTop;
  14. plugin.init = function() {
  15. plugin.settings = $.extend({}, defaults, options);
  16. $(element).wrap("<div class='navigation-fixed-wrapper'></div>").each(function(){
  17. wrapper = $(element).parent();
  18. if(plugin.settings.placeholder){
  19. $("<div class='navigation-fixed-placeholder'></div>").insertBefore(wrapper);
  20. $(".navigation-fixed-placeholder").css("height", $(wrapper).outerHeight());
  21. placeholder = $(".navigation-fixed-placeholder");
  22. }
  23. });
  24. wrapperTop = $(wrapper).offset().top;
  25. if(wrapperTop <= plugin.settings.topSpace){
  26. toFixed();
  27. }
  28. else{
  29. $(window).on("scroll", function(){
  30. if($(window).scrollTop() >= wrapperTop - plugin.settings.topSpace){
  31. if(!$(wrapper).hasClass("fixed"))
  32. toFixed();
  33. }
  34. else{
  35. if($(wrapper).hasClass("fixed"))
  36. toStatic();
  37. }
  38. });
  39. }
  40. $(window).resize(function(){
  41. resizeNav();
  42. if(plugin.settings.placeholder){
  43. $(placeholder).css("height", $(wrapper).outerHeight());
  44. }
  45. })
  46. $(element).on("click touchstart", function(){
  47. resizeNav();
  48. });
  49. }
  50. var toFixed = function() {
  51. $(wrapper).addClass("fixed");
  52. if(plugin.settings.placeholder){
  53. $(placeholder).addClass("visible");
  54. }
  55. resizeNav();
  56. if(options !== undefined){
  57. plugin.callback("onFixed");
  58. }
  59. }
  60. var toStatic = function() {
  61. $(wrapper).removeClass("fixed");
  62. if(plugin.settings.placeholder){
  63. $(placeholder).removeClass("visible");
  64. }
  65. if(options !== undefined){
  66. plugin.callback("onStatic");
  67. }
  68. }
  69. var resizeNav = function() {
  70. $(element).css("width", $(wrapper).parent().width());
  71. $(wrapper).css("top", plugin.settings.topSpace);
  72. }
  73. var detectIOS = function() {
  74. var userAgent = navigator.userAgent || navigator.vendor || window.opera;
  75. if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
  76. return true;
  77. }
  78. else{
  79. return false;
  80. }
  81. }
  82. plugin.callback = function(func) {
  83. if (options[func] !== undefined) {
  84. options[func].call(element);
  85. }
  86. }
  87. plugin.init();
  88. }
  89. $.fn.fixed = function(options) {
  90. return this.each(function() {
  91. if (undefined == $(this).data('fixed')) {
  92. var plugin = new $.fixed(this, options);
  93. $(this).data('fixed', plugin);
  94. }
  95. });
  96. }
  97. })( jQuery, window, document );