|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +function addScrollToBottom() { |
| 4 | + function showButton() { |
| 5 | + currentScroll() <= threshold ? hidden && (button.className = "", hidden = !1) : hidden || (button.className = "hidden", hidden = !0); |
| 6 | + } |
| 7 | + |
| 8 | + function currentScroll() { |
| 9 | + return scrollContainer.scrollTop || document.documentElement.scrollTop || 0; |
| 10 | + } |
| 11 | + |
| 12 | + function scrollToPosition(position) { |
| 13 | + scrollContainer.scrollTop = position; |
| 14 | + document.documentElement.scrollTop = position; |
| 15 | + } |
| 16 | + |
| 17 | + function scrollToBottom() { |
| 18 | + var targetPosition = scrollContainer.scrollHeight - window.innerHeight; |
| 19 | + scrollToPosition(targetPosition); |
| 20 | + } |
| 21 | + |
| 22 | + var options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, |
| 23 | + backgroundColor = options.backgroundColor || "#e2e2e2", // Gray background similar to the back-to-top button |
| 24 | + cornerOffset = options.cornerOffset || 20, |
| 25 | + diameter = options.diameter || 40, |
| 26 | + ease = options.ease || function (t) { return .5 * (1 - Math.cos(Math.PI * t)); }, |
| 27 | + id = options.id || "scroll-to-bottom", |
| 28 | + innerHTML = options.innerHTML || '<svg viewBox="0 0 24 24"><path d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6z"></path></svg>', // Down arrow SVG |
| 29 | + scrollContainer = options.scrollContainer || document.body, |
| 30 | + scrollDuration = options.scrollDuration || 100, |
| 31 | + showWhenScrollTopIs = options.showWhenScrollTopIs || 1, |
| 32 | + size = options.size || diameter, |
| 33 | + textColor = options.textColor || "#000", // Arrow color similar to back-to-top button |
| 34 | + zIndex = options.zIndex || 1; |
| 35 | + |
| 36 | + var n = Math.round(.5 * size), // Adjusted for better centering |
| 37 | + i = Math.round(.25 * size), // Adjusted for better centering |
| 38 | + d = "#" + id + "{background:" + backgroundColor + ";-webkit-border-radius:50%;-moz-border-radius:50%;border-radius:50%;top:" + cornerOffset + "px;-webkit-box-shadow:0 2px 5px 0 rgba(0,0,0,.26);-moz-box-shadow:0 2px 5px 0 rgba(0,0,0,.26);box-shadow:0 2px 5px 0 rgba(0,0,0,.26);color:" + textColor + ";cursor:pointer;display:block;height:" + size + "px;opacity:1;outline:0;position:fixed;right:" + cornerOffset + "px;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-transition:top .2s,opacity .2s;-o-transition:top .2s,opacity .2s;-moz-transition:top .2s,opacity .2s;transition:top .2s,opacity .2s;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:" + size + "px;z-index:" + zIndex + "}#" + id + " svg{display:block;fill:currentColor;height:" + n + "px;margin:" + i + "px auto 0;width:" + n + "px}#" + id + ".hidden{top:-" + size + "px;opacity:0}"; |
| 39 | + |
| 40 | + var r = document.createElement("style"); |
| 41 | + r.appendChild(document.createTextNode(d)); |
| 42 | + document.head.insertAdjacentElement("afterbegin", r); |
| 43 | + |
| 44 | + var button = function () { |
| 45 | + var o = document.createElement("div"); |
| 46 | + return o.id = id, o.className = "hidden", o.innerHTML = innerHTML, o.addEventListener("click", function (o) { |
| 47 | + o.preventDefault(); |
| 48 | + scrollToBottom(); |
| 49 | + }), document.body.appendChild(o), o; |
| 50 | + }(), |
| 51 | + hidden = !0, |
| 52 | + threshold = showWhenScrollTopIs; |
| 53 | + |
| 54 | + function adjustButtonPosition() { |
| 55 | + var navbarHeight = document.querySelector('.navbar').offsetHeight || 0; |
| 56 | + button.style.top = (cornerOffset + navbarHeight) + "px"; |
| 57 | + } |
| 58 | + |
| 59 | + adjustButtonPosition(); // Initial adjustment to ensure visibility |
| 60 | + |
| 61 | + window.addEventListener("resize", adjustButtonPosition); // Adjust if window is resized |
| 62 | + (scrollContainer === document.body ? window : scrollContainer).addEventListener("scroll", showButton); |
| 63 | + showButton(); |
| 64 | +} |
| 65 | + |
| 66 | +window.addEventListener("load", function () { |
| 67 | + var button = document.getElementById("scroll-to-bottom"), |
| 68 | + stickyTop = document.querySelector(".sticky-top"), |
| 69 | + fixedTop = document.querySelector(".fixed-top"); |
| 70 | + |
| 71 | + button && window.addEventListener("scroll", function () { |
| 72 | + if (stickyTop) { |
| 73 | + var stickyTopRect = stickyTop.getBoundingClientRect(); |
| 74 | + window.scrollY > stickyTopRect.height |
| 75 | + ? (button.style.position = "fixed", button.style.top = stickyTopRect.height + 15 + "px") |
| 76 | + : (button.style.position = "fixed", button.style.top = "15px"); |
| 77 | + } |
| 78 | + |
| 79 | + if (fixedTop) { |
| 80 | + var fixedTopRect = fixedTop.getBoundingClientRect(); |
| 81 | + button.style.position = "fixed"; |
| 82 | + button.style.top = fixedTopRect.height + 15 + "px"; |
| 83 | + } |
| 84 | + }); |
| 85 | +}); |
0 commit comments