|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +function addScrollToBottom() { |
| 4 | + function showButton() { |
| 5 | + const remainingScroll = scrollContainer.scrollHeight - currentScroll() - window.innerHeight; |
| 6 | + |
| 7 | + // Hide the button when only two clicks are left |
| 8 | + if (remainingScroll <= 1 * incrementAmount) { |
| 9 | + button.className = "hidden"; |
| 10 | + } else { |
| 11 | + button.className = ""; // Ensure the button is visible |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + function currentScroll() { |
| 16 | + return scrollContainer.scrollTop || document.documentElement.scrollTop || 0; |
| 17 | + } |
| 18 | + |
| 19 | + function scrollToPosition(position) { |
| 20 | + scrollContainer.scrollTop = position; |
| 21 | + document.documentElement.scrollTop = position; |
| 22 | + } |
| 23 | + |
| 24 | + function scrollIncrementally() { |
| 25 | + // Scroll down by a specific amount each time the button is clicked |
| 26 | + var targetPosition = currentScroll() + incrementAmount; |
| 27 | + |
| 28 | + // Make sure we don't scroll past the bottom |
| 29 | + if (targetPosition + window.innerHeight > scrollContainer.scrollHeight) { |
| 30 | + targetPosition = scrollContainer.scrollHeight - window.innerHeight; |
| 31 | + } |
| 32 | + |
| 33 | + scrollToPosition(targetPosition); |
| 34 | + showButton(); // Update button visibility after scrolling |
| 35 | + } |
| 36 | + |
| 37 | + var options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, |
| 38 | + backgroundColor = options.backgroundColor || "#e2e2e2", // Gray background similar to the back-to-top button |
| 39 | + cornerOffset = options.cornerOffset || 20, |
| 40 | + diameter = options.diameter || 40, |
| 41 | + ease = options.ease || function (t) { return .5 * (1 - Math.cos(Math.PI * t)); }, |
| 42 | + id = options.id || "scroll-to-bottom", |
| 43 | + 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 |
| 44 | + scrollContainer = options.scrollContainer || document.body, |
| 45 | + scrollDuration = options.scrollDuration || 100, |
| 46 | + showWhenScrollTopIs = options.showWhenScrollTopIs || 1, |
| 47 | + size = options.size || diameter, |
| 48 | + textColor = options.textColor || "#000", // Arrow color similar to back-to-top button |
| 49 | + zIndex = options.zIndex || 9999; // Ensure the button is always on top |
| 50 | + |
| 51 | + var n = Math.round(.5 * size), // Adjusted for better centering |
| 52 | + i = Math.round(.25 * size), // Adjusted for better centering |
| 53 | + 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}"; |
| 54 | + |
| 55 | + var r = document.createElement("style"); |
| 56 | + r.appendChild(document.createTextNode(d)); |
| 57 | + document.head.insertAdjacentElement("afterbegin", r); |
| 58 | + |
| 59 | + var incrementAmount = window.innerHeight / 1.5; // Set the scroll increment amount |
| 60 | + |
| 61 | + var button = function () { |
| 62 | + var o = document.createElement("div"); |
| 63 | + return o.id = id, o.className = "", o.innerHTML = innerHTML, o.addEventListener("click", function (o) { |
| 64 | + o.preventDefault(); |
| 65 | + scrollIncrementally(); // Incremental scrolling |
| 66 | + }), document.body.appendChild(o), o; |
| 67 | + }(), |
| 68 | + hidden = !1, |
| 69 | + threshold = showWhenScrollTopIs; |
| 70 | + |
| 71 | + function adjustButtonPosition() { |
| 72 | + var navbarHeight = document.querySelector('.navbar').offsetHeight || 0; |
| 73 | + |
| 74 | + // Set the button's top position considering the navbar height |
| 75 | + var finalPosition = navbarHeight + 20; // Adjust this value as needed to ensure it's not hidden under the navbar |
| 76 | + button.style.top = finalPosition + "px"; |
| 77 | + } |
| 78 | + |
| 79 | + adjustButtonPosition(); // Initial adjustment to ensure visibility |
| 80 | + |
| 81 | + window.addEventListener("resize", adjustButtonPosition); // Adjust if window is resized |
| 82 | + (scrollContainer === document.body ? window : scrollContainer).addEventListener("scroll", showButton); |
| 83 | + showButton(); |
| 84 | +} |
| 85 | + |
| 86 | +window.addEventListener("load", function () { |
| 87 | + var button = document.getElementById("scroll-to-bottom"), |
| 88 | + stickyTop = document.querySelector(".sticky-top"), |
| 89 | + fixedTop = document.querySelector(".fixed-top"); |
| 90 | + |
| 91 | + button && window.addEventListener("scroll", function () { |
| 92 | + if (stickyTop) { |
| 93 | + var stickyTopRect = stickyTop.getBoundingClientRect(); |
| 94 | + window.scrollY > stickyTopRect.height |
| 95 | + ? (button.style.position = "fixed", button.style.top = stickyTopRect.height + 15 + "px") |
| 96 | + : (button.style.position = "fixed", button.style.top = stickyTopRect.height + 15 + "px"); |
| 97 | + } |
| 98 | + |
| 99 | + if (fixedTop) { |
| 100 | + var fixedTopRect = fixedTop.getBoundingClientRect(); |
| 101 | + button.style.position = "fixed"; |
| 102 | + button.style.top = fixedTopRect.height + 15 + "px"; |
| 103 | + } |
| 104 | + }); |
| 105 | +}); |
0 commit comments