View difference between Paste ID: h0Y6hKj4 and f5C3KSEZ
SHOW: | | - or go back to the newest paste.
1
/*on function "retrieveScale(btn)", when you will put a value on var "scale" you call:
2
	scale = scaleValue(top, left, btnRadius, $(window).width(), $(window).height());
3
but on function "scaleValue()" you put theses args:
4
	scaleValue( topValue, leftValue, radiusValue, windowH, windowW)
5
6-
notice that the last two args are "windoW" (Window Width) and "WindowH" (Window Height)... So that's a mistake, right? this must be
6+
notice that the last two args are "windoH" (Window Height) and "WindowW" (Window Width)... So that's a mistake, right? this must be
7
	scale = scaleValue(top, left, btnRadius, $(window).height(), $(window).width());
8
9
other problem is that "close" button with a null target (#0). Solved it, just copy the code below to your morphing_modal_constructor
10
*/
11
//Advanced Morphing Modal
12
$(document).ready(function() {
13
	$('[data-type="modal-trigger"]').click(function(){
14
		var actionBtn = $(this),
15
			URL = actionBtn.attr('data-url');
16
		$.ajax({
17
			type: "POST",
18
			url: URL,
19
			dataType: "text",
20
			success: function(response) {
21
				morphingModal(actionBtn, response);
22
			}
23
		});
24
	});
25
	$(document).keyup(function(event){
26
		if($('.modal-is-visible').length > 0 && event.which=='27') closeModal();
27
	});
28
29
	$(window).on('resize', function(){
30
		//on window resize - update cover layer dimention and position
31
		if($('.modal-is-visible').length > 0) window.requestAnimationFrame(updateLayer);
32
	});
33
});
34
35
function createComponents() {
36
	var section = $("<section/>");
37
	section.attr({
38
		class: "cd-section",
39
		id: "mm_point"
40
	});
41
	var modal = $("<div/>");
42
	modal.attr({
43
		class: 'cd-modal'
44
	});
45
	var content = $("<div/>");
46
	content.attr({
47
		class: 'cd-modal-content',
48
		id: 'mm_content'
49
	});
50
	modal.append(content);
51
	var closeBtn = $('<a/>').attr({
52
		class: 'cd-modal-close',
53
		id: "mm_close",
54
		style: "pointer: cursor"
55
	}).html('Fechar').click(function(){
56
		closeModal();
57
	});
58
	section.append(modal).append(closeBtn);
59
	$('body').append(section);
60
}
61
62
function removeComponents() {
63
	$('#mm_point').remove();
64
}
65
66
function morphingModal(actionBtn, content) {
67
	if($("#mm_content").length == 0) createComponents();
68
	$('#mm_content').html(content);
69
	openModal(actionBtn);
70
}
71
72
function retrieveScale(btn) {
73
	var btnRadius = btn.width()/2,
74
		left = btn.offset().left + btnRadius,
75
		top = btn.offset().top + btnRadius - $(window).scrollTop(),
76
		scale = scaleValue(top, left, btnRadius, $(window).width(), $(window).height());
77
78
	btn.css('position', 'fixed').velocity({
79
		top: top - btnRadius,
80
		left: left - btnRadius,
81
		translateX: 0,
82
	}, 0);
83
84
	return scale;
85
}
86
87
function scaleValue( topValue, leftValue, radiusValue, windowW, windowH) {
88
	var maxDistHor = ( leftValue > windowW/2) ? leftValue : (windowW - leftValue),
89
		maxDistVert = ( topValue > windowH/2) ? topValue : (windowH - topValue);
90
	return Math.ceil(Math.sqrt( Math.pow(maxDistHor, 2) + Math.pow(maxDistVert, 2) )/radiusValue);
91
}
92
93
function animateLayer(layer, scaleVal, bool, id) {
94
	layer.velocity({ scale: scaleVal }, 400, function(){
95
		$('body').toggleClass('overflow-hidden', bool);
96
		if(bool) {
97
			$("#mm_point").addClass('modal-is-visible');
98
		} else {
99
			layer.removeClass('is-visible').removeAttr('style');
100
			$('.to-circle[data-type="modal-trigger"]').removeClass('to-circle');
101
		}
102
	});
103
}
104
105
function updateLayer() {
106
	var layer = $('.cd-modal-bg'),
107
		layerRadius = layer.width()/2,
108
		layerTop = layer.siblings('.btn').offset().top + layerRadius - $(window).scrollTop(),
109
		layerLeft = layer.siblings('.btn').offset().left + layerRadius,
110
		scale = scaleValue(layerTop, layerLeft, layerRadius,$(window).width(), $(window).height());
111
	
112
	layer.velocity({
113
		top: layerTop - layerRadius,
114
		left: layerLeft - layerRadius,
115
		scale: scale,
116
	}, 0);
117
}
118
119
function openModal(actionBtn) {
120
	var bg = $('.cd-modal-bg');
121
	actionBtn.addClass('to-circle');
122
	bg.css("top", actionBtn.offset().top + "px").css("left", actionBtn.offset().left + (actionBtn.width()/2) + "px");
123
	var scaleValue = retrieveScale(bg);
124
	$('.cd-modal-bg').addClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
125
		animateLayer($('.cd-modal-bg'), scaleValue, true);
126
		$(this).off();
127
	});
128
129
	//if browser doesn't support transitions...
130
	if($('.no-csstransitions').length > 0 ) animateLayer($('.cd-modal-bg'), scaleValue, true);
131
}
132
133
function closeModal() {
134
	var section = $('.cd-section.modal-is-visible');
135
	section.removeClass('modal-is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
136
		animateLayer($('.cd-modal-bg.is-visible'), 1, false);
137
		$(this).off();
138
	});
139
	//if browser doesn't support transitions...
140
	if($('.no-csstransitions').length > 0 ) animateLayer($('.cd-modal-bg.is-visible'), 1, false);
141
	window.setTimeout(function() { removeComponents() }, 400);
142
}