-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
92 lines (79 loc) · 2.6 KB
/
main.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
(function(){
"use strict"
var missions = ["Conquer Asia and South America",
"Conquer North America and Africa",
"Conquer Asia and Africa",
"Conquer North America and Australia",
"Conquer Europe and South America and a 3rd continent of your choice",
"Conquer Europe and Australia and a 3rd continent of your choice",
"Destroy the red",
"Destroy the yellow",
"Destroy the blue",
"Destroy the green",
"Destroy the grey",
"Conquer 24 territories of your choice",
"Conquer 18 territories with 2 soldier min in each ter"
];
var missionSelect =[];
// cashing the dom
var button = document.getElementById("submit");
var numPlayers = document.getElementById("numPlayers");
var formNumPlayer = document.getElementById("formNumPlayer");
var container =document.querySelector(".sub-container");
var toggleButton =document.querySelector(".btn-primary");
// initialize risk
function init(){
countPlayer();
render();
button.removeEventListener("click", buttonClick);
}
// creating dynamic html elements
function createElement(element,classme, player,idme){
var myElement = document.createElement(element);
myElement.setAttribute("class",classme);
if(idme){
myElement.setAttribute("id",idme);
}
if(player){
myElement.textContent = player;
}
return myElement;
}
// get the number of players
function countPlayer(){
var valNum = numPlayers.value;
return valNum;
}
// rendering the html elements to the dom
function render(){
var myAray = missions;
for ( var i=0; i < countPlayer() ; i++ ){
var randomNumber = (Math.floor(Math.random()*myAray.length));
var toggleButton = createElement("button","btn-primary btn animated fadeIn", "Player "+(i+1),"btn"+i);
var missionContainer = createElement("div","mission-container animated fadeIn");
toggleButton.addEventListener("click",toggleMission);
missionSelect.push(myAray[randomNumber]);
myAray.splice(randomNumber,1);
var yourMission= createElement("p","lead is-visible",missionSelect[i],"p"+i);
container.appendChild(missionContainer);
missionContainer.appendChild(toggleButton);
missionContainer.appendChild(yourMission);
}
}
function toggleMission(){
$(this).toggleClass("button-clicked")
$(this).next().toggleClass("animated bounce is-visible");
}
// adding event listeners to the toggle buttons
button.addEventListener("click",buttonClick);
function buttonClick(e){
e.preventDefault();
smoothScroll();
init();
}
function smoothScroll(){
$('html, body').animate({
scrollTop: $("#target").offset().top
}, 1000);
}
})();