为Zepto添加Slide动画效果
一.緣由
? ? ? 公司的移動(dòng)端項(xiàng)目,采用zepto為主要框架,但是zepto畢竟是精簡(jiǎn)版的jquery,體積小了,功能自然沒(méi)有這么強(qiáng)大,特別是動(dòng)畫和選擇器這兩塊,需要我們自己去拓展。
? ? ? 在項(xiàng)目開發(fā)過(guò)程中,很多頁(yè)面過(guò)渡需要用到動(dòng)畫,簡(jiǎn)單的show/hide過(guò)渡太生硬,對(duì)用戶不友好,并且移動(dòng)端大多都是采用slide效果,此文主要是為zepto拓展slide動(dòng)畫。
?
二.發(fā)現(xiàn)
? ? ? 從zepto的在線文檔上可以發(fā)現(xiàn)一個(gè)發(fā)布在github上的動(dòng)畫模塊,但是缺少slide效果,度娘上找了找,相關(guān)的極少,只發(fā)現(xiàn)了一個(gè)slideDown的例子,所以我們就照著這個(gè)例子寫了幾個(gè)常用的slide動(dòng)畫。
?
三.行動(dòng)
? ? ? ?首先,我們把動(dòng)畫方向分為上下滑動(dòng)和左右滑動(dòng),滑動(dòng)形式分為元素自身的伸縮和相對(duì)位移?
? ? ? ?1.左右slide
? ? ? ?這應(yīng)該是最常用的一個(gè)效果
? ? ? ?貼上代碼:注釋部分二選一, slideLeft 和 slideRight 的滑動(dòng)形式必須設(shè)為一致,不然無(wú)法工作
?
$.fn.slideLeft = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'hidden'
});
//獲取元素寬度
var width = this.width();
//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// top: 0,
// position: position,
// visibility: 'visible',
// overflow: 'auto'
//}).animate({ width: 0 }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
top: 0,
position: position,
visibility: 'visible',
overflow: 'auto'
}).animate({ left: -width }, speed, null, callback);
};
$.fn.slideRight = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'hidden'
});
//獲取元素寬度
var width = this.width() === 0 ? $(window).width() : this.width();
//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// top: 0,
// width: 0,
// position: position,
// visibility: 'visible',
// overflow: 'auto'
//}).animate({ width: width }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
top: 0,
left: -width,
position: position,
visibility: 'visible',
overflow: 'auto',
}).animate({ left: 0 }, speed, null, callback);
};
$.fn.slideLeft = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'hidden'});//獲取元素寬度var width = this.width();//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// top: 0,// position: position,// visibility: 'visible',// overflow: 'auto'//}).animate({ width: 0 }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({top: 0,position: position,visibility: 'visible',overflow: 'auto'}).animate({ left: -width }, speed, null, callback);};$.fn.slideRight = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'hidden'});//獲取元素寬度var width = this.width() === 0 ? $(window).width() : this.width();//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// top: 0,// width: 0,// position: position,// visibility: 'visible',// overflow: 'auto'//}).animate({ width: width }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({top: 0,left: -width,position: position,visibility: 'visible',overflow: 'auto',}).animate({ left: 0 }, speed, null, callback);};
? ? 左右位移效果圖: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ??? ? ?
? ? 左右伸縮效果圖:
? ??
?
? ?2.上下slide
? ? ? 貼上代碼:注釋部分二選一, slideUp 和 slideDown 的滑動(dòng)形式必須設(shè)為一致,不然無(wú)法工作
? ??
$.fn.slideDown = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'hidden'
});
//獲取元素高度
var height = this.height() === 0 ? $(window).height() : this.height();
//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// position: position,
// visibility: 'visible',
// overflow: 'auto',
// height: 0
//}).animate({ height: height }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
top: -height,
left: 0,
position: position,
visibility: 'visible',
overflow: 'auto'
}).animate({ top: 0 }, speed, null, callback);
};
$.fn.slideUp = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'auto'
});
//獲取元素高度
var height = this.height();
//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// position: position,
// visibility: 'visible',
// overflow: 'hidden',
// height: height
//}).animate({ height: 0 }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
left: 0,
position: position,
visibility: 'visible',
overflow: 'auto'
}).animate({ top: -height }, speed, null, callback);
};
$.fn.slideDown = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'hidden'});//獲取元素高度var height = this.height() === 0 ? $(window).height() : this.height();//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// position: position,// visibility: 'visible',// overflow: 'auto',// height: 0//}).animate({ height: height }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({top: -height,left: 0,position: position,visibility: 'visible',overflow: 'auto'}).animate({ top: 0 }, speed, null, callback);};$.fn.slideUp = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'auto'});//獲取元素高度var height = this.height();//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// position: position,// visibility: 'visible',// overflow: 'hidden',// height: height//}).animate({ height: 0 }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({left: 0,position: position,visibility: 'visible',overflow: 'auto'}).animate({ top: -height }, speed, null, callback);};
? ? ?上下位移效果圖: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ?? ? ?
? ? 上下伸縮效果圖:
? ? ?
?
四.結(jié)束
? ? ? 這樣,我們?cè)陧?xiàng)目中就能愉快的使用動(dòng)畫了。
? ? ? 順便附上整個(gè)動(dòng)畫模塊,但在這之前必須先添加上animate模塊,因?yàn)閦epto本身是不具有animate事件的,許多模塊都是單獨(dú)分出去的,可以參考這里,代碼我們可以從github中的animate模塊復(fù)制進(jìn)去。
? ??
//animate模塊
; (function ($, undefined) {
var prefix = '', eventPrefix,
vendors = { Webkit: 'webkit', Moz: '', O: 'o' },
testEl = document.createElement('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 = {}
function dasherize(str) { return str.replace(/([A-Z])/g, '-$1').toLowerCase() }
function normalizeEvent(name) { return eventPrefix ? eventPrefix + name : name.toLowerCase() }
if (testEl.style.transform === undefined) $.each(vendors, function (vendor, event) {
if (testEl.style[vendor + 'TransitionProperty'] !== undefined) {
prefix = '-' + vendor.toLowerCase() + '-'
eventPrefix = event
return false
}
})
transform = prefix + 'transform'
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'] = ''
$.fx = {
off: (eventPrefix === undefined && testEl.style.transitionProperty === undefined),
speeds: { _default: 400, fast: 200, slow: 600 },
cssPrefix: prefix,
transitionEnd: normalizeEvent('TransitionEnd'),
animationEnd: normalizeEvent('AnimationEnd')
}
$.fn.animate = function (properties, duration, ease, callback, delay) {
if ($.isFunction(duration))
callback = duration, ease = undefined, duration = undefined
if ($.isFunction(ease))
callback = ease, ease = undefined
if ($.isPlainObject(duration))
ease = duration.easing, callback = duration.complete, delay = duration.delay, duration = duration.duration
if (duration) duration = (typeof duration == 'number' ? duration :
($.fx.speeds[duration] || $.fx.speeds._default)) / 1000
if (delay) delay = parseFloat(delay) / 1000
return this.anim(properties, duration, ease, callback, delay)
}
$.fn.anim = function (properties, duration, ease, callback, delay) {
var key, cssValues = {}, cssProperties, transforms = '',
that = this, wrappedCallback, endEvent = $.fx.transitionEnd,
fired = false
if (duration === undefined) duration = $.fx.speeds._default / 1000
if (delay === undefined) delay = 0
if ($.fx.off) duration = 0
if (typeof properties == 'string') {
// keyframe animation
cssValues[animationName] = properties
cssValues[animationDuration] = duration + 's'
cssValues[animationDelay] = delay + 's'
cssValues[animationTiming] = (ease || 'linear')
endEvent = $.fx.animationEnd
} else {
cssProperties = []
// CSS transitions
for (key in properties)
if (supportedTransforms.test(key)) transforms += key + '(' + properties[key] + ') '
else cssValues[key] = properties[key], cssProperties.push(dasherize(key))
if (transforms) cssValues[transform] = transforms, cssProperties.push(transform)
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
$(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
setTimeout(function () {
if (fired) return
wrappedCallback.call(that)
}, ((duration + delay) * 1000) + 25)
}
// trigger page reflow so new elements can animate
this.size() && this.get(0).clientLeft
this.css(cssValues)
if (duration <= 0) setTimeout(function () {
that.each(function () { wrappedCallback.call(this) })
}, 0)
return this
}
testEl = null
})(Zepto)
//動(dòng)畫效果模塊
; (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)
//不是很理解作者的想法,如果這里繼續(xù)執(zhí)行下去,所有調(diào)用zepto原生show事件的元素,都會(huì)被這個(gè)事件覆蓋,并且透明度都為被設(shè)為1...
if (speed === undefined) return 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)
})
}
$.fn.slideDown = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'hidden'
});
//獲取元素高度
var height = this.height() === 0 ? $(window).height() : this.height();
//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// position: position,
// visibility: 'visible',
// overflow: 'auto',
// height: 0
//}).animate({ height: height }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
top: -height,
left: 0,
position: position,
visibility: 'visible',
overflow: 'auto'
}).animate({ top: 0 }, speed, null, callback);
};
$.fn.slideUp = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'auto'
});
//獲取元素高度
var height = this.height();
//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// position: position,
// visibility: 'visible',
// overflow: 'hidden',
// height: height
//}).animate({ height: 0 }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
left: 0,
position: position,
visibility: 'visible',
overflow: 'auto'
}).animate({ top: -height }, speed, null, callback);
};
$.fn.slideLeft = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'hidden'
});
//獲取元素寬度
var width = this.width();
//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// top: 0,
// position: position,
// visibility: 'visible',
// overflow: 'auto'
//}).animate({ width: 0 }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
top: 0,
position: position,
visibility: 'visible',
overflow: 'auto'
}).animate({ left: -width }, speed, null, callback);
};
$.fn.slideRight = function (speed, callback) {
//獲取元素position
var position = this.css('position');
this.show().css({
position: 'absolute',
visibility: 'hidden'
});
//獲取元素寬度
var width = this.width() === 0 ? $(window).width() : this.width();
//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------
//return this.css({
// top: 0,
// width: 0,
// position: position,
// visibility: 'visible',
// overflow: 'auto'
//}).animate({ width: width }, speed, null, callback);
//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------
return this.css({
top: 0,
left: -width,
position: position,
visibility: 'visible',
overflow: 'auto',
}).animate({ left: 0 }, speed, null, callback);
};
})(Zepto)
//animate模塊; (function ($, undefined) {var prefix = '', eventPrefix,vendors = { Webkit: 'webkit', Moz: '', O: 'o' },testEl = document.createElement('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 = {}function dasherize(str) { return str.replace(/([A-Z])/g, '-$1').toLowerCase() }function normalizeEvent(name) { return eventPrefix ? eventPrefix + name : name.toLowerCase() }if (testEl.style.transform === undefined) $.each(vendors, function (vendor, event) {if (testEl.style[vendor + 'TransitionProperty'] !== undefined) {prefix = '-' + vendor.toLowerCase() + '-'eventPrefix = eventreturn false}})transform = prefix + 'transform'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'] = ''$.fx = {off: (eventPrefix === undefined && testEl.style.transitionProperty === undefined),speeds: { _default: 400, fast: 200, slow: 600 },cssPrefix: prefix,transitionEnd: normalizeEvent('TransitionEnd'),animationEnd: normalizeEvent('AnimationEnd')}$.fn.animate = function (properties, duration, ease, callback, delay) {if ($.isFunction(duration))callback = duration, ease = undefined, duration = undefinedif ($.isFunction(ease))callback = ease, ease = undefinedif ($.isPlainObject(duration))ease = duration.easing, callback = duration.complete, delay = duration.delay, duration = duration.durationif (duration) duration = (typeof duration == 'number' ? duration :($.fx.speeds[duration] || $.fx.speeds._default)) / 1000if (delay) delay = parseFloat(delay) / 1000return this.anim(properties, duration, ease, callback, delay)}$.fn.anim = function (properties, duration, ease, callback, delay) {var key, cssValues = {}, cssProperties, transforms = '',that = this, wrappedCallback, endEvent = $.fx.transitionEnd,fired = falseif (duration === undefined) duration = $.fx.speeds._default / 1000if (delay === undefined) delay = 0if ($.fx.off) duration = 0if (typeof properties == 'string') {// keyframe animationcssValues[animationName] = propertiescssValues[animationDuration] = duration + 's'cssValues[animationDelay] = delay + 's'cssValues[animationTiming] = (ease || 'linear')endEvent = $.fx.animationEnd} else {cssProperties = []// CSS transitionsfor (key in properties)if (supportedTransforms.test(key)) transforms += key + '(' + properties[key] + ') 'else cssValues[key] = properties[key], cssProperties.push(dasherize(key))if (transforms) cssValues[transform] = transforms, cssProperties.push(transform)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 setTimeoutfired = true$(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 firedsetTimeout(function () {if (fired) returnwrappedCallback.call(that)}, ((duration + delay) * 1000) + 25)}// trigger page reflow so new elements can animatethis.size() && this.get(0).clientLeftthis.css(cssValues)if (duration <= 0) setTimeout(function () {that.each(function () { wrappedCallback.call(this) })}, 0)return this}testEl = null})(Zepto)//動(dòng)畫效果模塊; (function ($, undefined) {var document = window.document, docElem = document.documentElement,origShow = $.fn.show, origHide = $.fn.hide, origToggle = $.fn.togglefunction anim(el, speed, opacity, scale, callback) {if (typeof speed == 'function' && !callback) callback = speed, speed = undefinedvar props = { opacity: opacity }if (scale) {props.scale = scaleel.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)//不是很理解作者的想法,如果這里繼續(xù)執(zhí)行下去,所有調(diào)用zepto原生show事件的元素,都會(huì)被這個(gè)事件覆蓋,并且透明度都為被設(shè)為1...if (speed === undefined) return origShow.call(this) // 原版為:if (speed === undefined) speed = 0else 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 = 1return 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)})}$.fn.slideDown = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'hidden'});//獲取元素高度var height = this.height() === 0 ? $(window).height() : this.height();//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// position: position,// visibility: 'visible',// overflow: 'auto',// height: 0//}).animate({ height: height }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({top: -height,left: 0,position: position,visibility: 'visible',overflow: 'auto'}).animate({ top: 0 }, speed, null, callback);};$.fn.slideUp = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'auto'});//獲取元素高度var height = this.height();//-------通過(guò)伸縮元素高度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// position: position,// visibility: 'visible',// overflow: 'hidden',// height: height//}).animate({ height: 0 }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({left: 0,position: position,visibility: 'visible',overflow: 'auto'}).animate({ top: -height }, speed, null, callback);};$.fn.slideLeft = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'hidden'});//獲取元素寬度var width = this.width();//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// top: 0,// position: position,// visibility: 'visible',// overflow: 'auto'//}).animate({ width: 0 }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({top: 0,position: position,visibility: 'visible',overflow: 'auto'}).animate({ left: -width }, speed, null, callback);};$.fn.slideRight = function (speed, callback) {//獲取元素positionvar position = this.css('position');this.show().css({position: 'absolute',visibility: 'hidden'});//獲取元素寬度var width = this.width() === 0 ? $(window).width() : this.width();//-------通過(guò)伸縮元素寬度實(shí)現(xiàn)動(dòng)畫-------//return this.css({// top: 0,// width: 0,// position: position,// visibility: 'visible',// overflow: 'auto'//}).animate({ width: width }, speed, null, callback);//-------通過(guò)移動(dòng)元素相對(duì)位置實(shí)現(xiàn)動(dòng)畫-------return this.css({top: 0,left: -width,position: position,visibility: 'visible',overflow: 'auto',}).animate({ left: 0 }, speed, null, callback);};})(Zepto)
總結(jié)
以上是生活随笔為你收集整理的为Zepto添加Slide动画效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 策略路由和路由策略
- 下一篇: linux文件上传,给文件或目录添加ap