Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api。 如果你会用jquery,那么你也会用zepto。

Zepto的设计目的是提供 jQuery 的类似的API,但并不是100%覆盖 jQuery 。所以会有些jQuery的动画功能Zepto实现不了,例如fade动画,但官方提供了解决办法,

主要是通过引入两个js插件:animate.jszepto.animate.alias.js来实现完整的动画功能:

zepto.animate.alias.js:

  1. /**
  2. * zepto.animate.alias.js
  3. */
  4. (function($) {
  5. $.extend($.fn, {
  6. fadeIn: function(speed, easing, complete) {
  7. if(typeof(speed) === 'undefined') speed = 400;
  8. if(typeof(easing) === 'undefined') {
  9. easing = 'swing';
  10. } else if(typeof(easing) === 'function') {
  11. if(typeof(complete) === 'undefined') complete = easing;
  12. easing = 'swing';
  13. }
  14.  
  15. $(this).css({
  16. display: 'block',
  17. opacity: 0
  18. }).animate({
  19. opacity: 1
  20. }, speed, easing, function() {
  21. // complete callback
  22. complete && typeof(complete) === 'function' && complete();
  23. });
  24.  
  25. return this;
  26. },
  27. fadeOut: function(speed, easing, complete) {
  28. if(typeof(speed) === 'undefined') speed = 400;
  29. if(typeof(easing) === 'undefined') {
  30. easing = 'swing';
  31. } else if(typeof(easing) === 'function') {
  32. if(typeof(complete) === 'undefined') complete = easing;
  33. easing = 'swing';
  34. }
  35.  
  36. $(this).css({
  37. opacity: 1
  38. }).animate({
  39. opacity: 0
  40. }, speed, easing, function() {
  41. $(this).css('display', 'none');
  42. // complete callback
  43. complete && typeof(complete) === 'function' && complete();
  44. });
  45.  
  46. return this;
  47. },
  48. fadeToggle: function(speed, easing, complete) {
  49. return this.each(function() {
  50. var el = $(this);
  51. el[(el.css('opacity') === 0 || el.css('display') === 'none') ? 'fadeIn' : 'fadeOut'](speed, easing, complete)
  52. })
  53. }
  54. })
  55. })(Zepto);

animate.js:

  1. (function($, undefined) {
  2. var prefix = '',
  3. eventPrefix,
  4. vendors = {
  5. Webkit: 'webkit',
  6. Moz: '',
  7. O: 'o'
  8. },
  9. testEl = document.createElement('div'),
  10. supportedTransforms = /^((translate|rotate|scale)(X|Y|Z|3d)?|matrix(3d)?|perspective|skew(X|Y)?)$/i,
  11. transform,
  12. transitionProperty, transitionDuration, transitionTiming, transitionDelay,
  13. animationName, animationDuration, animationTiming, animationDelay,
  14. cssReset = {}
  15.  
  16. function dasherize(str) {
  17. return str.replace(/([A-Z])/g, '-$1').toLowerCase()
  18. }
  19.  
  20. function normalizeEvent(name) {
  21. return eventPrefix ? eventPrefix + name : name.toLowerCase()
  22. }
  23.  
  24. if(testEl.style.transform === undefined) $.each(vendors, function(vendor, event) {
  25. if(testEl.style[vendor + 'TransitionProperty'] !== undefined) {
  26. prefix = '-' + vendor.toLowerCase() + '-'
  27. eventPrefix = event
  28. return false
  29. }
  30. })
  31.  
  32. transform = prefix + 'transform'
  33. cssReset[transitionProperty = prefix + 'transition-property'] =
  34. cssReset[transitionDuration = prefix + 'transition-duration'] =
  35. cssReset[transitionDelay = prefix + 'transition-delay'] =
  36. cssReset[transitionTiming = prefix + 'transition-timing-function'] =
  37. cssReset[animationName = prefix + 'animation-name'] =
  38. cssReset[animationDuration = prefix + 'animation-duration'] =
  39. cssReset[animationDelay = prefix + 'animation-delay'] =
  40. cssReset[animationTiming = prefix + 'animation-timing-function'] = ''
  41.  
  42. $.fx = {
  43. off: (eventPrefix === undefined && testEl.style.transitionProperty === undefined),
  44. speeds: {
  45. _default: 400,
  46. fast: 200,
  47. slow: 600
  48. },
  49. cssPrefix: prefix,
  50. transitionEnd: normalizeEvent('TransitionEnd'),
  51. animationEnd: normalizeEvent('AnimationEnd')
  52. }
  53.  
  54. $.fn.animate = function(properties, duration, ease, callback, delay) {
  55. if($.isFunction(duration))
  56. callback = duration, ease = undefined, duration = undefined
  57. if($.isFunction(ease))
  58. callback = ease, ease = undefined
  59. if($.isPlainObject(duration))
  60. ease = duration.easing, callback = duration.complete, delay = duration.delay, duration = duration.duration
  61. if(duration) duration = (typeof duration == 'number' ? duration :
  62. ($.fx.speeds[duration] || $.fx.speeds._default)) / 1000
  63. if(delay) delay = parseFloat(delay) / 1000
  64. return this.anim(properties, duration, ease, callback, delay)
  65. }
  66.  
  67. $.fn.anim = function(properties, duration, ease, callback, delay) {
  68. var key, cssValues = {},
  69. cssProperties, transforms = '',
  70. that = this,
  71. wrappedCallback, endEvent = $.fx.transitionEnd,
  72. fired = false
  73.  
  74. if(duration === undefined) duration = $.fx.speeds._default / 1000
  75. if(delay === undefined) delay = 0
  76. if($.fx.off) duration = 0
  77.  
  78. if(typeof properties == 'string') {
  79. // keyframe animation
  80. cssValues[animationName] = properties
  81. cssValues[animationDuration] = duration + 's'
  82. cssValues[animationDelay] = delay + 's'
  83. cssValues[animationTiming] = (ease || 'linear')
  84. endEvent = $.fx.animationEnd
  85. } else {
  86. cssProperties = []
  87. // CSS transitions
  88. for(key in properties)
  89. if(supportedTransforms.test(key)) transforms += key + '(' + properties[key] + ') '
  90. else cssValues[key] = properties[key], cssProperties.push(dasherize(key))
  91.  
  92. if(transforms) cssValues[transform] = transforms, cssProperties.push(transform)
  93. if(duration > 0 && typeof properties === 'object') {
  94. cssValues[transitionProperty] = cssProperties.join(', ')
  95. cssValues[transitionDuration] = duration + 's'
  96. cssValues[transitionDelay] = delay + 's'
  97. cssValues[transitionTiming] = (ease || 'linear')
  98. }
  99. }
  100.  
  101. wrappedCallback = function(event) {
  102. if(typeof event !== 'undefined') {
  103. if(event.target !== event.currentTarget) return // makes sure the event didn't bubble from "below"
  104. $(event.target).unbind(endEvent, wrappedCallback)
  105. } else
  106. $(this).unbind(endEvent, wrappedCallback) // triggered by setTimeout
  107.  
  108. fired = true
  109. $(this).css(cssReset)
  110. callback && callback.call(this)
  111. }
  112. if(duration > 0) {
  113. this.bind(endEvent, wrappedCallback)
  114. // transitionEnd is not always firing on older Android phones
  115. // so make sure it gets fired
  116. setTimeout(function() {
  117. if(fired) return
  118. wrappedCallback.call(that)
  119. }, ((duration + delay) * 1000) + 25)
  120. }
  121.  
  122. // trigger page reflow so new elements can animate
  123. this.size() && this.get(0).clientLeft
  124.  
  125. this.css(cssValues)
  126.  
  127. if(duration <= 0) setTimeout(function() {
  128. that.each(function() {
  129. wrappedCallback.call(this)
  130. })
  131. }, 0)
  132.  
  133. return this
  134. }
  135.  
  136. testEl = null
  137. })(Zepto);

Zepto.js实现fadeIn,fadeOut功能的更多相关文章

  1. 原生JS实现购物车结算功能代码+zepto版

    html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  2. zepto.js

    // Zepto.js// (c) 2010-2016 Thomas Fuchs// Zepto.js may be freely distributed under the MIT license. ...

  3. 学习zepto.js(对象方法)[3]

    继续说zepto里attributes的相关操作. attr,removeAttr,prop这三个方法. attr(): 三种用途 get: 返回值为一个string字符串 $("<s ...

  4. 移动开发js库Zepto.js应用详解

    从哪里下载 Zepto 地址:http://zeptojs.com/ 中文版地址:http://www.css88.com/doc/zeptojs_api/ 这个问题看起来很蠢,从官网下载不就行了嘛! ...

  5. 学习zepto.js(原型方法)

    学习zepto.js(原型方法)[1] 转载 新的一周,新的开始,今天来学习一下zepto里边的原型方法,就是通过$.进行调用的方法,也是可以通过$.fn进行扩展的方法: $.camelCase(): ...

  6. zepto.js的基本介绍与使用

    最近看到了一篇文章,是介绍一种新的js框架,名为zepto.js,他适用于移动设备已经桌面浏览器除了ie系列的.. 他兼容jquery的API,所以学起来或用起来并不吃力.他比jquery的优势在于1 ...

  7. Zepto.js入门介绍

    GitHub Zepto Zepto的一些可选功能是专门针对移动端浏览器的:因为它的最初目标在移动端提供一个精简的类似jquery的js库. Zepto不支持旧版本的Internet Explorer ...

  8. zepto.js swipe实现触屏tab菜单

    今天我们来说下zepto.js,有兴趣的朋友可以先进这个网站“http://zeptojs.com/” ,这个可以说是手机里的jquery,但是它取消了hover,加上了swipe及tap这两个触屏功 ...

  9. 怎么使用zepto.js的tap事件引起的探索

    前言:   在使用zepto.js之前,你首先要知道它是什么?为什么要使用它?以及它和jquery有什么区别? ①:简单来说zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与j ...

随机推荐

  1. java5核心基础之泛型(3)-泛型作用于编译阶段-怎样将String对象传入Integer类型的泛型对象中?

    泛型作用于编译阶段: 泛型是作用于编译阶段,在编译阶段控制类型,以确保在编写代码的时候仅仅能传入指定类型数据到泛型集合对象中去. 怎样验证呢,贴代码例如以下: package highBasic.ge ...

  2. Java类的多态机制

    Java中将一个方法调用同一个方法主体关联起来被称作绑定. 绑定分为前期绑定和后期绑定.前期绑定是在编译器决定的,而后期绑定是在程序运行时决定的.Java中除了static方法和final方法(pri ...

  3. MVC地区多级联动扩展实现(非递归形式)

    MVC前台界面调用方式如下: @Html.AreaDropDownList(, string.Empty) 参数说明: 第一个参数控件的名称: 第二个参数选中的地区编码: 第三个参数地区层级: 第四个 ...

  4. css兼容性的一些问题

    1. 文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff 下实际占高17px,上留白1px,下留白3px, ...

  5. Gym-101158J Cover the Polygon with Your Disk 计算几何 求动圆与多边形最大面积交

    题面 题意:给出小于10个点形成的凸多边形 和一个半径为r 可以移动的圆 求圆心在何处的面积交最大,面积为多少 题解:三分套三分求出圆心位置,再用圆与多边形面积求交 #include<bits/ ...

  6. UVA-12578 10:6:2 计算几何 模拟

    题面 题意:给你一块长方形,告诉你长:宽=10:6 里面有一个圆,长:半径=5:1,给你长方形的长,求圆的面积和剩余部分的面积 题解:直接模拟输出就好 #include<bits/stdc++. ...

  7. javascript设计模式-掺元类

    有一种重用代码的方法不需要用到严格的继承.如果想把一个函数用到多个类中,可以通过扩充的方式让这些类共享该函数.其实际做法大大体为:先创建一个包含各种通用方法的类,然后再用它扩充其他的类.这种方式就叫做 ...

  8. 如何用ajax写分页查询(以留言信息为例)-----2017-05-17

    要写分页,首先你得清楚,一页你想显示多少条信息?如何计算总共显示的页数? 先说一下思路: (1)从数据库读取数据,以chenai表为例,读取所有留言信息.并能够实现输入发送者,可以查询该发送者的留言总 ...

  9. java中字符串比较==和equals

    1 总体来说java中字符串的比较是==比较引用,equals 比较值的做法.(equals 对于其他引用类型比较的是地址,这是因为object的equals方法比较的是引用),但是不同的声明方法字符 ...

  10. KAFKA 调优

    KAFKA 调优 最近要对kafka集群做调优,就在网上看了些资料,总结如下. 我们的kafka版本是0.10.1.0. 机器配置是40G内存,300G硬盘. 一共有3台机器组成一个小的集群. Kak ...