弹出框div页面居中/遮罩
function _getposition(){ var p = {}; var scrollWidth, scrollHeight; if(window.innerHeight && window.scrollMaxY){ scrollWidth = window.innerWidth + window.scrollMaxX; scrollHeight = window.innerHeight + window.scrollMaxY; }else if(document.body.scrollHeight>document.body.offsetHeight){ scrollWidth = document.body.scrollWidth; scrollHeight = document.body.scrollHeight; }else{ scrollWidth = document.body.offsetWidth; scrollHeight = document.body.offsetHeight; } if(self.innerHeight){ if(document.documentElement.clientWidth){ p.windowWidth = document.documentElement.clientWidth; }else{ p.windowWidth = self.innerWidth; } p.windowHeight = self.innerHeight; }else if(document.documentElement && document.documentElement.clientHeight){ p.windowWidth = document.documentElement.clientWidth; p.windowHeight = document.documentElement.clientHeight; }else if(document.body){ p.windowWidth = document.body.clientWidth; p.windowHeight = document.body.clientHeight; } if(scrollWidth < p.windowWidth){ p.width = scrollWidth; }else{ p.width = p.windowWidth; } if(scrollHeight < p.windowHeight){ p.height = scrollHeight; }else{ p.height = p.windowHeight; } p.windowWidth = Math.max(p.windowWidth, scrollWidth); p.windowHeight = Math.max(p.windowHeight, scrollHeight); if(typeof(window.pageXOffset) == "number"){ p.left = window.pageXOffset; }else if(document.documentElement && document.documentElement.scrollLeft){ p.left = document.documentElement.scrollLeft; }else if(document.body){ p.left = document.body.scrollLeft; }else if(window.scrollX){ p.left = window.scrollX; } if(typeof(window.pageYOffset) == "number"){ p.top = window.pageYOffset; }else if(document.documentElement && document.documentElement.scrollTop){ p.top = document.documentElement.scrollTop; }else if(document.body){ p.top = document.body.scrollTop; }else if(window.scrollY){ p.top = window.scrollY; } return p; } var p = _getposition(); var left = p.left + ((p.width - $("#ddl_egg_msg_div").width()) / 2); var top = p.top + ((p.height - $("#ddl_egg_msg_div").height()) / 2); $("#ddl_egg_msg_div").css({left:left,top:top}); $("#coverdiv").width(p.width).height(p.windowHeight).show();//整页遮罩
div出现在页面的随机位置
var sHeight = jQuery(document.body).outerHeight(true) - 100; var sWidth = jQuery(document.body).outerWidth(true) - 100; var div_top = GetRandomNum(0,sHeight); var div_left = GetRandomNum(0,sWidth); jQuery("#div").css("position","absolute"); jQuery("#div").css("z-index",100000); jQuery("#div").css("top",div_top); jQuery("#div").css("left",div_left); function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); return(Min + Math.round(Rand * Range)); }
该算法简单使用,并发访问性能非常好。随机概率
function get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); //概率数组循环 foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum <= $proCur) { $result = $key; break; } else { $proSum -= $proCur; } } unset($proArr); return $result; }
调用
//本次抽奖的奖项信息,必须按照从大到小的顺序进行填写,id为奖次,gift为中奖信息,v为中奖概率必须是整数,num为奖品数量 //需要注意的是,该处也必须包含不中奖的信息,概率从小到大进行排序 $gift_arr = array( '0' => array('id' => 1, 'gift' => '44元购买1G/年空间', 'v' => 1, 'num' => 1), '1' => array('id' => 2, 'gift' => '55元购买1G/年空间', 'v' => 2, 'num' => 2), '2' => array('id' => 3, 'gift' => '66元购买1G/年空间', 'v' => 5, 'num' => 2), '3' => array('id' => 4, 'gift' => '77元购买1G/年空间', 'v' => 10, 'num' => 3), '4' => array('id' => 5, 'gift' => '88元购买1G/年空间', 'v' => 15, 'num' => 4), '5' => array('id' => 6, 'gift' => '99元购买1G/年空间', 'v' => 67, 'num' => 10) ); foreach ($gift_arr as $val) { $arr[$val['id']] = $val['v']; } //$rid中奖的序列号码 $rid = $this->get_rand($arr); //根据概率获取奖项id $str = $gift_arr[$rid - 1]['gift']; //中奖项
中奖到达上限后,中的奖项换成不中奖,中的奖项发完换成不中奖