Skip to content

Instantly share code, notes, and snippets.

View zhuzhuaicoding's full-sized avatar

zhuzhu_coder zhuzhuaicoding

View GitHub Profile
@zhuzhuaicoding
zhuzhuaicoding / NaN-Infinity-trick
Created October 5, 2012 15:35
NaN,Infinity trick
1/0 = Infinity
0/0 = NaN
1/'foo' = NaN
1/Infinity = 0
(1/Infinity)*Infinity = NaN
Infinity == Infinity (true)
0/0为NaN,其他数除0为Infinite
@zhuzhuaicoding
zhuzhuaicoding / the Property of the RegExp function
Created October 10, 2012 04:00
$1,...属性是挂在正则表达式对象RegExp对象
var myRe = /d(b+)d/g;
var myArray = myRe.exec("cdbbdbsbz");
console.log(RegExp.$1);//bb
@zhuzhuaicoding
zhuzhuaicoding / timeout diff with sleep func
Created October 14, 2012 02:25
sleep and settimeout diff
function sleep(ms) {
var dt = new Date();
dt.setTime(dt.getTime() + ms);
while (new Date().getTime() < dt.getTime());
}
// sleep(4000);
console.log('setTimeout begin');
setTimeout(
@zhuzhuaicoding
zhuzhuaicoding / scrollTop-Compatitable
Created October 17, 2012 09:04
scrollTop compatitable way
function getScrollTop(){
var position = [0, 0];
if (typeof window.pageYOffset != 'undefined') {
position = [
window.pageXOffset, window.pageYOffset];
} else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
position = [
document.documentElement.scrollLeft, document.documentElement.scrollTop];
} else if (typeof document.body.scrollTop != 'undefined') {
position = [
@zhuzhuaicoding
zhuzhuaicoding / multiple-same-events-on-elem
Created October 19, 2012 11:17
bind mulitiple click events,can all exec.
http://jsfiddle.net/UumUP/2913/
// Function to change the content of t2
function modifyText() {
var t2 = document.getElementById("t2");
t2.firstChild.nodeValue = "three";
}
// add event listener to t
var el = document.getElementById("t");
@zhuzhuaicoding
zhuzhuaicoding / position-fixed.css
Created October 27, 2012 01:18 — forked from subtleGradient/position-fixed.css
Make position:fixed work in IE6
/*Make position:fixed work in IE6!*/
.fixed-top /* position fixed Top */{position:fixed;bottom:auto;top:0;}
.fixed-bottom /* position fixed Bottom */{position:fixed;bottom:0;top:auto;}
.fixed-left /* position fixed Left */{position:fixed;right:auto;left:0;}
.fixed-right /* position fixed right */{position:fixed;right:0;left:auto;}
* html,* html body /* IE6 Fixed Position Jitter Fix */{background-image:url(about:blank);background-attachment:fixed;}
* html .fixed-top /* IE6 position fixed Top */{position:absolute;bottom:auto;top:expression(eval(document.documentElement.scrollTop));}
* html .fixed-right /* IE6 position fixed right */{position:absolute;right:auto;left:expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.marginLeft,10)||0)-(parseInt(this.currentStyle.marginRight,10)||0));}
The common practice of rounding, namely the round() function in PHP, is to round to the nearest decimal point, that is, 1.0, 2.0, 3.0, etc. or, 10, 20, 30, etc. For example,
echo round(4.25, 0); // 4
echo round(3.1415926, 2); // 3.14
echo round(299792, -3); // 300000
When I’m trying to aggregate all the customer ratings for a specific provider in one of my web hosting reviews community, I want to round the average rating to the nearest 0.5 (half the decimal) so that a half star would be correctly displayed.
This is more of a mathematical problem than a PHP one. After some thinking and testing, I came up with a slightly more sophisticated solution than but the round() function:
echo round(1.7 * 2) / 2; // 1.5
@zhuzhuaicoding
zhuzhuaicoding / getScrollXY
Created October 27, 2012 06:18
getScrollXY
function getScrollXY() {
var scrOfX = 0, scrOfY = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
//Netscape compliant
scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
//DOM compliant
scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
@zhuzhuaicoding
zhuzhuaicoding / fixed style use javascirpt
Created October 27, 2012 06:19
fixed style use javascirpt
/* It is not possible to get certain styles set in css such as display using
the normal javascript. So we have to use this function taken from:
http://www.quirksmode.org/dom/getstyles.html */
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle) // IE
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle) // FF
@zhuzhuaicoding
zhuzhuaicoding / getStyle
Created October 27, 2012 06:20
getStyle compatibility
/* It is not possible to get certain styles set in css such as display using
the normal javascript. So we have to use this function taken from:
http://www.quirksmode.org/dom/getstyles.html */
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle) // IE
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle) // FF