|
| 1 | +<div id="vs-banner" class="vueschool-banner hide" role="banner"> |
| 2 | + <a href="https://vueschool.io/sales/blackfriday?friend=vuejs&utm_source=Vuejs.org&utm_medium=Link&utm_content=TopBanner&utm_campaign=Black%20Friday" title="Learn Vue.js with Vue School's video courses" target="_blank"> |
| 3 | + <img class="vueschool-banner--logo" src="/images/vueschool_logo.svg" alt="Vue School Logo"> |
| 4 | + <div class="vueschool-banner--wrapper"> |
| 5 | + <p>Black Friday 40% OFF at Vue School<span>Learn Vue.js through video courses.</span></p> |
| 6 | + </div> |
| 7 | + <div id="vs-close" class="vueschool-banner--close"></div> |
| 8 | + </a> |
| 9 | +</div> |
| 10 | + |
| 11 | +<script> |
| 12 | +
|
| 13 | + (function () { |
| 14 | + // Get elements |
| 15 | + var elBody = document.getElementsByTagName("body")[0]; |
| 16 | + var elBanner = document.getElementById("vs-banner"); |
| 17 | + var elClose = document.getElementById("vs-close"); |
| 18 | + var elMenu = document.getElementById("mobile-bar"); |
| 19 | + // Variable names |
| 20 | + var nameWrapper = "vueschool-weekend-promo"; |
| 21 | + var nameFixMenu = "vueschool-menu-fixed"; |
| 22 | + var nameStorage = "vueschool-banner"; |
| 23 | + // Defaults values |
| 24 | + var isMenuFixed = false; |
| 25 | + var posMenu = 80; |
| 26 | + var initBanner = function () { |
| 27 | + // Add event listeners |
| 28 | + toggleBannerEvents(true); |
| 29 | + // Add class to the body to push fixed elements |
| 30 | + elBody.classList.add(nameWrapper); |
| 31 | + // Display the banner |
| 32 | + elBanner.style.display = "block"; |
| 33 | + // Init close button action |
| 34 | + elClose.onclick = closeBanner; |
| 35 | + // Get the menu position |
| 36 | + getMenuPosition(); |
| 37 | + // Check current page offset position |
| 38 | + isMenuFixed = isUnderBanner(); |
| 39 | + // And position menu accordingly |
| 40 | + if (isMenuFixed) { |
| 41 | + elBody.classList.add("fixed"); |
| 42 | + } |
| 43 | + } |
| 44 | + var closeBanner = function(e) { |
| 45 | + // Prevent bubbling event that redirect to vueschool.io |
| 46 | + e.preventDefault(); |
| 47 | + // Remove events |
| 48 | + toggleBannerEvents(false); |
| 49 | + // Hide the banner |
| 50 | + elBanner.style.display = "none"; |
| 51 | + // Remove class to the body that push fixed elements |
| 52 | + elBody.classList.remove(nameWrapper); |
| 53 | + // Save action in the local storage |
| 54 | + localStorage.setItem(nameStorage, true); |
| 55 | + } |
| 56 | + var getMenuPosition = function() { |
| 57 | + posMenu = elBanner.clientHeight; |
| 58 | + } |
| 59 | + var isUnderBanner = function() { |
| 60 | + return window.pageYOffset > posMenu; |
| 61 | + } |
| 62 | + var fixMenuAfterBanner = function() { |
| 63 | + if (isUnderBanner()) { |
| 64 | + if (!isMenuFixed) { |
| 65 | + // The menu will be fixed |
| 66 | + toggleFixedPosition(true); |
| 67 | + } |
| 68 | + } else if (isMenuFixed) { |
| 69 | + // The menu stay under the banner |
| 70 | + toggleFixedPosition(false); |
| 71 | + } |
| 72 | + } |
| 73 | + var toggleBannerEvents = function (on) { |
| 74 | + // Add or remove event listerners attached to the DOM |
| 75 | + var method = on ? "addEventListener" : "removeEventListener"; |
| 76 | + window[method]("resize", getMenuPosition); |
| 77 | + window[method]("scroll", fixMenuAfterBanner); |
| 78 | + } |
| 79 | + var toggleFixedPosition = function (on) { |
| 80 | + // Switch between relative and fixed position |
| 81 | + var method = on ? "add" : "remove"; |
| 82 | + elBody.classList[method](nameFixMenu); |
| 83 | + isMenuFixed = on; |
| 84 | + } |
| 85 | + // Load component according to user preferences |
| 86 | + if (!localStorage.getItem(nameStorage)) { |
| 87 | + initBanner(); |
| 88 | + } |
| 89 | + })() |
| 90 | +</script> |
0 commit comments