Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lights by Katerina Boronina #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions lights.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#lights {
width: 200px;
margin: 100px auto;
height: 400px;
box-sizing: border-box;
border: 1px solid #ddd;
background: #eeeeee;
z-index: 10;
}

.light {
width: 100px;
height: 100px;
border-radius: 50%;
border: 1px solid #aaaaaa;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Все эти штуки умещаются в aaa

background: #bbbbbb;
background: -moz-radial-gradient(50% 25%, circle, #fefefe 0%, #bbbbbb 100%);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Префиксы уже не нужны http://caniuse.com/#feat=css-gradients

background: -webkit-radial-gradient(50% 25%, circle, #fefefe 0%, #bbbbbb 100%);
background: -o-radial-gradient(50% 25%, circle, #fefefe 0%, #bbbbbb 100%);
background: -ms-radial-gradient(50% 25%, circle, #fefefe 0%, #bbbbbb 100%);
background: radial-gradient(50% 25%, circle, #fefefe 0%, #bbbbbb 100%);
margin: 25px auto;
box-sizing: border-box;
}

.light#ylw {
top: 200px;
}

.light#grn {
top: 300px;
}

#lights.red > .light#red {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем нужен именно непосредственный ребёнок?

background: #ff2020;
background: -moz-radial-gradient(50% 25%, circle, #fefefe 0%, #ff2020 100%);
background: -webkit-radial-gradient(50% 25%, circle, #fefefe 0%, #ff2020 100%);
background: -o-radial-gradient(50% 25%, circle, #fefefe 0%, #ff2020 100%);
background: -ms-radial-gradient(50% 25%, circle, #fefefe 0%, #ff2020 100%);
background: radial-gradient(50% 25%, circle, #fefefe 0%, #ff2020 100%);
}

#lights.yellow > .light#ylw {
background: #f7df10;
background: -moz-radial-gradient(50% 25%, circle, #fefefe 0%, #f7df10 100%);
background: -webkit-radial-gradient(50% 25%, circle, #fefefe 0%, #f7df10 100%);
background: -o-radial-gradient(50% 25%, circle, #fefefe 0%, #f7df10 100%);
background: -ms-radial-gradient(50% 25%, circle, #fefefe 0%, #f7df10 100%);
background: radial-gradient(50% 25%, circle, #fefefe 0%, #f7df10 100%);
}

#lights.green > .light#grn {
background: #08a008;
background: -moz-radial-gradient(50% 25%, circle, #fefefe 0%, #08a008 100%);
background: -webkit-radial-gradient(50% 25%, circle, #fefefe 0%, #08a008 100%);
background: -o-radial-gradient(50% 25%, circle, #fefefe 0%, #08a008 100%);
background: -ms-radial-gradient(50% 25%, circle, #fefefe 0%, #08a008 100%);
background: radial-gradient(50% 25%, circle, #fefefe 0%, #08a008 100%);
}

.tram {
width: 200px;
height: 135px;
/*background: url("tram.png");
background-size: cover;*/
position: absolute;
top: 365px;
left: 0px;
z-index: 1;
cursor: pointer;
}

.tram.leaves {
opacity: 0;
transition: opacity 3s ease-in 0.5s;
-webkit-transition: opacity 3s ease-in 0.5s;
-o-transition: opacity 3s ease-in 0.5s;
-moz-transition: opacity 3s ease-in 0.5s;;

}
87 changes: 87 additions & 0 deletions lights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Object.prototype.on = function (eventName, handler) {
if (!this._handlers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мы обычно пишем так:
this._handlers = this._handlers || [], но не принципиально

this._handlers = [];
if (!this._handlers[eventName])
this._handlers[eventName] = handler;
};

Object.prototype.off = function (eventName) {
if (!this._handlers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь попахивает оверхедом, и копипастой

this._handlers = [];
if (!this._handlers[eventName])
return;
this._handlers[eventName] = null;
};

Object.prototype.trigger = function (eventName) {
if (!this._handlers)
this._handlers = [];
if (!this._handlers[eventName])
return;
this._handlers[eventName].call(arguments, 1);
};

function Lights(config) {
this.state = null;
this._timeout = null;
this._config = config;
this._elem = document.getElementById(config.blockId);

this.toRed = function (time) {
if (this._timeout)
clearTimeout(this._timeout);
this.state = "red";
this._elem.className = this.state;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._timeout = setTimeout(function () {
this.toYellow();
}.bind(this), (time ? time : this._config.red) * 1000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 за bind!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 за time ? time, тут тоже можно написать (time || this._config.red)

};

this.toYellow = function (time) {
if (this._timeout)
clearTimeout(this._timeout);
this.state = "yellow";
this._elem.className = this.state;
this._timeout = setTimeout(function () {
this.toGreen();
}.bind(this), (time ? time : this._config.yellow) * 1000);
};

this.toGreen = function (time) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По сути все три метода – копипаста. Это печалит :(

if (this._timeout)
clearTimeout(this._timeout);
this.state = "green";
this._elem.className = this.state;
this._timeout = setTimeout(function () {
this.toRed();
}.bind(this), (time ? time : this._config.green) * 1000);
};

this.start = function () {
if (this._timeout)
clearTimeout(this._timeout);
this.toRed();
};
}

function tramAnimation(v, delay, duration, to, durationLeft) {
duration -= delay;
var x0 = document.getElementsByClassName("tram")[0].getBoundingClientRect().left;
document.getElementsByClassName("tram")[0].style.left = (x0 + v*1) + "px";
if (duration > 0)
setTimeout(function() {
tramAnimation(v, delay, duration, to);
}, delay);
else
if (to) {
document.getElementsByClassName("tram")[0].className += " leaves"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а тут classList прямо напрашивается

setTimeout(function() {
tramAnimation(v, delay, 4000, false);
}, delay);
}
else {
document.getElementsByClassName("tram")[0].className = "tram";
document.getElementsByClassName("tram")[0].style.left = "0px";
document.getElementsByClassName("tram")[0].onclick = function () { lights.trigger('tram'); };
}
}
38 changes: 38 additions & 0 deletions svetofor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<title>����������� � ����������</title>
<link rel="stylesheet" type="text/css" href="lights.css">
<script type="text/javascript" src="lights.js"></script>
</head>
<body>
<img class="tram" onclick="lights.trigger('tram')" src="tram.png" />
<div id="lights">
<div class="light" id="red"></div>
<div class="light" id="ylw"></div>
<div class="light" id="grn"></div>
</div>
<script type="text/javascript">
var config = {
blockId: 'lights',
red: 2,
yellow: 0.3,
green: 1
};

var lights = new Lights(config);
lights.on("tram", function() {
document.getElementsByClassName("tram")[0].onclick = function() {};
var duration = 3000;
var s = (window.innerWidth - 600) / 2;
var delay = 30;
var v = (s / (duration / delay)).toFixed();
setTimeout(function () {
lights.toGreen(10);
}, duration);
tramAnimation(v, delay, duration, true);
});
lights.start();
</script>
</body>
</html>