Last active
November 27, 2015 04:38
-
-
Save nhtua/270289a2d5d58ab505f7 to your computer and use it in GitHub Desktop.
[javascript] The common functions for js
This file contains 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
/** | |
* This function check an element is scrolled into view area on screen. | |
* @param elem; use CSS Selector syntax to specific an element. | |
* @return boolean; | |
* @author Scott Dowding | |
* @refer http://stackoverflow.com/questions/487073 | |
*/ | |
function isScrolledIntoView(elem) | |
{ | |
var $elem = $(elem); | |
var $window = $(window); | |
var docViewTop = $window.scrollTop(); | |
var docViewBottom = docViewTop + $window.height(); | |
var elemTop = $elem.offset().top; | |
var elemBottom = elemTop + $elem.height(); | |
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); | |
} | |
/** | |
* Generate random string. | |
* @param Integer len; Length of string | |
* @return String; | |
* @author Roger Knapp | |
* @refer http://stackoverflow.com/a/1349426/1235074 | |
*/ | |
function random(len) | |
{ | |
var text = ""; | |
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
for( var i=0; i < len; i++ ) | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
return text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment