aboutsummaryrefslogtreecommitdiff
path: root/assets/js
diff options
context:
space:
mode:
authorTrack3 <pengliabc@live.cn>2018-10-23 13:15:50 +0800
committerTrack3 <pengliabc@live.cn>2018-10-23 13:15:50 +0800
commit44dbcd6f34c676cefbacf12f18d9883c597a681a (patch)
tree86610dc396b64baf3f2c4ef00a3b84dc6c1d2bde /assets/js
downloadhermit-44dbcd6f34c676cefbacf12f18d9883c597a681a.tar.gz
First commit
Diffstat (limited to 'assets/js')
-rw-r--r--assets/js/main.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/assets/js/main.js b/assets/js/main.js
new file mode 100644
index 0000000..ed98d94
--- /dev/null
+++ b/assets/js/main.js
@@ -0,0 +1,78 @@
+/**
+ * Utils
+ */
+
+// Throttle
+//
+const throttle = (callback, limit) => {
+ let timeoutHandler = null;
+ return () => {
+ if (timeoutHandler == null) {
+ timeoutHandler = setTimeout(() => {
+ callback();
+ timeoutHandler = null;
+ }, limit);
+ }
+ };
+};
+
+/**
+ * Functions
+ */
+
+// Auto Hide Header
+//
+let lastScrollPosition = window.pageYOffset;
+let header = document.getElementById('site-header');
+
+const autoHideHeader = () => {
+ let currentScrollPosition = window.pageYOffset;
+ if (currentScrollPosition > lastScrollPosition) {
+ header.classList.remove('slideInUp');
+ header.classList.add('slideOutDown');
+ } else {
+ header.classList.remove('slideOutDown');
+ header.classList.add('slideInUp');
+ }
+ lastScrollPosition = currentScrollPosition;
+}
+
+// Mobile Menu Toggle
+//
+let mobileMenu = document.getElementById('mobile-menu');
+let mobileMenuVisible = false;
+
+const mobileMenuToggle = () => {
+ if (mobileMenuVisible == false) {
+ mobileMenu.style.animationName = 'bounceInRight';
+ mobileMenu.style.webkitAnimationName = 'bounceInRight';
+ mobileMenu.style.display = 'block';
+ mobileMenuVisible = true;
+ } else {
+ mobileMenu.style.animationName = 'bounceOutRight';
+ mobileMenu.style.webkitAnimationName = 'bounceOutRight'
+ mobileMenuVisible = false;
+ }
+}
+
+// Show Featured Image
+//
+const showFeaturedImg = () => {
+ document.getElementById('bg-img').classList.add('show-bg-img');
+}
+
+const showContent = () => {
+ document.getElementById('bg-img').classList.remove('show-bg-img');
+}
+
+if (haveHeader == true) {
+ document.getElementById('menu-btn').addEventListener('click', mobileMenuToggle);
+
+ window.addEventListener('scroll', throttle(() => {
+ autoHideHeader();
+
+ if (mobileMenuVisible == true) {
+ mobileMenuToggle();
+ }
+ }, 250));
+}