Hello everyone, here is "learn the Web series from scratch" and synchronize updates at the following address ...
- Github:https://github.com/daotin/web
- Public number: The top of the Web front
- Blog Park: http://www.cnblogs.com/lvonve/
- csdn:https://blog.csdn.net/lvonve/
Here I will start with the Web front End 0 Foundation, step-up learning web-related knowledge points, during the period will also share some fun projects. Now let's go into the Web front-end learning adventure!
First, the touch incident defects
In the above page classification project, the tap event is handled using the touch event, because there is always a delay if the click event is used.
However, the touch event is not perfect, whether it is our own packaged tap event, or the Zepto's own tap event, there is a fatal flaw on the mobile side, that is " point through".
What is "point through"?
If there are two boxes, box A and box B, if box A is above box B, when we use the TAP event to click on Box A, box B will trigger the Click event, which is the point.
The order in which these two events are triggered is the tap event, followed by the Click event. Because the TAP event is handled internally by the touch event, the touch event is triggered before the click event.
At this time, we want to have no delay, and do not want to trigger a point through the effect, and sometimes, we hope that our web page can not only be accessed on the mobile side, in the PC mode can also be accessed, but the tap event can only be used on the mobile side, so can only use the Click event, but the click has a delay, how
We know that the touch event can only be used on the mobile side, which we cannot change, so we can only change the delay problem, so we introduced the "fastclick.js" library file to solve the click Delay problem.
How to use:
1. Introduction of Fastclick.js file.
2. Add the following function to the script:
Native JS words, add:
if ('addEventListener' in document) { document.addEventListener('DOMContentLoaded', function() { // document.body 表示整个body下的所有元素都是用fastclick效果,可以修改。 FastClick.attach(document.body); }, false);}
JQuery or Zepto words:
$(function() { FastClick.attach(document.body);});
3, normal use 元素.addEventListener("click", function(){}) or 元素.on("click", function(){}) , to use the modified click event, the Click event has no delay.
Second, the mobile side of some common plug-ins
Seeing the benefits of Fastclick plug-ins, we have dug out more useful plugins that can greatly improve the efficiency of our development.
1, Iscroll
The following is an official introduction:
Iscroll is a high performance, resource-intensive, non-dependent, multi-platform JavaScript scrolling plugin.
It can work on desktops, mobile devices and smart TV platforms. It has been aggressively optimizing performance and file size to deliver the smoothest experience on old and new devices.
Iscroll is more than just scrolling. It can handle any element that requires the user to interact with the mobile. With only 4kb size iscroll in your project, your project will have scrolling, zooming, panning, infinite Scrolling, parallax scrolling, and rotation. Give it a broom. It can even help you clean the office.
Even if the platform itself provides a good scroll, iscroll can provide even more incredible functionality on this basis. Specifically:
Fine-grained control of the scrolling position, even during scrolling. You can always get and set the X, Y coordinates of the scroll.
Animations can use user-defined erase functions (bounce ' bounce ', flex ' elastic ', rewind ' back ', ... )。
You can easily attach a large number of custom events (Onbeforescrollstart, *
Out-of-the-box multi-platform support. From the very old Android device to the latest iphone, from Chrome to Internet Explorer.
How to use:
1, hope your structure is as follows, but do not limit the label (the following UL can be changed to Div,li can be changed to P, etc., do not qualify the label type).
<div id="wrapper"> <ul> <li>...</li> <li>...</li> ... </ul></div>
2. Initialize the iscroll in the script tag.
var wrapper = document.getElementById('wrapper');var myScroll = new IScroll(wrapper);
If it's jQuery, it's easier, in a word:
var myScroll = new IScroll(".wrapper");
3, if you want to implement like a scroll wheel, display scrollbars and other effects, you can initialize the time, these requirements as objects, fill in the second parameter, for example, to increase the scroll wheel up and down operation and display the effect of the scroll bar:
var myScroll = new IScroll(".wrapper", { mouseWheel: true, // 使用滚轮 scrollbars: true // 显示滚动条});
With such a simple three-step operation, you can easily implement the functionality you want.
2, swipe
Swipe.js is a relatively well-known touch slide plugin that handles content slippage, supports custom options, lets it scroll automatically, controls scrolling intervals, returns callback functions, and more. Often used as a carousel diagram .
How to use:
1. Introduction of Swipe.js File
2, I hope your HTML structure is (not qualified tag name):
<div id='slider' class='swipe'> <div class='swipe-wrap'> <div></div> <div></div> <div></div> </div></div>
3, the format of the settings (fixed writing, it is best not to modify, of course, the class name needs to be modified)
.swipe { overflow: hidden; visibility: hidden; position: relative;}.swipe-wrap { overflow: hidden; position: relative;}.swipe-wrap > div { float:left; width:100%; position: relative;}
3. Initialize in script:
window.mySwipe = Swipe(document.getElementById('slider'));
4. If you want to automatically rotate, swipe, and so on, you need to introduce an object in the second parameter of initialization, such as:
window.mySwipe = new Swipe(document.getElementById('slider'), { startSlide: 2, // 默认显示第二张图片 speed: 400, // 过渡400ms auto: 3000, // 轮播间隔 2s continuous: true, // 循环轮播(默认开启) disableScroll: false, // 禁止滑动(默认关闭) stopPropagation: false, callback: function(index, elem) {}, transitionEnd: function(index, elem) {}});
5, of course, you can also use the left and right two buttons on the PC to the next page. Swipe will provide next() , prev() and so on, a function to implement the next page. Like what:
document.getElementById('btn1').onclick = function(){ window.mySwipe.prev(); // 调用系统的prev()方法};document.getElementById('btn2').onclick = function(){ window.mySwipe.next(); // 调用系统的next()方法};
3, Swiper
Swiper and swipe are similar, can provide the effect of carousel touch sliding, but swiper can provide more effects, more cool, the corresponding volume is larger.
Instruction for use: Reference link: http://www.swiper.com.cn/usage/index.html
It is important to note that Swiper differs from swipe, which is also a fixed, open-label, unique difference between a class-style name and a non-immutable one. Because it introduces the CSS style of the library file. So it's best not to change the name of the class style. Specific content can refer to the official website, there are a lot of detailed use instructions and special effects demonstration.
Temporary tips: overflow:hidden You can let the child elements float the parent box from a height of 0 to auto-scaling.
Start from scratch Web Mobile Web (v) Touch event flaw, mobile plug-in common