WebApp开发-Zepto
zepto.js自己去官网下载哈。
DOM操作
$(document).ready(function(){
var $cr = $("<div class='cr'>插入的div</div>");
// 插入操作
$("#a").append($cr);
$cr.appendTo($("#a"));
$("#a").prepend($cr);
$cr.prependTo($("#a"));
$("#a").after($cr);
$cr.insertAfter($("#a"));
$("#a").before($cr);
$cr.insertBefore($("#a")); // 删除 remove\empty
$("ul li").remove();
$("ul li").empty(); // 复制节点
$("ul li").click(function(){
$(this).clone().appendTo("ul");
}); // 替换节点 replaceWith
$("p").replaceWith("<span>是的,哈哈</span>"); // 包裹节点 wrap wrapAll
$("p").wrap("<div></div>");
$("p").wrapAll("<div></div>");
});
属性与样式操作
$(document).ready(function(){
// 属性操作 attr("title") attr("name","att") attr({"name":"att" , "class":"test"}) removeAttr("title name")
console.log($("div").attr("title"));
$("div").attr("name","att");
$("div").attr({"name":"att" , "class":"test"})
$("div").removeAttr("title name"); // 添加样式操作 addClass
$("div").addClass("red").addClass("fs");
$("div").addClass("red fs"); // 删除class类 removeClass
$("div").removeClass(); // 切换样式 toggle toggleClass
$("button").click(function(){
$("div").toggle(); // show和hide的切换
$("div").toggleClass("red"); // addClass("red")和removeClass("red");
}); // 判断是否含有某个样式 hasClass
console.log($("div").hasClass("red fs"))
});
遍历节点
$(document).ready(function(){
// next是取得紧邻的后面的同辈元素
console.log($("#one h3").next()); // prev获取紧邻的前面的同辈元素
console.log($("#one a").prev()); // siblings获取前后的所有同辈元素
console.log($("#one p").siblings()); // parent与parents直系亲属
console.log($("b").parent());
console.log($("b").parents());
});
CSS-DOM操作
$(document).ready(function(){
$(".one").css("color", "red").css("fontSize", "36px");
$(".one").css({
color:"red",
fontSize:"36px"
});
$(".one").width(500);
$(".one").height(500);
});
ready与onload的区别
// html文件 、 css文件 、 js文件 、 图片文件等
$(document).ready(function(){
// DOM加载完毕,图片并未完全加载,调用时机比较快
});
window.onload = function(){
// 全部文件加载完毕,调用时机比较久
}
事件开头简写
$(document).ready(function(){ })
$(function(){ })
$().ready(function(){ })
事件绑定
$(".one").bind("click", function(e){
console.log("one被点击了!!!")
}) $(".one").click(function(e){
console.log("我是简写方式");
}) $(".one").on("click", function(e){
console.log("我是on事件");
});
---------------------------------
// on
$("ul").on('click', 'li', function(e){ })
// off
$("li").off();
zepto不支持事件捕获
事件其他用法
// 自定义事件
$("div").bind("muke", function(){
console.log("触发自定义事件");
});
$("div").trigger("muke"); // 命名空间
$("div").bind("click", function(){
console.log("我是普通的click事件");
})
$("div").bind("click.muke", function(){
console.log("我是click.muke的事件");
}) $("div").unbind(".muke"); // 绑定多个事件
$("div").bind("click", function(){
console.log(111)
}).bind("touchstart", function(){
console.log(222)
})
动画
$('button').on('click' , function(){
$('div').toggle('slow'); //切换元素的显示与隐藏
$('div').hide(3000);
$('div').show('slow');
$('div').fadeIn('slow');
$('div').fadeOut('slow');
$('div, button').fadeToggle('slow');
$("div").fadeTo(3000 , 1)
})
animate() 动画函数
$("div").animate({left:"300px", height:"300px"}, 3000, function(){
alert("动画执行完毕");
})
Ajax
function Ajax(){
var xmlHttpReq = null;
if(window.ActiveXObject){
// 兼容IE5、IE6
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpReq = new XMLHttpRequest();
} xmlHttpReq.open("GET", "test.php", true);
xmlHttpReq.onreadystatechange = RequestCallBack; function RequestCallBack(){
if(xmlHttpReq.readyState == 4){
if(xmlHttpReq.status == 200){
console.log("获取数据:"+xmlHttpReq.responseText);
}
}
}
xmlHttpReq.send(null);
}
ajax请求
// get
$.get('urlxxx',function(response){
$(document.body).append(response)
}); // post
$.post('urlxxx', { sample: 'payload' }, function(response){ }); // ajax
$.ajax({
type: 'GET',
url: 'urlxxx',
data: { name: 'Zepto.js' },
dataType: 'json',
success: function(data){
this.append(data.project.html)
},
error: function(xhr, type){
alert('Ajax error!')
}
})
touch模块
先引入touch.js
<script type="text/javascript" src="../../lib/touch.js"></script>
touch.js
;(function($){
var touch = {},
touchTimeout, tapTimeout, swipeTimeout, longTapTimeout,
longTapDelay = 750,
gesture function swipeDirection(x1, x2, y1, y2) {
return Math.abs(x1 - x2) >=
Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
} function longTap() {
longTapTimeout = null
if (touch.last) {
touch.el.trigger('longTap')
touch = {}
}
} function cancelLongTap() {
if (longTapTimeout) clearTimeout(longTapTimeout)
longTapTimeout = null
} function cancelAll() {
if (touchTimeout) clearTimeout(touchTimeout)
if (tapTimeout) clearTimeout(tapTimeout)
if (swipeTimeout) clearTimeout(swipeTimeout)
if (longTapTimeout) clearTimeout(longTapTimeout)
touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null
touch = {}
} function isPrimaryTouch(event){
return (event.pointerType == 'touch' ||
event.pointerType == event.MSPOINTER_TYPE_TOUCH)
&& event.isPrimary
} function isPointerEventType(e, type){
return (e.type == 'pointer'+type ||
e.type.toLowerCase() == 'mspointer'+type)
} $(document).ready(function(){
var now, delta, deltaX = 0, deltaY = 0, firstTouch, _isPointerType if ('MSGesture' in window) {
gesture = new MSGesture()
gesture.target = document.body
} $(document)
.bind('MSGestureEnd', function(e){
var swipeDirectionFromVelocity =
e.velocityX > 1 ? 'Right' : e.velocityX < -1 ? 'Left' : e.velocityY > 1 ? 'Down' : e.velocityY < -1 ? 'Up' : null
if (swipeDirectionFromVelocity) {
touch.el.trigger('swipe')
touch.el.trigger('swipe'+ swipeDirectionFromVelocity)
}
})
.on('touchstart MSPointerDown pointerdown', function(e){
if((_isPointerType = isPointerEventType(e, 'down')) &&
!isPrimaryTouch(e)) return
firstTouch = _isPointerType ? e : e.touches[0]
if (e.touches && e.touches.length === 1 && touch.x2) {
// Clear out touch movement data if we have it sticking around
// This can occur if touchcancel doesn't fire due to preventDefault, etc.
touch.x2 = undefined
touch.y2 = undefined
}
now = Date.now()
delta = now - (touch.last || now)
touch.el = $('tagName' in firstTouch.target ?
firstTouch.target : firstTouch.target.parentNode)
touchTimeout && clearTimeout(touchTimeout)
touch.x1 = firstTouch.pageX
touch.y1 = firstTouch.pageY
if (delta > 0 && delta <= 250) touch.isDoubleTap = true
touch.last = now
longTapTimeout = setTimeout(longTap, longTapDelay)
// adds the current touch contact for IE gesture recognition
if (gesture && _isPointerType) gesture.addPointer(e.pointerId)
})
.on('touchmove MSPointerMove pointermove', function(e){
if((_isPointerType = isPointerEventType(e, 'move')) &&
!isPrimaryTouch(e)) return
firstTouch = _isPointerType ? e : e.touches[0]
cancelLongTap()
touch.x2 = firstTouch.pageX
touch.y2 = firstTouch.pageY deltaX += Math.abs(touch.x1 - touch.x2)
deltaY += Math.abs(touch.y1 - touch.y2)
})
.on('touchend MSPointerUp pointerup', function(e){
if((_isPointerType = isPointerEventType(e, 'up')) &&
!isPrimaryTouch(e)) return
cancelLongTap() // swipe
if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
(touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)) swipeTimeout = setTimeout(function() {
if (touch.el){
touch.el.trigger('swipe')
touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
}
touch = {}
}, 0) // normal tap
else if ('last' in touch)
// don't fire tap when delta position changed by more than 30 pixels,
// for instance when moving to a point and back to origin
if (deltaX < 30 && deltaY < 30) {
// delay by one tick so we can cancel the 'tap' event if 'scroll' fires
// ('tap' fires before 'scroll')
tapTimeout = setTimeout(function() { // trigger universal 'tap' with the option to cancelTouch()
// (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
var event = $.Event('tap')
event.cancelTouch = cancelAll
// [by paper] fix -> "TypeError: 'undefined' is not an object (evaluating 'touch.el.trigger'), when double tap
if (touch.el) touch.el.trigger(event) // trigger double tap immediately
if (touch.isDoubleTap) {
if (touch.el) touch.el.trigger('doubleTap')
touch = {}
} // trigger single tap after 250ms of inactivity
else {
touchTimeout = setTimeout(function(){
touchTimeout = null
if (touch.el) touch.el.trigger('singleTap')
touch = {}
}, 250)
}
}, 0)
} else {
touch = {}
}
deltaX = deltaY = 0 })
// when the browser window loses focus,
// for example when a modal dialog is shown,
// cancel all ongoing events
.on('touchcancel MSPointerCancel pointercancel', cancelAll) // scrolling the window indicates intention of the user
// to scroll, not tap or swipe, so cancel all ongoing events
$(window).on('scroll', cancelAll)
}) ;['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',
'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName){
$.fn[eventName] = function(callback){ return this.on(eventName, callback) }
})
})(Zepto)
使用时:
<script>
$(document).ready(function(){
$('#touch_test').bind('touchmove', function(e) { e.preventDefault() }) listen_to('#touch_test') function listen_to(el) {
$(el).tap(function(){
console.log(' | tap!')
})
.doubleTap(function(){
console.log(' | double tap!')
})
.swipe(function(){
console.log(' | swipe!')
})
.swipeLeft(function(){
console.log(' | swipe left!')
})
.swipeRight(function(){
console.log(' | swipe right!')
})
.swipeUp(function(){
console.log(' | swipe up!')
})
.swipeDown(function(){
console.log(' | swipe down!')
})
.longTap(function(){
console.log(' | long tap!')
})
.singleTap(function(){
console.log(' | single tap!')
})
}
});
</script>
插件的写法
先编写自定义插件js文件
zepto.color.js
;(function($){
//一个插件的写法
$.fn.color = function(option){
var options = $.extend({
col: "blue",
fz : "20px"
}, option); this.css("color", options.col);
this.css("fontSize", options.fz);
return this;
}
})(Zepto); //多组插件写法
/*
;(function($){ $.extend($.fn, {
color: function(option){
var options = $.extend({
col: "blue",
fz : "20px"
}, option); this.css("color", options.col);
this.css("fontSize", options.fz); return this;
}, background: function(option){
var options = $.extend({
bg: "blue"
}, option); this.css("background", options.bg); return this;
}
}) })(Zepto);
*/
使用时:
<script type="text/javascript" src="../../lib/zepto.color.js"></script>
<script type="text/javascript">
$("div").color({
col : "red"
}).addClass("helloworld");
</script>
WebApp开发-Zepto的更多相关文章
- 【blade利刃出鞘】一起进入移动端webapp开发吧
前言 在移动浪潮袭来的时候,小钗有幸进入框架组做webapp框架开发,过程中遇到了移动端的各种坑,也产生了各种激情,就我们公司的发展历程来说 第一阶段:使用传统方式开发移动站点,少量引入HTML5元素 ...
- webapp开发调试环境--weinre配置
用谷歌调试工具中的手机模拟器模拟手机进行webapp的开发,与真机上的效果还是有些偏差,opera手机模拟器的效果亦不佳.有时在pc上开发出来的webapp效果良好,在部分真机上就出现了偏差,这时候就 ...
- webapp开发中的一些注意的
和大多数响应式的布局一样,webapp开发也是需要浮动布局和百分比布局,需要考虑的是小屏幕手机250px和大屏幕设备768px,但是习惯以320px和640px来分割,jq中的一句话$(functio ...
- 【原】webapp开发中兼容Android4.0以下版本的css hack
话说现在的手机型号越来越多,主要还是android和ios这2个巨头称霸了江湖,而他们自带的浏览器内核是webkit,那对于做移动网页开发的同事来说,一般只要做好webkit内核浏览器的展现效果就行了 ...
- jquery mobile自己定义webapp开发实例(一)——前言篇
用jquery mobile做了一段时间的webapp开发,准备用自己的一个小demo做一个模块化的分享 点击demo演示 手机演示二维码: 此demo已经是比較老的版本号,用户体验流畅度确实还存在非 ...
- 跟我一起玩转Sencha Touch 移动 WebApp 开发1
跟我一起玩转Sencha Touch 移动 WebApp 开发(一) 1.目录 移动框架简介,为什么选择Sencha Touch? 环境搭建 创建项目框架,框架文件简介 创建简单Tabpanel案例 ...
- 移动端WEBAPP开发遇到的坑,以及填坑方案!持续更新~~~~
前言:在移动端WEBAPP开发中会遇到各种各样的问题,通过此文对遇到的问题做一个归纳总结,方便自己日后查询,也给各位前端开发友人做一个参考. 此文中涉及的问题是本人开发中遇到的,解决方案是本人思考 ...
- WebApp开发总结
WebApp开发总结 框架的使用网络上都有教程,就不写了,主要记录下个人的开发总结以方便以后开发注意. css公用样式统一定义 css样式抽出复用 appearance: none; 取消系统默认样式 ...
- 移动前端webApp开发点滴积累20140629
#移动前端webApp开发点滴积累20140629 ##关于input行内居中的问题 给input设定一个比较高的高度,在某些版本的移动设备上,文字不能垂直居中,即使设定了相同的行高也不行.(见图) ...
随机推荐
- Virus:病毒查杀
简介 小伙伴们,大家好,今天分享一次Linux系统杀毒的经历,还有个人的一些总结,希望对大家有用. 这次遇到的是一个挖矿的病毒,在挖一种叫门罗币(XMR)的数字货币,行情走势请看 https://ww ...
- 免费网盘!无限申请5TB容量并且不限速的网盘!
鸽了好久没有更新博客了哎…… 前言 这里我先说一下下,本人深受百度网盘坑害,自己上传的文件,16GB下载花了3天时间 最后下载失败? 所以找到的一个新的储存个人文件的方法. 这个网盘是onedrive ...
- debian wget 报ERROR: The certificate of ‘xxx’ is not trusted.
# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz --2019-01-11 15:04:41-- https://w ...
- 【全集】IDEA入门到实战
课程介绍 IDEA是一款功能强悍.非常好用的Java开发工具,近几年编程开发人员对IDEA情有独钟.虽然IDEA功能很强大,但目前市面讲解的不细致.不系统,导致很多IDEA初学者要么无从下手,要么 ...
- 彻底搞懂flex弹性盒模型布局
为什么要用flex 基于css3简单方便,更优雅的实现,浏览器兼容性好,传统的css实现一个div居中布局要写一堆代码,而现在几行代码就搞定了,没有理由不用flex. 兼容性: Base Browse ...
- 使用vscode对threejs的本地调试
始终坚信阅读和调试结合的方式,才是学习开源的高效方法 一.老版本调试 遥想当年的threejs(使用版本为r75)还没有CommonJS,对于我这个小白可以拿起大刀大杀四方......(咳.咳. 就是 ...
- 软链接和硬链接——Linux中的文件共享
硬链接(Hard Link)和软链接也称为符号链接(Symbolic Link)的目的是为了解决文件的共享使用问题.要阐明其原理,必须先理解Linux的文件存储方式. 索引结点 Linux是一个UNI ...
- export和export default的区别
export和export default的区别一.export的使用1.直接输出export let words = ‘hello world!!!’export function output() ...
- 【python人脸识别】使用opencv识别图片中的人脸
概述: OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库 为什么有OpenCV? 计算机视觉市场巨大而且持续增长,且这方面没有标准API,如今的计算机视觉软件大概有以下三种: 1.研究 ...
- C++ Primer 抄书笔记(一)
操作系统通过调用main函数(function)来运行C++程序: int main(){ ; } main函数返回类型必为int.大多数系统中main的返回值被用来指示状态.0即成功:非0由系统定义 ...