forked from MahdiF/loud-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloudlinks-1.0.js
133 lines (112 loc) · 4.62 KB
/
loudlinks-1.0.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
var loudlinks = (function(document) {
// Create audio element and make it awesome
var generateAudioElement = (function() {
var audioPlayer = document.createElement('audio'); // create the audio element
var source1 = document.createElement('source'); // creating a source element
var source2 = document.createElement('source');
audioPlayer.id = 'loudLinksAudioPlayer'; // give the audio element the proper id
audioPlayer.preload = true; // audio element preload attribute
source1.id = 'mp3Source';
source2.id = 'oggSource';
audioPlayer.appendChild(source1); // appending the sources to the player element
audioPlayer.appendChild(source2);
document.body.appendChild(audioPlayer); // appending the player to the body
})();
// declaring stuff ---------------------------------------------
var audioSrc;
var soundMp3Link;
var soundOggLink;
var element;
var LoudLinksHover = document.getElementsByClassName('loud-link-hover');
var LoudLinksClick = document.getElementsByClassName('loud-link-click');
var audioPlayer = document.getElementById('loudLinksAudioPlayer');
var mp3Location = 'sounds/mp3/'; // mp3 sounds location
var oggLocation = 'sounds/ogg/'; // ogg sounds location
var mp3Source = document.getElementById('mp3Source');
var oggSource = document.getElementById('oggSource');
// ------------------------------------------------------------
// check if the browser supports Audio ¯\_(ツ)_/¯
checkAudioSupport = function() {
return !!document.createElement('audio').canPlayType;
}
// get the audio source and appending it to <audio>
getAudioSource = function(element) {
var audioSrc = element.getAttribute('data-src'); // getting the sound name from the data-src Attribute
var soundMp3Link = mp3Location + audioSrc + '.mp3'; // settnig the mp3 sound in a variable
var soundOggLink = oggLocation + audioSrc + '.ogg'; // settnig the ogg sound in a variable
mp3Source.setAttribute('src', soundMp3Link); // putting the mp3 sound link in the src Attribute of <source>
oggSource.setAttribute('src', soundOggLink); // putting the mp3 sound link in the src Attribute of <source>
oggSource.addEventListener('error', function(){
console.log('😶 D\'oh! The ♪ ogg file URL is wrong!');
}, false);
mp3Source.addEventListener('error', function(){
console.log('😶 D\'oh! The ♪ mp3 file URL is wrong!');
}, false);
}
// checking if the data-src Attribute isn't empty
checkAttribute = function(element) {
var audioSrc = element.getAttribute('data-src'); // getting the sound name from the data-src Attribute
if (audioSrc) {
return true;
} else {
return false;
}
}
// Play audio
playAudio = function(element) {
if (checkAttribute(element)) { // check if the data-src Attribute is filled (there is a name of a sound file)
getAudioSource(element);
audioPlayer.load();
audioPlayer.play();
}
}
// Stop audio
stopAudio = function() {
audioPlayer.pause();
audioPlayer.currentTime = 0;
}
// track the element to a certian action
trackHover = function(element) {
element.addEventListener('mouseover', function() { // play audio on hover
playAudio(element);
}, false);
// --- all below to stop the sound ---- //
element.addEventListener('mouseout', function() { // stop audio on mouse out
stopAudio();
}, false);
element.addEventListener('touchmove', function() { // stop audio on touch and move
stopAudio();
}, false);
element.addEventListener('click', function() { // stop audio on click
stopAudio();
}, false);
element.addEventListener('dblclick', function() { // stop audio on double click
stopAudio();
}, false);
}
// track elements that'll have click event
trackClick = function(element) {
element.addEventListener('click', function() {
playAudio(element);
}, false);
}
// Go crazy! Scan all the links and see if they have the 'data-src' Attribute and do the events
runTrackers = function() {
var i;
var loudLinksHoverLength = LoudLinksHover.length;
var loudLinksClickLength = LoudLinksClick.length;
for (i = 0; i < loudLinksHoverLength; i++) { // Hover
trackHover(LoudLinksHover[i]);
}
for (i = 0; i < loudLinksClickLength; i++) { // Click
trackClick(LoudLinksClick[i]);
}
}
// Check if the browser supports audio then get crazy!
if (checkAudioSupport()) {
console.log('Audio works like a charm 👍');
runTrackers();
} else {
console.log('Oh man 😩! \nI\'m sorry but your browsers doesn\'t support awesomeness.')
}
})(document);