$.w.ltAnimate = function (el, props, opts, cb) {});
var self = this, id = new Date().getTime(); // id self._debug = true; // if ((typeof el == "string") && el) el = this.ltElem(el); if ((typeof el != "object") || !el || (typeof el.nodeType != "number") || (el.nodeType > 1)) { doFail(" "); return; } // opts switch (typeof opts) { case "number": opts = {duration: opts}; break; case "function": opts = {cbDone: opts}; break; case "object": if (!opts) opts = {}; break; default: opts = {}; } if (typeof cb == "function") opts.cbDone = cb; // var defaultOptions = { tick : 30, // , duration : 1000, // easing : 'linear', // cbDone : function() { // if (self._debug) $.w.console.log(" [id: " + id + "] "); }, cbFail : function() { // if (self._debug) $.w.console.log(" [id: " + id + "] "); } }
// , var instructions = []; for (var key in props) { if (!props.hasOwnProperty(key)) continue; instructions.push([key, props[key]]); } // , if (instructions.length === 0) { doFail(" , "); return; }
// var optionsList = [], easing = {linear: 1, swing:1, quad:1, cubic:1}; for (var key in opts) { if (!opts.hasOwnProperty(key)) continue; switch (key) { case "duration": if (typeof opts[key] != "number") { $.w.console.log("ltAnimate(): ! . "); continue; } break; case "easing": if (typeof easing[opts[key]] == "undefined") { $.w.console.log("ltAnimate(): ! easing. "); continue; } break; case "cbDone": case "cbFail": if (typeof opts[key] != "function") { $.w.console.log("ltAnimate(): ! !"); continue; } break; default: $.w.console.log("ltAnimate(): ! !"); continue; } optionsList.push([key, opts[key]]) } // options defaultOptions var options = defaultOptions; if (optionsList.length) { for (var i=0; i < optionsList.length; i++) { if (optionsList[i][0] == 'duration') options.duration = optionsList[i][1]; if (optionsList[i][0] == 'easing') options.easing = optionsList[i][1]; if (optionsList[i][0] == 'cbDone') options.cbDone = optionsList[i][1]; if (optionsList[i][0] == 'cbFail') options.cbFail = optionsList[i][1]; } }
// , var startParams = {};
// if (el.ltAnimateQueue && el.ltAnimateQueue.length > 0) { // , ( , .. ) var animateEnds = 1, timeNow = new Date().getTime(); for (var i=0; i < el.ltAnimateQueue.length; i++) { if (i == 0) { animateEnds = el.ltAnimateQueue[i][1] - timeNow + el.ltAnimateQueue[i][0]; } else { animateEnds += el.ltAnimateQueue[i][1]; } } // el.ltAnimateQueue.push([timeNow + animateEnds, options.duration]); // , var thisTimeout = $.w.setTimeout(function(){ checkAnimation(); }, animateEnds); // , , ltAnimateStop if (!el.ltAnimateTimeouts) { el.ltAnimateTimeouts = []; } el.ltAnimateTimeouts.push(thisTimeout); // } else { // , el.ltAnimateQueue = [[new Date().getTime(), options.duration]]; startAnimation(); } // , function checkAnimation() { // , if (!el.ltAnimateIsDoing) { startAnimation(); } else { // , function _check() { if (!el.ltAnimateIsDoing) { $.w.clearInterval(_checking); startAnimation(); } } var _checking = $.w.setInterval(_check, 30); } }
function startAnimation() { // el.ltAnimateIsDoing = true; // var startStyles = self.ltStyle(el); // startParams.left = parseInt(startStyles.left); startParams.right = parseInt(startStyles.right); startParams.top = parseInt(startStyles.top) + 0.01; startParams.bottom = parseInt(startStyles.bottom) - 0.01; startParams.width = parseInt(startStyles.width); startParams.height = parseInt(startStyles.height); startParams.opacity = parseFloat(startStyles.opacity); startParams.marginTop = parseInt(startStyles.marginTop); startParams.marginBottom = parseInt(startStyles.marginBottom); startParams.marginLeft = parseInt(startStyles.marginLeft); startParams.marginRight = parseInt(startStyles.marginRight); startParams.parentWidth = parseInt(self.ltStyle(el.parentNode).width); startParams.parentHeight = parseInt(self.ltStyle(el.parentNode).height); // Chrome IE for (key in startParams) { if (key == 'left' && !startParams[key]) { startParams.left = startParams.parentWidth - startParams.right - startParams.width || 0; } if (key == 'right' && !startParams[key]) { startParams.right = startParams.parentWidth - startParams.left - startParams.width || 0; } if (key == 'bottom' && !startParams[key]) { startParams.bottom = startParams.parentHeight - startParams.top - startParams.height || 0; } if (key == 'top' && !startParams[key]) { startParams.top = startParams.parentHeight - startParams.bottom - startParams.height || 0; } } // el.currentAnimation = new doAnimation({ element : el, delay : defaultOptions.delay }); }
/** * ( el) ( styleName ). * opts - , computed true. , , false - . * (, div) , , . * IE8 %, auto, thin/medium/thick . * Opacity IE8 ( 0 1) * * @param {DOM} el - * @param {string} style - , * @param {Object} opts - * * @returns {(number|string)} value - */ $.w.ltStyle = function(el, styleName, opts) { if (!opts || typeof opts != 'object' || typeof opts.computed != 'boolean') opts = {computed : true}; if (typeof el == 'string') el = this.ltElem(el); // (NodeList), if (!el || !el.nodeType || (el.nodeType != 1)) return ''; var _style; // IE8 getComputedStyle currentStyle if (!$.w.getComputedStyle) { var __style = el.currentStyle, _style = {}; for (var i in __style) { _style[i] = __style[i]; } // , IE8 : pixelLeft, pixelRight - , var pixel = { left: 1, right: 1, width: 1, height: 1, top: 1, bottom: 1 }; // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 var other = { paddingLeft: 1, paddingRight: 1, paddingTop: 1, paddingBottom: 1, marginLeft: 1, marginRight: 1, marginTop: 1, marginBottom: 1 }; var leftCopy = el.style.left; var runtimeLeftCopy = el.runtimeStyle.left; // if (!styleName) { // IE8 , for (c in _style) { if (!_style.hasOwnProperty(c)) continue; if (c.indexOf("border") !== 0) continue; switch (_style[c]) { case "thin": _style[c] = 2; break; case "medium": _style[c] = 4; break; case "thick": _style[c] = 6; break; default: _style[c] = 0; } } //pixel for (var key in pixel) { _style[key] = el.style["pixel" + key.charAt(0).toUpperCase() + key.replace(key.charAt(0), "")]; } // getComputedStyle for (var key in other) { el.runtimeStyle.left = el.currentStyle.left; el.style.left = _style[key]; _style[key] = el.style.pixelLeft; el.style.left = leftCopy; el.runtimeStyle.left = runtimeLeftCopy; } // } else { if (_style[styleName]) { if (style.indexOf("border") === 0) switch (_style[styleName]) { case "thin": _style[styleName] = 2; break; case "medium": _style[styleName] = 4; break; case "thick": _style[styleName] = 6; break; default: _style[styleName] = 0; } } else { if (pixel[styleName]) { _style[styleName] = el.style["pixel" + key.charAt(0).toUpperCase() + key.replace(key.charAt(0), "")]; } else { el.runtimeStyle.left = el.currentStyle.left; el.style.left = _style[styleName]; _style[styleName] = el.style.pixelLeft; el.style.left = leftCopy; el.runtimeStyle.left = runtimeLeftCopy; } } } // opacity IE8 if (_style.filter.match('alpha')) { _style.opacity = _style.filter.substr(14); _style.opacity = parseInt(_style.opacity.substring(0, _style.opacity.length - 1)) / 100; } else { _style.opacity = 1; } // } else { if (opts.computed) { _style = $.w.getComputedStyle(el, null); } else { _style = el.style.styleName; } } if (!styleName) { return _style || ''; } else { return _style[styleName] || ''; } };
// var val = instructions[i][1].toString(); // , val.match(/\%/) ? percent = true : percent = false; val = parseFloat(val); var x; switch (instructions[i][0]) { case 'top' : x = function(factor, val, percent) { element.style.bottom = ''; element.style.top = startParams.top - (startParams.top - (percent ? startParams.parentHeight * val / 100 : val))*factor + 'px'; }; break; case 'bottom' : x = function(factor, val, percent) { element.style.top = ''; element.style.bottom = startParams.bottom - (startParams.bottom - (percent ? (startParams.parentHeight * val / 100) : val))*factor + 'px'; }; break; case 'left' : x = function(factor, val, percent) { element.style.right = ''; element.style.left = startParams.left - (startParams.left - (percent ? (startParams.parentWidth * val / 100) : val))*factor + 'px'; }; break; case 'right' : x = function(factor, val, percent) { element.style.left = ''; element.style.right = startParams.right - (startParams.right - (percent ? (startParams.parentWidth * val / 100) : val))*factor + 'px'; }; break; case 'width' : x = function(factor, val, percent) { element.style.width = startParams.width - (startParams.width - (percent ? (startParams.width * val / 100) : val))*factor + 'px'; }; break; case 'height' : x = function(factor, val, percent) { element.style.height = startParams.height - (startParams.height - (percent ? (startParams.height * val / 100) : val))*factor + 'px'; }; break; case 'opacity' : x = function(factor, val, percent) { // IE8 if (!$.w.getComputedStyle) { element.style.filter = 'alpha(opacity=' + (startParams.opacity - (startParams.opacity - (percent ? (val / 100) : val))*factor) * 100 + ')'; } else { element.style.opacity = startParams.opacity - (startParams.opacity - (percent ? (val / 100) : val))*factor; } } break; case 'marginTop' : x = function(factor, val, percent) { element.style.marginBottom = 'auto'; element.style.marginTop = startParams.marginTop - (startParams.marginTop - (percent ? (startParams.height * val / 100) : val))*factor + 'px'; }; break; case 'marginBottom' : x = function(factor, val, percent) { element.style.marginTop = 'auto'; element.style.marginBottom = startParams.marginBottom - (startParams.marginBottom - (percent ? (startParams.height * val / 100) : val))*factor + 'px'; }; break; case 'marginLeft' : x = function(factor, val, percent) { element.style.marginRight = 'auto'; element.style.marginLeft = startParams.marginLeft - (startParams.marginLeft - (percent ? (startParams.width * val / 100) : val))*factor + 'px'; }; break; case 'marginRight' : x = function(factor, val, percent) { element.style.marginLeft = 'auto'; element.style.marginRight = startParams.marginRight - (startParams.marginRight - (percent ? (startParams.width * val / 100) : val))*factor + 'px'; } break; // , default : x = function(){}; } // exec.push([x, val, percent]); } var eLength = exec.length;
Samurai swings his sword
Like a samurai, not swinging his sword,
But only waved his sword
el.ltAnimateInterval = $.w.setInterval(function(){ _animating(); }, options.tick);
// jumpToEnd - true/false - , , // function _animating(param, jumpToEnd, callback) { counter++; // 0 1 var progress = counter / animationLength; // stopAnimation if (param == animationLength) { $.w.clearInterval(el.ltAnimateInterval); // if (jumpToEnd) _step(getProgress(1)); // el.ltAnimateQueue.splice(0, 1); // el.ltAnimateIsDoing = false; // , if (!callback) { try { options.cbDone(); } catch(e) { doFail(e); } } else { try { callback(); } catch(e) { doFail(e); } } return false; } // , if (progress > 1) { // , (progress , 0.99...) _step(getProgress(1)); $.w.clearInterval(el.ltAnimateInterval); // el.ltAnimateQueue.splice(0, 1); // el.ltAnimateIsDoing = false; try { options.cbDone(); } catch(e) { doFail(e); } return false; } _step(getProgress(progress)); }
function _step(factor) { for (var i=0; i < eLength; i++) { var s = exec[i][0], val = exec[i][1], percent = exec[i][2]; s(factor, val, percent); } }
// 0 1 var progress = counter / animationLength;
// , function getProgress(p) { switch (options.easing) { case 'linear' : return p; break; case 'swing' : return 0.5 - Math.cos(p * Math.PI ) / 2; break case 'quad' : return Math.pow(p, 2); break; case 'cubic' : return Math.pow(p, 3); break; default : return p; } }
// el.stopAnimation = function(jumpToEnd, callback) { _animating(animationLength, jumpToEnd, callback); // if (el.ltAnimateTimeouts) { for (var i=0; i < el.ltAnimateTimeouts.length; i++) { $.w.clearTimeout(el.ltAnimateTimeouts[i]) } el.ltAnimateTimeouts = []; } }
/* * : , (true/false) (true/false), */ $.w.ltAnimateStop = function(el, jumpToEnd, callback) { // , if (!el.ltAnimateInterval) return false; el.stopAnimation(jumpToEnd, callback); };
// function doFail(text) { if (self._debug._enabled) { if ((typeof text != "string") || !text) text = " [id: " + id + "] - ."; $.w.console.log("ltAnimate(): ! " + text); } if (opts.cbFail) { try { opts.cbFail(); } catch (e) { $.w.console.log("ltAnimate(): ! [id: " + id + ", " + e.name + ": " + e.message + "]"); } } }
Source: https://habr.com/ru/post/315942/
All Articles