-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemand_progress.js
More file actions
65 lines (55 loc) · 2.2 KB
/
demand_progress.js
File metadata and controls
65 lines (55 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Demand Progress - Actionkit JS,
* Author: @mvattuone
*/
/* ==================================================================
Demand Progress - Actionkit Javascript
Author: @mvattuone, Citizen Engagement Laboratory
Javascript specifically for Demand Progress Actionkit templates.
================================================================== */
/**
* Takes object, looks for form-wrap child, and binds scroll event that fixes form
* to the top of the page. Checks to make sure form doesn't go over footer.
* TODO: Test in other projects with fixed position sidebar form and refine to make it reusable across orgs.
*/
/* Modified by WAWD for DemandProgress ActionKit templates, 2/2018 */
// sticky form function
function fixFormToTop(obj) {
if ($(obj).length !== 0) {
formWrapper = $(obj).children(".ak-sticky"),
heightOfForm = $(".ak-sticky").outerHeight(),
contentHeight = $('.ak-page-container').outerHeight() + $('.ak-page-header').outerHeight(),
lastScrollTop = 0,
formDistanceFromTop = $(obj).offset().top;
$(window).scroll(function() {
var distanceToTravel = contentHeight - heightOfForm;
scrollTop = $(window).scrollTop();
if (formDistanceFromTop + heightOfForm + 40 < contentHeight){
//ensures we only do this if form area is taller than content
if (scrollTop >= formDistanceFromTop && scrollTop <= distanceToTravel) {
formWrapper.removeClass("relative").css("top","0px");
formWrapper.addClass("fixed");
} else if (scrollTop >= distanceToTravel) {
formWrapper.removeClass("fixed");
formWrapper.addClass("relative").css("top",distanceToTravel - formDistanceFromTop);
} else {
formWrapper.removeClass("fixed");
formWrapper.addClass("relative").css("top",0);
}
}
});
}
}
// move cursor to Name input
function SetFocus () {
$("html, body").animate({ scrollTop: 0 }, "fast");
var focusInput = $('form:first *:input[type!=hidden]:first');
focusInput.focus();
}
//Init
$(document).ready(function() {
$(window).load(function() {
var form = $('.ak-sticky-parent');
fixFormToTop(form);
});
});