Created
April 2, 2014 01:50
-
-
Save jesslilly/9926636 to your computer and use it in GitHub Desktop.
Expander animates a DIV with javascript. Exercise in less than 1 hour.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Expander = (function(){ | |
var Expander = function(id) { | |
this._id = id; | |
this._elem = elem = document.getElementById(id); | |
this._step = 0; | |
this._height = 0; | |
}; | |
Expander.prototype.expand = function(incr,to) { | |
var self = this; | |
var animate = (incr>0 && this._height<to) | |
|| (incr<0 && this._height>to); | |
if (animate) { | |
this._step=(incr*1.1); | |
this._height+=this._step; | |
this._elem.setAttribute('style','height:'+this._height+'px;'); | |
window.requestAnimationFrame(function(){self.expand(self._step,to)}); | |
} | |
}; | |
return Expander; | |
})(); | |
var e = new Expander("expand"); | |
document.getElementById('btnExpand').addEventListener('click',function(evt) { | |
e.expand(3,100); | |
}); | |
document.getElementById('btnReset').addEventListener('click',function(evt) { | |
e.expand(-3,20); | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="expand">a</div> | |
<div id="test"></div> | |
<input type="button" value="expand" id="btnExpand"/> | |
<input type="button" value="reset" id="btnReset"/> | |
<script src="expander.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#expand { | |
background-color: #BBFFBB; | |
height: 20px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment