Skip to content

Instantly share code, notes, and snippets.

@grahamd711
Created April 10, 2017 20:36
Show Gist options
  • Save grahamd711/debc9b7256f16ce65a2ef441013f67a1 to your computer and use it in GitHub Desktop.
Save grahamd711/debc9b7256f16ce65a2ef441013f67a1 to your computer and use it in GitHub Desktop.
$(function () {
myPage.init();
});
var myPage = (function() {
var that = {};
that.init = function () {
//trigger the animation - open modal window
$('[data-type="modal-trigger"]').on('click', function(){
var actionBtn = $(this),
scaleValue = retrieveScale(actionBtn.next('.cd-modal-bg'));
actionBtn.addClass('to-circle');
actionBtn.next('.cd-modal-bg').addClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
animateLayer(actionBtn.next('.cd-modal-bg'), scaleValue, true);
});
//if browser doesn't support transitions...
if(actionBtn.parents('.no-csstransitions').length > 0 ) animateLayer(actionBtn.next('.cd-modal-bg'), scaleValue, true);
});
//trigger the animation - close modal window
$('.cd-section .cd-modal-close').on('click', function(){
closeModal();
});
$(document).keyup(function(event){
if(event.which=='27') closeModal();
});
$(window).on('resize', function(){
//on window resize - update cover layer dimention and position
if($('.cd-section.modal-is-visible').length > 0) window.requestAnimationFrame(updateLayer);
});
function retrieveScale(btn) {
var btnRadius = btn.width()/2,
left = btn.offset().left + btnRadius,
top = btn.offset().top + btnRadius - $(window).scrollTop(),
scale = scaleValue(top, left, btnRadius, $(window).height(), $(window).width());
btn.css('position', 'fixed').velocity({
top: top - btnRadius,
left: left - btnRadius,
translateX: 0,
}, 0);
return scale;
}
function scaleValue( topValue, leftValue, radiusValue, windowW, windowH) {
var maxDistHor = ( leftValue > windowW/2) ? leftValue : (windowW - leftValue),
maxDistVert = ( topValue > windowH/2) ? topValue : (windowH - topValue);
return Math.ceil(Math.sqrt( Math.pow(maxDistHor, 2) + Math.pow(maxDistVert, 2) )/radiusValue);
}
function animateLayer(layer, scaleVal, bool) {
layer.velocity({ scale: scaleVal }, 400, function(){
$('body').toggleClass('overflow-hidden', bool);
(bool)
? layer.parents('.cd-section').addClass('modal-is-visible').end().off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend')
: layer.removeClass('is-visible').removeAttr( 'style' ).siblings('[data-type="modal-trigger"]').removeClass('to-circle');
});
}
function updateLayer() {
var layer = $('.cd-section.modal-is-visible').find('.cd-modal-bg'),
layerRadius = layer.width()/2,
layerTop = layer.siblings('.btn').offset().top + layerRadius - $(window).scrollTop(),
layerLeft = layer.siblings('.btn').offset().left + layerRadius,
scale = scaleValue(layerTop, layerLeft, layerRadius, $(window).height(), $(window).width());
layer.velocity({
top: layerTop - layerRadius,
left: layerLeft - layerRadius,
scale: scale,
}, 0);
}
function closeModal() {
var section = $('.cd-section.modal-is-visible');
section.removeClass('modal-is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
animateLayer(section.find('.cd-modal-bg'), 1, false);
});
//if browser doesn't support transitions...
if(section.parents('.no-csstransitions').length > 0 ) animateLayer(section.find('.cd-modal-bg'), 1, false);
}
$('#pagepiling').pagepiling({
verticalCentered: false,
css3: false,
navigation: {
textColor: '#fff',
bulletsColor: '#fff',
position: 'right',
tooltips: []
},
onLeave: function (index, nextIndex, direction) {
//fading out the txt of the leaving section
$('.section').eq(index - 1).find('h1, h5').fadeOut(700, 'easeInQuart');
//fading in the text of the destination (in case it was fadedOut)
$('.section').eq(nextIndex - 1).find('h1, h5').fadeIn(700, 'easeInQuart');
//reaching our last section? The one with our normal site?
if (nextIndex == 4) {
$('#arrow').hide();
//fading out navigation bullets
$('#pp-nav').fadeOut();
$('#section-4').find('.content').animate({
top: '0%'
}, 700, 'easeInQuart');
}
//leaving our last section? The one with our normal site?
if (index == 5) {
$('#arrow').show();
//fadding in navigation bullets
$('#pp-nav').fadeIn();
$('#section-4 .content').animate({
top: '100%'
}, 700, 'easeInQuart');
}
},
});
$('#arrow').click(function () {
$.fn.pagepiling.moveSectionDown();
});
}
return that;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment