jam-cloud/web/app/assets/javascripts/modern/home_page.js

307 lines
7.6 KiB
JavaScript

(function ($) {
"use strict";
/* ==========================================================================
ieViewportFix - fixes viewport problem in IE 10 SnapMode and IE Mobile 10
========================================================================== */
function ieViewportFix() {
var msViewportStyle = document.createElement("style");
msViewportStyle.appendChild(
document.createTextNode(
"@-ms-viewport { width: device-width; }"
)
);
if (navigator.userAgent.match(/IEMobile\/10\.0/)) {
msViewportStyle.appendChild(
document.createTextNode(
"@-ms-viewport { width: auto !important; }"
)
);
}
document.getElementsByTagName("head")[0].
appendChild(msViewportStyle);
}
/* ==========================================================================
exists - Check if an element exists
========================================================================== */
function exists(e) {
return $(e).length > 0;
}
/* ==========================================================================
handleMobileMenu
========================================================================== */
var MOBILEBREAKPOINT = 768;
function handleMobileMenu() {
if ($(window).width() > MOBILEBREAKPOINT) {
$("#mobile-menu").hide();
$("#mobile-menu-trigger").removeClass("mobile-menu-opened").addClass("mobile-menu-closed");
} else {
if (!exists("#mobile-menu")) {
$("#menu").clone().attr({
id: "mobile-menu",
"class": "fixed"
}).insertAfter("#header");
}
$('#mobile-menu a').on("click", function () {
var $t = $('#mobile-menu-trigger');
var $n = $('#mobile-menu');
var ti = $(this).attr('title');
var $element = $('#' + ti);
$.scrollTo($element, {duration: 500, offset: -150})
if ($t.hasClass("mobile-menu-opened")) {
$t.removeClass("mobile-menu-opened").addClass("mobile-menu-closed");
$n.slideUp(300);
} else {
$t.removeClass("mobile-menu-closed").addClass("mobile-menu-opened");
$n.slideDown(300);
}
event.preventDefault();
});
$("#mobile-menu li, #mobile-menu li a, #mobile-menu ul").attr("style", "");
}
}
/* ==========================================================================
showHideMobileMenu
========================================================================== */
function showHideMobileMenu() {
$("#mobile-menu-trigger").on("click", function (event) {
var $t = $(this),
$n = $("#mobile-menu");
if ($t.hasClass("mobile-menu-opened")) {
$t.removeClass("mobile-menu-opened").addClass("mobile-menu-closed");
$n.slideUp(300);
} else {
$t.removeClass("mobile-menu-closed").addClass("mobile-menu-opened");
$n.slideDown(300);
}
event.preventDefault();
});
}
/* ==========================================================================
handleBackToTop
========================================================================== */
function handleBackToTop() {
$('#back-to-top').on("click", function () {
$('html, body').animate({scrollTop: 0}, 'slow');
return false;
});
}
/* ==========================================================================
showHidebackToTop
========================================================================== */
function showHidebackToTop() {
if ($(window).scrollTop() > $(window).height() / 2) {
$("#back-to-top").removeClass('gone').addClass('visible');
} else {
$("#back-to-top").removeClass('visible').addClass('gone');
}
}
/* ==========================================================================
handleVideoBackground
========================================================================== */
var min_w = 0,
video_width_original = 1920,
video_height_original = 1080,
vid_ratio = 1920 / 1080;
function handleVideoBackground() {
$('.fullwidth-section .fullwidth-section-video').each(function (i) {
var $sectionWidth = $(this).closest('.fullwidth-section').outerWidth(),
$sectionHeight = $(this).closest('.fullwidth-section').outerHeight();
$(this).width($sectionWidth);
$(this).height($sectionHeight);
// calculate scale ratio
var scale_h = $sectionWidth / video_width_original,
scale_v = $sectionHeight / video_height_original,
scale = scale_h > scale_v ? scale_h : scale_v;
// limit minimum width
min_w = vid_ratio * ($sectionHeight + 20);
if (scale * video_width_original < min_w) {
scale = min_w / video_width_original;
}
$(this).find('video').width(Math.ceil(scale * video_width_original + 2)).height(Math.ceil(scale * video_height_original + 2));
});
}
/* ==========================================================================
handleStickyHeader
========================================================================== */
var stickyHeader = false;
var stickypoint = 150;
if ($('body').hasClass('sticky-header')) {
stickyHeader = true;
}
//stickypoint = $("#header-top").outerHeight() + $("#header-wrap").outerHeight() + 150;
function handleStickyHeader() {
var b = document.documentElement,
e = false;
function f() {
window.addEventListener("scroll", function (h) {
if (!e) {
e = true;
setTimeout(d, 250);
}
}, false);
window.addEventListener("load", function (h) {
if (!e) {
e = true;
setTimeout(d, 250);
}
}, false);
}
function d() {
var h = c();
if (h >= stickypoint) {
$('#header').addClass("stuck");
$('#logo img').attr("src", "assets/images/logo-2.png");
} else {
$('#header').removeClass("stuck");
$('#logo img').attr("src", "assets/images/logo.png");
$('.header-style-2 #logo img').attr("src", "assets/images/logo-2.png");
$('#content').animate({top: '0px'}, 100);
}
e = false;
}
function c() {
return window.pageYOffset || b.scrollTop;
}
f();
}
/* ==========================================================================
When document is ready, do
========================================================================== */
window.modernNavInit = function () {
ieViewportFix();
//if (!isTouchDevice()) {
// enableParallax();
//}
handleMobileMenu();
showHideMobileMenu();
handleBackToTop();
showHidebackToTop();
handleVideoBackground();
$('.sf-menu li a').on('click', function () {
var ti = $(this).attr('title');
var $element = $('#' + ti);
$.scrollTo($element, {duration: 500, offset: -150})
});
$('#logo').click(function (e) {
$('body').animate({scrollTop: 0}, 500);
});
};
/* ==========================================================================
When the window is scrolled, do
========================================================================== */
$(window).scroll(function () {
showHidebackToTop();
});
/* ==========================================================================
When the window is resized, do
========================================================================== */
$(window).resize(function () {
handleMobileMenu();
handleVideoBackground();
//if(stickyHeader && ($(window).width() > 1024)){
// handleStickyHeader();
//}
});
})(jQuery);
// non jQuery scripts below