-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarious-js-snippets.js
153 lines (139 loc) · 4.35 KB
/
various-js-snippets.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/********************************************************/
// Edit the client/browser view
/********************************************************/
javascript: document.body.contentEditable = true;
void 0;
// credits: https://nickjanetakis.com/blog/temporarily-edit-text-on-any-website
/********************************************************/
// CF7 focus form labels effect
/********************************************************/
// Add a .focused class to style with an animation class
jQuery(document).ready(function ($) {
// contact form focus
(function () {
var inputs = $(".wpcf7-form label");
inputs.each(function (index, el) {
var input = $(this).find("input, textarea");
var label = $(this).find(".label");
input.on("focus", function (event) {
// event.preventDefault();
label.addClass("focused");
});
input.on("blur", function (event) {
// event.preventDefault();
var val = $(this).val();
if (val == "") {
label.removeClass("focused");
}
});
});
})();
}); // end CF7 focus effect
// credits: https://mikelaroy.ca/wp_site/wp-content/themes/mikelaroy-2016/dist/assets/js/app.js
// css: CF7 labels animation
/*
label .label {
display: inline-block;
position: relative;
transform: translateY(140%) translateX(12%);
transition-delay: 0s;
transition-duration: 0.25s;
transition-property: transform;
transition-timing-function: ease;
z-index: 50;
}
label .label.focused {
color: var(--dark-blue-03-color);
transform: translateY(0px) translateX(0px);
}
*/
/********************************************************/
// Add Smooth Scroll effect via js animate()
/********************************************************/
jQuery(document).ready(function ($) {
// Smooth Scroll
$(function () {
$("a[href*=#]:not([href=#])").click(function () {
if (
location.pathname.replace(/^\//, "") ==
this.pathname.replace(/^\//, "") &&
location.hostname == this.hostname
) {
var target = $(this.hash);
target = target.length
? target
: $("[name=" + this.hash.slice(1) + "]");
if (target.length) {
$("html,body").animate(
{
// 205 is a fixed header offset
scrollTop: target.offset().top - 205,
},
1000
);
return false;
}
}
});
});
}); // end Smooth Scroll
// source: https://nvision.co/development/smooth-scroll-simple-javascript/
// credits: https://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
/********************************************************/
// Open External Links In New Tab
/********************************************************/\
// jQuey version
jQuery(document).ready(function ($) {
$("a").each(function () {
var a = new RegExp("/" + window.location.host + "/");
if (!a.test(this.href)) {
$(this).click(function (event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, "_blank");
// console.log("external-link");
});
}
});
}); // end jQuey version
// vanilla JavaScript version
var links = document.links;
for (var i = 0, linksLength = links.length; i < linksLength; i++) {
var hrefValue = links[i].href;
if (
links[i].hostname != window.location.hostname &&
links[i].firstChild &&
links[i].firstChild.nodeName != "IMG" &&
!links[i].href.startsWith("tel:") &&
!links[i].href.startsWith("mailto:")
) {
links[i].target = '_blank';
links[i].rel = 'noopener';
links[i].title = hrefValue;
links[i].classList.add('debug', 'external-link');
// console.log('external-link');
}
} // end vanilla JavaScript version
// ADDING EXTERNAL LINK or PDF ICON via CSS
// https://developer.wordpress.org/resource/dashicons/#external
// https://developer.wordpress.org/resource/dashicons/#pdf
// sass styles:
// a {
// // external links
// &.external-link {
// &::after {
// font-family: "Dashicons";
// content: "\f504"; /* external */
// width: 11px;
// height: 11px;
// }
// }
// // pdf links/attachments
// &[href$=".pdf"] {
// &::after {
// font-family: "Dashicons";
// content: "\f190"; /* pdf */
// }
// }
// }
// end Open External Links In New Tab