JS Zepto jQuery for mobile
Zepto
github地址: https://github.com/madrobby/zepto
官方地址: http://zeptojs.com/
中文版地址: http://www.css88.com/doc/zeptojs_api/
Zepto就是jQuery的移动端版本, 可以看做是一个轻量级的jQuery
注意点
Zepto的设计目的是提供 jQuery 的类似的API,但并不是100%覆盖 jQuery
jQuery的底层是通过DOM来实现效果的, zepto.js 是用css3 来实现的;
官网下载的zepto,就已经包含了官网所述的默认模块了
github上下载的zepto模块需要自己导入
<!--<!–引入核心模块;包含许多jQuery中常见方法–>--> <script src="js/zepto.js"></script> <!--<!–引入zepto事件模块, 包含了常见的事件方法on/off/click ...–>--> <script src="js/event.js"></script> <!--<!–引入zepto动画模块,–>--> <script src="js/fx.js"></script> <!--<!–引入zepto动画模块的常用方法,–>--> <script src="js/fx_methods.js"></script>
Zepto点击事件
由于移动端的手势多而且分单击双击,所以移动端的click事件有300ms左右的延迟,所以移动端的点击事件用tab
$("div").tap(function(){ ...... })
zepto中touch相关事件
touchstart
touchstart是手指刚触摸到元素时触发的事件
touchmove
touchmove是手指移动时触发的事件
touchend
当手指离开指定元素时触发
注意点
添加以上三个事件的时候用addEventListener
以上三个事件对pc端无效
zepto中touch事件的对象
touches:
保存了屏幕上所有手指的列表
targetTouches
保存了元素上所有手指的列表
changedTouches
包含了刚刚与屏幕接触的手指或者刚刚离开屏幕的手指
TouchEvent {isTrusted: true, touches: TouchList, targetTouches: TouchList, changedTouches: TouchList}
zepto中touch事件的XY
client: 可视区域
page: 内容
var oDiv = document.querySelector("div"); /* 1.注意点: 无论是event对象中的touches/targetTouches/changedTouches都是一个伪数组 所以我们想要获取手指位置的时候, 需要从伪数组中取出需要获取的那个手指对象 */ oDiv.addEventListener("touchstart", function (event) { // 获取手指距离屏幕左上角的位置 // console.log(event.targetTouches[0].screenX); // console.log(event.targetTouches[0].screenY); // 获取相对于当前视口的距离 console.log("clientX", event.targetTouches[0].clientX); console.log("clientY", event.targetTouches[0].clientY); /* clientX 10 clientY 8 pageX 1156 pageY 8 */ // 获取相对于当前页面内容的距离 console.log("pageX", event.targetTouches[0].pageX); console.log("pageY", event.targetTouches[0].pageY); });
简单案例: 物块拖拽
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>08-touch事件练习</title> <style> *{ margin: 0; padding: 0; } div{ width: 100px; height: 100px; background: red; } </style> </head> <body> <div></div> <script> var oDiv = document.querySelector("div"); var startX, startY, moveX, moveY; // 1.监听手指按下事件 oDiv.addEventListener("touchstart", function (event) { // 记录手指按下的位置 startX = event.targetTouches[0].clientX; startY = event.targetTouches[0].clientY; }); // 2.监听手指一动事件 oDiv.addEventListener("touchmove", function (event) { // 记录手指移动之后的位置 moveX = event.targetTouches[0].clientX; moveY = event.targetTouches[0].clientY; // 计算两个位置之间相差的距离, 相差的距离就是需要移动的距离 var offsetX = moveX - startX; var offsetY = moveY- startY; // 让div移动起来 oDiv.style.transform = "translate("+offsetX+"px,"+offsetY+"px)"; }); </script> </body> </html>
zepto中touch事件的点透问题
如果两个元素是重叠的(一个在另一个上面), 并且上面一个监听了touchstart事件, 下面一个监听了click事件,那么如果上面一个元素触发touchstart事件之后消失了, 那么就会出现点透问题
解决方案: fastclick框架
注: 框架必须在前面调用, 并且所有的元素都被注册了fastclick中的事件, 以后所有的click事件都是fastclick的click事件
zepto中的swipe事件
手指在元素上滑动触发的事件
$("div").swipeLeft(function () { // console.log("左滑动"); $(this).animate({left: "0px"}, 1000); }); $("div").swipeRight(function () { // console.log("右滑动"); $(this).animate({left: "100px"}, 1000); }); $("div").swipeUp(function () { // console.log("上滑动"); $(this).animate({top: "0px"}, 1000); }); $("div").swipeDown(function () { // console.log("下滑动"); $(this).animate({top: "100px"}, 1000);
移动端hover事件
移动端只能使用mouseenter和mouseleave来监听移入和移出
iscroll框架
当我们做移动端侧边栏的时候, 自己实现可能会出现bug也可能比较麻烦,这时候可以用iscroll框架
github地址: https://github.com/cubiq/iscroll
实现步骤
按照框架的需要搭建一个三层的结构
引入iscroll.js框架
创建一个IScroll对象, 把需要滚动的容器给它
var myScroll = new IScroll('.test', { mouseWheel: true, // 开启鼠标滚动滚动 scrollbars: true // 开启滚动条, 但是容器必须是定位的, 否则滚动条的位置不对 }); // 相关常用的回调函数 myScroll.on("beforeScrollStart", function () { console.log("手指触摸, 还没有开始滚动"); }); myScroll.on("scrollStart", function () { console.log("开始滚动"); }); myScroll.on("scrollEnd", function () { console.log("结束滚动"); });
swiper框架
Swiper是纯javascript打造的滑动特效插件,面向手机、平板电脑等移动终端。
Swiper能实现触屏焦点图、触屏Tab切换、触屏多图切换等常用效果。
Swiper开源、免费、稳定、使用简单、功能强大,是架构移动终端网站的重要选择!
如何使用:
引入对应的css和js文件
按照框架的需求搭建三层结构
创建一个Swiper对象, 将容器元素传递给它,第二个参数接收一个对象
<div> <ul> <li>slider1</li> <li>slider2</li> <li>slider3</li> </ul> </div> <script> var mySwiper = new Swiper('.test',{ loop: true, autoplay: true, // 如果需要分页器 pagination: { el: '.swiper-pagination', }, // 如果需要前进后退按钮 navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, // 如果需要滚动条 scrollbar: { el: '.swiper-scrollbar', },); </script>
推荐教程:《JS教程》
The above is the detailed content of JS Zepto jQuery for mobile. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP
