-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (69 loc) · 2.53 KB
/
script.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
var tabs = $(".tabs");
var items = $(".tabs").find("a").length;
var selector = $(".tabs").find(".selector");
var activeItem = tabs.find(".active");
var activeWidth = activeItem.innerWidth();
$(".selector").css({
left: activeItem.position.left + "px",
width: activeWidth + "px"
});
$(".tabs").on("click", "a", function() {
$(".tabs a").removeClass("active");
$(this).addClass("active");
var activeWidth = $(this).innerWidth();
var itemPos = $(this).position();
$(".selector").css({
left: itemPos.left + "px",
width: activeWidth + "px"
});
});
var getaudio = $("#player")[0];
/* Get the audio from the player (using the player's ID), the [0] is necessary */
var mouseovertimer;
/* Global variable for a timer. When the mouse is hovered over the speaker it will start playing after hovering for 1 second, if less than 1 second it won't play (incase you accidentally hover over the speaker) */
var audiostatus = "off";
/* Global variable for the audio's status (off or on). It's a bit crude but it works for determining the status. */
$(document).on("mouseenter", ".speaker", function() {
/* Bonus feature, if the mouse hovers over the speaker image for more than 1 second the audio will start playing */
if (!mouseovertimer) {
mouseovertimer = window.setTimeout(function() {
mouseovertimer = null;
if (!$(".speaker").hasClass("speakerplay")) {
getaudio.load();
/* Loads the audio */
getaudio.play();
/* Play the audio (starting at the beginning of the track) */
$(".speaker").addClass("speakerplay");
return false;
}
}, 1000);
}
});
$(document).on("mouseleave", ".speaker", function() {
/* If the mouse stops hovering on the image (leaves the image) clear the timer, reset back to 0 */
if (mouseovertimer) {
window.clearTimeout(mouseovertimer);
mouseovertimer = null;
}
});
$(document).on("click touchend", ".speaker", function() {
/* Touchend is necessary for mobile devices, click alone won't work */
if (!$(".speaker").hasClass("speakerplay")) {
if (audiostatus == "off") {
$(".speaker").addClass("speakerplay");
getaudio.load();
getaudio.play();
window.clearTimeout(mouseovertimer);
audiostatus = "on";
return false;
} else if (audiostatus == "on") {
$(".speaker").addClass("speakerplay");
getaudio.play();
}
} else if ($(".speaker").hasClass("speakerplay")) {
getaudio.pause();
$(".speaker").removeClass("speakerplay");
window.clearTimeout(mouseovertimer);
audiostatus = "on";
}
});