animate is not a function(zepto 使用報錯)[轉]

animate is not a function(zepto 使用報錯)

一、爲何使用zepto寫animate報錯?javascript

由於zepto默認構建包含: Core, Ajax, Event, Form, IE幾個模塊,要使用animate須要再引用fx模塊。css

下面附上 fx模塊的連接:fxhtml

或者: 引入這個zepto.fx.js 便可!java

/** * Zepto.fx.js * * 這個功能是Zepto封裝的插件animate動畫包 * 一、 根據瀏覽器屬性獲取前綴,並設置cssReset的屬性名稱前加入前綴, * 二、$.fn.animate 的主要功能實際上是判斷並修正參數,最後調用的$.fn.anim纔是操做動畫的核心方法。 * (c) 2010-2015 Thomas Fuchs * Zepto.js may be freely distributed under the MIT license. * @param {Object} $ * @param {Object} undefined */ ;(function($, undefined) { var prefix = '', eventPrefix, // prefix瀏覽器前綴 -webkit等,eventPrefix事件前綴 vendors = { Webkit: 'webkit', Moz: '', O: 'o' }, //前綴數據源 不包含IE testEl = document.createElement('div'), //臨時DIV容器 supportedTransforms = /^((translate|rotate|scale)(X|Y|Z|3d)?|matrix(3d)?|perspective|skew(X|Y)?)$/i, //變形檢測 transform, //變形 transitionProperty, transitionDuration, transitionTiming, transitionDelay, //過渡 animationName, animationDuration, animationTiming, animationDelay, //動畫 cssReset = {} //將駝峯字符串轉成css屬性,如aB-->a-b function dasherize(str) { return str.replace(/([a-z])([A-Z])/, '$1-$2').toLowerCase() } //修正事件名 function normalizeEvent(name) { return eventPrefix ? eventPrefix + name : name.toLowerCase() } /** * 根據瀏覽器內核,設置CSS前綴,事件前綴 * 如-webkit, css:-webkit- event:webkit * 這裏會在vendors存儲webkit,moz,o三種前綴 */ $.each(vendors, function(vendor, event) { if(testEl.style[vendor + 'TransitionProperty'] !== undefined) { prefix = '-' + vendor.toLowerCase() + '-' eventPrefix = event return false } }) transform = prefix + 'transform' //變形 //過渡,對於css屬性從新設置前綴 cssReset[transitionProperty = prefix + 'transition-property'] = cssReset[transitionDuration = prefix + 'transition-duration'] = cssReset[transitionDelay = prefix + 'transition-delay'] = cssReset[transitionTiming = prefix + 'transition-timing-function'] = cssReset[animationName = prefix + 'animation-name'] = cssReset[animationDuration = prefix + 'animation-duration'] = cssReset[animationDelay = prefix + 'animation-delay'] = cssReset[animationTiming = prefix + 'animation-timing-function'] = '' /** * 動畫常量數據源,默認設置 * @type {{off: boolean, speeds: {_default: number, fast: number, slow: number}, cssPrefix: string, transitionEnd: *, animationEnd: *}} */ $.fx = { off: (eventPrefix === undefined && testEl.style.transitionProperty === undefined), //能力檢測是否支持動畫,具體檢測是否支持過渡,支持過渡事件 speeds: { _default: 400, fast: 200, slow: 600 }, cssPrefix: prefix, //css 前綴 如-webkit- transitionEnd: normalizeEvent('TransitionEnd'), //過渡結束事件 animationEnd: normalizeEvent('AnimationEnd') //動畫播放結束事件 } /** * 建立自定義動畫 * @param properties 樣式集 * @param duration 持續事件 * @param ease 速率 * @param callback 完成時的回調 * @param delay 動畫延遲 * @returns {*} */ // 這裏是對參數的修正和處理,真正操做的是anim方法 $.fn.animate = function(properties, duration, ease, callback, delay) { //參數修正,傳參爲function(properties,callback) if($.isFunction(duration)) callback = duration, ease = undefined, duration = undefined if($.isFunction(ease)) //傳參爲function(properties,duration,callback) callback = ease, ease = undefined if($.isPlainObject(duration)) //傳參爲function(properties,{}) ease = duration.easing, callback = duration.complete, delay = duration.delay, duration = duration.duration // duration 數字:持續時間 字符串:取speeds: { _default: 400, fast: 200, slow: 600 }對應數字 if(duration) duration = (typeof duration == 'number' ? duration : ($.fx.speeds[duration] || $.fx.speeds._default)) / 1000 //動畫持續時間默認值 if(delay) delay = parseFloat(delay) / 1000 //延遲時間,除以1000轉換成s return this.anim(properties, duration, ease, callback, delay) } /** * 動畫核心方法 * @param properties 樣式集 * @param duration 持續事件 * @param ease 速率 * @param callback 完成時的回調 * @param delay 動畫延遲 * @returns {*} */ $.fn.anim = function(properties, duration, ease, callback, delay) { var key, cssValues = {}, cssProperties, transforms = '', // transforms 變形 cssValues設置給DOM的樣式 that = this, wrappedCallback, endEvent = $.fx.transitionEnd, fired = false //修正持續時間 if(duration === undefined) duration = $.fx.speeds._default / 1000 if(delay === undefined) delay = 0 //若是瀏覽器不支持動畫,持續時間設爲0,直接跳動畫結束 if($.fx.off) duration = 0 // properties是動畫名 if(typeof properties == 'string') { // keyframe [animationName] = properties cssValues[animationName] = properties cssValues[animationDuration] = duration + 's' cssValues[animationDelay] = delay + 's' cssValues[animationTiming] = (ease || 'linear') endEvent = $.fx.animationEnd //動畫結束事件 } else { //properties 是樣式集 cssProperties = [] // CSS transitionsanimation cssValues for(key in properties) // supportedTransforms.test(key) 正則檢測是否爲變形 // key + '(' + properties[key] + ') '拼湊成變形方法 if(supportedTransforms.test(key)) transforms += key + '(' + properties[key] + ') ' else cssValues[key] = properties[key], cssProperties.push(dasherize(key)) console.log(transforms) // 變形統一存入 cssValues cssProperties if(transforms) cssValues[transform] = transforms, cssProperties.push(transform) // duration > 0能夠播放動畫,且properties是對象,代表爲過渡,上面有字符串,則爲animate if(duration > 0 && typeof properties === 'object') { cssValues[transitionProperty] = cssProperties.join(', ') cssValues[transitionDuration] = duration + 's' cssValues[transitionDelay] = delay + 's' cssValues[transitionTiming] = (ease || 'linear') //默認線性速率 } } //動畫完成後的響應函數 wrappedCallback = function(event) { if(typeof event !== 'undefined') { if(event.target !== event.currentTarget) return // makes sure the event didn't bubble from "below" $(event.target).unbind(endEvent, wrappedCallback) } else $(this).unbind(endEvent, wrappedCallback) // triggered by setTimeout fired = true // TODO 既然已經執行完了,爲何這裏要重複css一下,不太理解 $(this).css(cssReset) callback && callback.call(this) } //處理動畫結束事件 if(duration > 0) { //綁定動畫結束事件 this.bind(endEvent, wrappedCallback) // transitionEnd is not always firing on older Android phones // so make sure it gets fired //延時ms後執行動畫,注意這裏加了25ms,保持endEvent,動畫先執行完。 //綁定過事件還作延時處理,是transitionEnd在older Android phones不必定觸發 setTimeout(function() { //若是觸發過,就不處理 if(fired) return wrappedCallback.call(that) }, ((duration + delay) * 1000) + 25) } // trigger page reflow so new elements can animate //主動觸發頁面迴流,刷新DOM,讓接下來設置的動畫能夠正確播放 //更改 offsetTop、offsetLeft、 offsetWidth、offsetHeight;scrollTop、scrollLeft、scrollWidth、scrollHeight;clientTop、clientLeft、clientWidth、clientHeight;getComputedStyle() 、currentStyle()。這些都會觸發迴流。迴流致使DOM從新渲染,平時要儘量避免,但這裏,爲了動畫即時生效播放,則主動觸發迴流,刷新DOM。 // 與.length屬性一致 this.size() && this.get(0).clientLeft //設置樣式,啓動動畫 this.css(cssValues) // duration爲0,即瀏覽器不支持動畫的狀況,直接執行動畫結束,執行回調。 if(duration <= 0) setTimeout(function() { that.each(function() { wrappedCallback.call(this) }) }, 0) return this; } testEl = null //去掉沒必要要的數據存儲,便於垃圾回收 })(Zepto) 

 

 


二、若是要使用 show、hide、fadeIn、fadeOut、等web

須要引用:zepto.fn.js瀏覽器

/** * 使用show()、hide()、fadeIn()、fadeOut()等 * Zepto.js * (c) 2010-2016 Thomas Fuchs * Zepto.js may be freely distributed under the MIT license. * @param {Object} $ * @param {Object} undefined */ ;(function($, undefined) { var document = window.document, docElem = document.documentElement, origShow = $.fn.show, origHide = $.fn.hide, origToggle = $.fn.toggle function anim(el, speed, opacity, scale, callback) { if(typeof speed == 'function' && !callback) callback = speed, speed = undefined var props = { opacity: opacity } if(scale) { props.scale = scale el.css($.fx.cssPrefix + 'transform-origin', '0 0') } return el.animate(props, speed, null, callback) } function hide(el, speed, scale, callback) { return anim(el, speed, 0, scale, function() { origHide.call($(this)) callback && callback.call(this) }) } $.fn.show = function(speed, callback) { origShow.call(this) if(speed === undefined) speed = 0 else this.css('opacity', 0) return anim(this, speed, 1, '1,1', callback) } $.fn.hide = function(speed, callback) { if(speed === undefined) return origHide.call(this) else return hide(this, speed, '0,0', callback) } $.fn.toggle = function(speed, callback) { if(speed === undefined || typeof speed == 'boolean') return origToggle.call(this, speed) else return this.each(function() { var el = $(this) el[el.css('display') == 'none' ? 'show' : 'hide'](speed, callback) }) } $.fn.fadeTo = function(speed, opacity, callback) { return anim(this, speed, opacity, null, callback) } $.fn.fadeIn = function(speed, callback) { var target = this.css('opacity') if(target > 0) this.css('opacity', 0) else target = 1 return origShow.call(this).fadeTo(speed, target, callback) } $.fn.fadeOut = function(speed, callback) { return hide(this, speed, null, callback) } $.fn.fadeToggle = function(speed, callback) { return this.each(function() { var el = $(this) el[ (el.css('opacity') == 0 || el.css('display') == 'none') ? 'fadeIn' : 'fadeOut' ](speed, callback) }) } })(Zepto);
相關文章
相關標籤/搜索