-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (78 loc) · 2.71 KB
/
app.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
console.log('O arquivo app.js foi carregado com sucesso!');
window.addEventListener('scroll', () => {
const scrollTop = window.scrollY;
const docHeight = document.body.scrollHeight - window.innerHeight;
const scrollPercent = (scrollTop / docHeight) * 100;
document.querySelector('.progress-bar').style.width = `${scrollPercent}%`;
});
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
const headerOffset = 100;
const elementPosition = target.getBoundingClientRect().top;
const offsetPosition = elementPosition - headerOffset;
window.scrollBy({
top: offsetPosition,
behavior: 'smooth'
});
});
});
class FormSubmit {
constructor(settings) {
this.settings = settings;
this.form = document.querySelector(settings.form);
this.formButton = document.querySelector(settings.button);
if (this.form) {
this.url = this.form.getAttribute("action");
}
this.sendForm = this.sendForm.bind(this);
}
displaySuccess() {
this.form.innerHTML = this.settings.success;
}
displayError() {
this.form.innerHTML = this.settings.error;
}
getFormObject() {
const formObject = {};
const fields = this.form.querySelectorAll("[name]");
fields.forEach((field) => {
formObject[field.getAttribute("name")] = field.value;
});
return formObject;
}
onSubmission(event) {
event.preventDefault();
event.target.disabled = true;
event.target.innerText = "Enviando...";
}
async sendForm(event) {
try {
this.onSubmission(event);
await fetch(this.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(this.getFormObject()),
});
this.displaySuccess();
} catch (error) {
this.displayError();
throw new Error(error);
}
}
init() {
if (this.form) this.formButton.addEventListener("click", this.sendForm);
return this;
}
}
const formSubmit = new FormSubmit({
form: "[data-form]",
button: "[data-button]",
success: "<h1 class='success'>Mensagem enviada!</h1>",
error: "<h1 class='error'>Não foi possível enviar sua mensagem.</h1>",
});
formSubmit.init();