排序:
// 封装函数 重复使用sort方法
// 降序
function mySort(key){
data.sort(function(prev,next){
return next - prev;
})
return data;
}
// 升序
function mySort(key){
data.sort(function(prev,next){
return prev - next;
})
return data;
}
显示和隐藏
var show = function(dom) {
dom.style["display"] = "block";
}
// 隐藏标签
var hide = function(dom) {
dom.style["display"] = "none";
}
随机数:
Snake.prototype.myRandom = function(min,max){
// 指定范围的随机数
return Math.floor(Math.random()*(max-min)+min);
}
轮播图动画:
// 封装函数
function animate(dom,prop,value,callback){
// 清除定时器 (防抖思想)
clearInterval(dom.timer);
// 执行定时器
dom.timer = setInterval(function(){
// 记录目标属性值
var target = parseInt(value);
// 获取当前的属性值
var cur = parseInt(getComputedStyle(dom)[prop]);
// 记录步长
// var speed = 10;
var speed = (target - cur) / 10;
// 步长在运算过程中,产生小数点 当步长为0.3时 需要取值为1 (向上取整); 当步长为-0.4 ,需要为-1(向下取整)
// 让步长接近等于 1 或者 等于-1
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
// 判断当前属性值是否达到目标位置
if(cur == target){
// console.log("当前属性值: "+cur);
clearInterval(dom.timer);
// 是否符合条件
if(typeof callback == "function"){
// 调用回调函数
callback();
}
return ;
}
// 设置dom的样式
dom.style[prop] = cur + speed +"px";
},100)
}