Wowhead Fixed Search Header

Makes the page header (the search box) "sticky" when you scroll.

目前为 2016-10-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Wowhead Fixed Search Header
  3. // @description Makes the page header (the search box) "sticky" when you scroll.
  4. // @namespace drnick
  5. // @include *wowhead.com*
  6. // @grant none
  7. // @version 1.1.5
  8. // ==/UserScript==
  9.  
  10. (function() {
  11.  
  12. if (typeof jQuery == "undefined") throw "jQuery not found";
  13. if (window.top != window.self) return;
  14. var $ = jQuery;
  15. var $window = $(window);
  16. var $header = $(".header-search");
  17. var $headerInput = $(".header-search input");
  18. if ($header.length == 0) throw "#header not found";
  19. var styles = [
  20. ".header-search.sticky {",
  21. "position: fixed;",
  22. "top: 4px;",
  23. "right: 4px;",
  24. "z-index: 9999;",
  25. "width: 302px;",
  26. "box-shadow: 0 6px 8px #000;",
  27. "}",
  28. ".header-search.sticky input {",
  29. "width: 260px;",
  30. "padding: 2px;",
  31. "margin-left: -22px;",
  32. "}",
  33. ".header-search.sticky a {",
  34. "height: 26px;",
  35. "line-height: 26px;",
  36. "}",
  37. ].join("\n");
  38. $liveSearchStyle = $("<style type='text/css'></style");
  39. $("head").append("<style type='text/css'>" + styles + "</style>");
  40. $("head").append($liveSearchStyle);
  41. var offset = $header.offset().top;
  42. var height = $header.height();
  43. var $dummy = $header.clone();
  44. $dummy.empty();
  45. $dummy.attr("id", "header-dummy");
  46. $dummy.hide();
  47. $dummy.insertAfter($header);
  48. var liveSearchStyle = $liveSearchStyle.get(0);
  49. var isFloating = false;
  50. var liveSearchLeft = 0;
  51. var liveSearchTop = 0;
  52. var liveSearchWidth = 0;
  53. var liveSearchPos = "absolute";
  54. $window.scroll(function() {
  55. var scrollTop = $window.scrollTop();
  56. var recalc = true;
  57. if (!isFloating && scrollTop > offset) {
  58. $header.addClass("sticky");
  59. $dummy.show();
  60. isFloating = true;
  61. liveSearchPos = "fixed";
  62. $(".beta-ptr-links").hide();
  63. }
  64. else if (isFloating && scrollTop <= offset) {
  65. $header.removeClass("sticky");
  66. $dummy.hide();
  67. isFloating = false;
  68. liveSearchPos = "absolute";
  69. $(".beta-ptr-links").show();
  70. }
  71. else
  72. recalc = false;
  73. if (recalc) {
  74. liveSearchLeft = $header.offset().left | 0;
  75. liveSearchTop = ($header.offset().top + $header.height()) | 0;
  76. liveSearchWidth = $headerInput.outerWidth() | 0;
  77. if (isFloating) liveSearchTop -= ($window.scrollTop() | 0);
  78. liveSearchStyle.innerHTML = ".live-search { position:" + liveSearchPos + "; top: " + liveSearchTop + "px !important; " +
  79. "left: " + liveSearchLeft + "px !important; " +
  80. "width: " + liveSearchWidth + "px !important; }";
  81. }
  82. });
  83. $window.trigger('scroll');
  84. })();