2 Star 0 Fork 0

mirrors_arnaudsj/zepto

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Zepto.js: a minimalist framework for mobile WebKit browsers

Zepto.js is a minimalist inlinable framework for mobile WebKit browsers, with a jQuery-like chaining syntax

The ultimate goal is to have a ~2k library that handles most basic dredge work for you in a nice API so you can concentrate on getting stuff done.

Primary target platforms are:

  • iOS 4+

  • Android 2.2+

  • webOS 1.4.5+

Secondary platforms are:

  • Safari 5+ (desktop)

  • Chrome 5+ (desktop)

Syntax & features:

Basic call with CSS selector:

$('p>span').html('yoho').css('color:red');

Instead of a selector, a DOM Element, or a list of nodes can be passed in.

The $ function takes an optional context argument, which can be a DOM Element or a Zepto object:

$('span', $('p'))  // -> find all <span> elements in <p> elements

$('p').bind('click', function(){
  $('span', this).css('color:red'); // affects "span" children/grandchildren
});

Context and .find calls are equivalent:

$('span', $('p'))    // same
$('p').find('span')  // same

Element functions:

get(): return array of all elements found
get(0): return first element found
each(callback): iterate over array of all elements found
index('selector'): return an integer indicating the position of 'selector' in array of all elements found
first(): remove all but the first element from the list of found elements

find('selector'): find all children/grandchildren that match the given selector
closest('selector'): traverses the DOM upwards to find the first matching element
next(): next siblings
prev(): previous siblings
is('selector'): returns true/false if first element matches the selector

remove(): remove element

html('new html'): set the contents of the element(s)
text('new text'): set the text contents of the element(s)
append, prepend, before, after: like html, but add html to element contents (or before/after)
html(): get first element's .innerHTML
text(): get first element's .innerText
show(): forces elements to be displayed (only works correctly for block elements right now)
hide(): removes a elements from layout

offset(): get object with top: left: width: height: properties (in px)
height(): get first elements height in px
width(): get first elements width in px

attr('attribute'): get element attribute
attr('attribute', 'value'): set element attribute

css('css property', 'value'): set a CSS property
css({ property1: value1, property2: value2 }): set multiple CSS properties
css('css property'): get this CSS property of the first element

addClass('classname'): adds a CSS class name
removeClass('classname'): removes a CSS class name
hasClass('classname'): returns true of first element has a classname set
toggleClass('classname'[, switch]): adds/removes class, or adds/removes it when switch == true/false

bind(type, function): add an event listener (see below)
delegate(selector, type, function): add an event listener w/ event delegation (see below)
live(type, function): add an event listener that listens to the selector for current and future elements
trigger(type): triggers an event

Non-jQuery functions

pluck(property):
  return property for each element
  e.g. pluck('innerHTML') returns an array of all innerHTML properties of all elements found

compact():
  remove all null or undefined elements from list of elements found

anim(transform, opacity, duration):
  use -webkit-transform/opacity and do an animation

Utility functions:

$(document).ready(function(){ ... }): call function after DOM is ready to use (before load event fires)

Event handlers

Adding an event listener:

$('some selector').bind('click', function(event){ ... });

Adding an event listener on multiple events:

$('some selector').bind('touchstart touchmove touchend', function(event){ ... });

Adding one event listener that uses event delegation to be only active on a range of children/grandchildren (as given with the subselector):

$('some selector').delegate('some subselector', 'touchstart', function(event){ alert("I'm touched!") });

Adding a “live” event listener, that fires on all elements that match the selector now and in the future:

$('p.yay').live('click', function(){ alert("Clicked a p.yay element!") });

Removing an event listener:

$('some selector').unbind('click', listener);

Removing all event listeners for a particular event:

$('some selector').unbind('click');

Removing all event listeners:

$('some selector').unbind();

Touch events

Tapping:

$('some selector').tap(function(){ ... });

Double-tapping:

$('some selector').doubleTap(function(){ ... });

Swiping (e.g. “delete” button when swiping over a list entry):

$('some selector').swipe(function(){ ... });

<<<<<<< HEAD

Swiping left:

$('some selector').swipeLeft(function(){ ... });

Swiping right:

$('some selector').swipeRight(function(){ ... });

Ajax:

>>>>>>> 1182eedd3d59aec75b173b8723428439382b1cfe

Ajax

Simple GET and POST:

$.get(url, callback)
$.post(url, [data], [callback], [mime-type])
$.getJSON(url, callback)

If you need more control (all keys are optional):

$.ajax({
  type: 'POST', // defaults to 'GET'
  url: '/foo', // defaults to window.location
  data: {name: 'Zepto'}, // can be a string or object (objects are automatically serialized to JSON)
  dataType: 'json', // what response type you accept from the server ('json', 'xml', 'html', or 'text')
  success: function(body) { ... }, // body is a string (or if dataType is 'json', a parsed JSON object)
  error: function(xhr, type) { ... } // type is a string ('error' for HTTP errors, 'parsererror' for invalid JSON)
})

Loading content into an element:

$('selector').load('url'[, callback]);
$('selector').load('url #fragment-selector'[, callback]);

Environmental information

Zepto includes information about the environment it is running in the $.os object:

$.os.ios     // => true if running on Apple iOS
$.os.android // => true if running on Android
$.os.webos   // => true if running on HP/Palm WebOS
$.os.version // => string with version number, "4.0", "3.1.1", "2.1", etc.
$.os.iphone  // => true if running on iphone
$.os.ipad    // => true if running on ipad

Building

zepto.js can be used as-is. However, for best efficiency, run the included build step that uses the Google Closure Compiler to minify zepto.js and will give you an estimate on the compression that is achievable when zepto.js is served Gzipped.

For this to work, you need Ruby and Rake installed.

$ rake

You'll see an output like:

Original version: 5.7k
Minified: 3.7k
Minified and gzipped: 1.6k, compression factor 3.6

The minified file is saved in dist/zepto.min.js.

Loading Zepto

You load Zepto by using

<script src="/path/to/zepto.min.js"></script>

Or alternatively, you can just stick in the contents of zepto.min.js into a <script> tag in your HTML for the best loading performance. No need to load it form an external file!

Contributing!

I'd love some help on this, so please feel free to mess around!

If you don't know how a method should behave, please use jQuery as a reference. Zepto.js should closely emulate it. Note that emulation of all features of jQuery is not a project goal, rather we want the most useful parts while keeping to the ~2k minified goal.

Also, Zepto.js contains some non-jQuery extensions, that are geared towards mobile devices.

Visit zeptojs.com/

Join #zepto on irc.freenode.net and stay updated on twitter.com/zeptojs

Have fun!

License

Zepto.js is is licensed under the terms of the MIT License, see the included MIT-LICENSE file.

Copyright (c) 2010 Thomas Fuchs http://zeptojs.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
HTML 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_arnaudsj/zepto.git
git@gitee.com:mirrors_arnaudsj/zepto.git
mirrors_arnaudsj
zepto
zepto
master

搜索帮助