(function(window, undefined) {
// 获取元素css属性值
function getCss(elem, attr) {
return elem.currentStyle ?
elem.currentStyle[attr] :
window.getComputedStyle(elem, null)[attr];
}
// 去除字符串中的非数字,不包括负号
function toInt(str) {
var rex = /[^0-9]/ig;
return Number((str[0] === '-' && str[1] !== '=') ? '-' + str.replace(rex, '') : str.replace(rex, ''));
}
// 封装动画函数,参数:dom对象、css属性值对象、动画执行时间、动画完成后回调
function animation(elem, params, speed, callback) {
for (var param in params) {
(function(param) {
var elemValue = toInt(getCss(elem, param)),
targetValue = toInt(params[param]),
currentDis = elemValue,
unit = params[param].substr(params[param].indexOf('[A-Za-z]+') - 1);
if (params[param].length > 2) {
var prefix = params[param].substr(0, 2);
if (prefix === '+=')
targetValue = elemValue + targetValue;
else if (prefix === '-=')
targetValue = elemValue - targetValue;
}
var dis = (targetValue - elemValue) / speed,
sizeFlag = targetValue < elemValue;
var timer = setInterval(function() {
elemValue = toInt(getCss(elem, param));
if (sizeFlag) {
if (currentDis <= targetValue) {
clearInterval(timer);
elem.style[param] = targetValue + unit;
} else {
currentDis += dis;
elem.style[param] = currentDis + unit;
}
}
else {
if (currentDis >= targetValue) {
clearInterval(timer);
elem.style[param] = targetValue + unit;
} else {
currentDis += dis;
elem.style[param] = currentDis + unit;
}
}
}, 1);
})(param);
}
if (typeof callback === 'function')
callback();
};
// 向右轮播数组移动
function rightRoundArrayMove() {
var winsLen = wins.length;
var lastWin = wins[winsLen - 1];
for (var i = winsLen - 1; i > 0; i--)
wins[i] = wins[i - 1];
wins[0] = lastWin;
}
// 向左轮播
function rightRound(time) {
rightRoundArrayMove();
wins.forEach(function(win, index) {
(index === 0) ?
win.style.left = index * winWidth - winWidth + 'px' :
animation(win, {left: '+=' + winWidth + 'px'}, time ? time : animationTime);
});
}
// 向右轮播
function leftRound(time) {
var winsLen = wins.length;
var firstWin = wins[0];
for (var i = 0; i < winsLen - 1; i++)
wins[i] = wins[i + 1];
wins[winsLen - 1] = firstWin;
wins.forEach(function(win, index) {
(index === wins.length - 1) ?
win.style.left = index * winWidth - winWidth + 'px' :
animation(win, {left: '-=' + winWidth + 'px'}, time ? time : animationTime);
});
}
var
// wins, btns, sbtns用于保存构造函数的参数
wins,
btns,
sbtns,
// 窗口的宽度
winWidth,
// 过渡动画时间(毫秒),默认为100
animationTime = 100,
// 点击按钮轮播间隔
clickInterval = animationTime << 2,
// 保存自动轮播定时器、定时器间隔、是否向右轮播
autoRoundTimer,
qinterval,
qisRight,
// slide构造函数,参数:窗口数组,按钮数组,侧边按钮数组
slide = function(wins, btns, sbtns) {
return new slide.prototype.init(wins, btns, sbtns);
};
slide.prototype = {
// 初始化窗口元素
init: function(awins, abtns, asbtns) {
if (!awins)
throw new Error('The window array cannot be empty.');
wins = Object.values(awins), btns = abtns, sbtns = asbtns;
// 处理窗口少于3个的情况
if (wins.length === 1) {
var winParent = wins[0].parentNode;
var winHTML = wins[0].outerHTML;
winParent.innerHTML += winHTML + winHTML;
wins = Object.values(winParent.children);
}
else if (wins.length === 2) {
var winParent = wins[0].parentNode;
winParent.innerHTML += wins[0].outerHTML + wins[1].outerHTML;
wins = Object.values(winParent.children);
}
winWidth = wins[0].offsetWidth;
wins.forEach(function(win, index) {
win.style.position = 'absolute';
win.index = index;
});
rightRoundArrayMove();
wins.forEach(function(win, index) {
win.style.left = index * winWidth - winWidth + 'px';
});
},
// 设置过渡动画时间
setAnimationTime: function(time) {
animationTime = time;
clickInterval = animationTime << 2;
},
// 自动轮播,参数:轮播时间间隔、是否为向右轮播
autoRound: function(interval, isRight) {
autoRoundTimer = setInterval(function() {
isRight ? rightRound() : leftRound();
}, interval);
qinterval = interval;
qisRight = isRight;
},
// 侧边按钮点击,参数为侧边按钮元素数组,该参数可在构造函数中传递或现在传递
sideBtnClickRound: function(sabtns) {
var leftBtn = sabtns ? sabtns[0] : sbtns[0],
rightBtn = sabtns ? sabtns[1] : sbtns[1];
var isclick= true;
leftBtn.onclick = function () {
if(isclick) {
isclick= false;
rightRound();
setTimeout(function() {
isclick = true;
}, clickInterval);
}
};
rightBtn.onclick = function () {
if(isclick) {
isclick= false;
leftRound();
setTimeout(function() {
isclick = true;
}, clickInterval);
}
};
},
// 普通按钮点击,参数:普通按钮数组、回调
btnsClickRound: function(abtns, callback) {
var ibtns = abtns ? abtns : btns;
var isclick= true;
ibtns.forEach(function(btn, index) {
btn.onclick = function() {
if(isclick) {
isclick= false;
if (typeof callback === 'function')
callback(ibtns, btn, index);
var poor = index - wins[1].index;
var count = Math.abs(poor);
if (poor < 0) {
var absPoor = count;
var timer = setInterval(function() {
console.log((absPoor + 1))
rightRound(animationTime / (absPoor + 2));
if ((--count) === 0)
clearInterval(timer);
}, animationTime);
}
else if (poor > 0) {
var timer = setInterval(function() {
leftRound(animationTime / (poor + 2));
if ((--count) === 0)
clearInterval(timer);
}, animationTime);
}
setTimeout(function() {
isclick = true;
}, clickInterval << 1);
}
}
});
},
// 设置鼠标移入取消自动轮播,参数:移入的元素、移入元素回调、移出元素回调
setOverStop: function(box, overCallback, outCallback) {
box.onmouseover = function(e) {
clearInterval(autoRoundTimer);
if (typeof overCallback === 'function')
overCallback(e);
}
box.onmouseout = function(e) {
slide.prototype.autoRound(qinterval, qisRight);
if (typeof outCallback === 'function')
outCallback(e);
}
}
}
slide.prototype.init.prototype = slide.prototype;
window.slide = _slide = slide;
})(window);
|