Home Web Front-end JS Tutorial Learn to use jquery iScroll.js mobile scroll bar plug-in_jquery

Learn to use jquery iScroll.js mobile scroll bar plug-in_jquery

May 16, 2016 pm 03:13 PM
jquery scroll bar

What is the most commonly used plug-in in your daily work, jQurey? Lazyload? But these are all on the PC side, but the most commonly used plug-in on the mobile side is iScroll. What exactly is iScroll and how should it be used? iScroll is a very powerful plug-in, and I only have a superficial understanding of it. Here we will briefly introduce it.
Generation of iScroll:

iScroll was created entirely because of mobile webkit browsers, such as on iPhone and Android mobile devices.
How to use iScroll:

The principle of iScroll is that there is an overflow hidden (overflow:hidden;) DOM in the outer layer, and then the first DOM structure in this area will be instantiated, and the wrapped content can be scrolled vertically or horizontally, so in When using iScroll, the scrolling elements should be as simple as possible, reduce the number of DOMs, and reduce nesting, because the more complex the DOM structure is, the more difficult it will be for iScroll to run, which may cause some nodes to display abnormally. Therefore, the recommended DOM structure is as follows:

<div id="wrapper">//overflow:hidden;
 <ul>
 //只有第一个DOM结构(ul)被实例化,这个DOM可以纵向或者横向的滚动,
 //多出的内容会被wrapper的样式hidden。
  <li>1</li>
  <li>2</li>
  <li>3</li>
 </ul>
</div>
Copy after login

Note: Again, only the first child element (ul) in the wrapper can be instantiated for scrolling, and scrolling must be achieved in conjunction with the outer DOM (wrapper).
What if there are multiple uls in the wrapper? It's very simple. Remember that sentence, only the first child element (ul) in the wrapper can be instantiated and scrolled:

<div id="wrapper">//overflow:hidden;
 <div id="first">
  //只有第一个DOM结构(ul)被实例化,这个DOM可以纵向或者横向的滚动,
  //多出的内容会被wrapper的样式hidden
  <ul>
   <li>1</li>
   <li>2</li>
   <li>3</li>
  </ul>
  <ul>
   <li>4</li>
   <li>5</li>
   <li>6</li>
  </ul>
 </div>
</div>
Copy after login

As you can see, only first will be instantiated. Note: The ID of the first DOM structure (first) does not need to be written here. I only wrote an ID to facilitate everyone's identification, but the outermost ID (wrapper) must be written because it is needed when JS is instantiated. Fill in this ID:

var myScroll = new iScroll("wrapper");
Copy after login

How iScroll should be instantiated:

Since we talked about instantiation, when should we instantiate it? It is said that there are many methods of instantiation, but I have never used it. I will only mention one:
(1) Load iscroll.js and uw3c.js of the current page at the bottom of the HTML (uw3c.html) page (after body and before html) to ensure that the DOM structure of HTML can be loaded.
(2) Instantiate iScroll before JS inserts the page DOM structure and data, that is, instantiate it at the very beginning of JS, because JS may be used to insert DOM or data later. This ensures that iScroll has been instantiated before inserting data. .

HTML://HTML structure

<html >
 <body>
  ...code...
 </body>
  //插入iscroll.js文件
 <script type="text/javascript" src="js/iscroll.js" > </script >
 //插入本页面JS文件
 <script type="text/javascript" src="js/uw3c.js" > </script >
</html>
Copy after login

JS://JS file content

var myscroll;
 function iscroll(data){
  //实例化iScroll
  myscroll=new iScroll("wrapper");
  pageData(data);
 }
 function pageData(obj){
  $("body").html(obj);
  myscroll.refresh();//当DOM结构发生变化的时候,需要刷新iScroll
 }
 iscroll("<div>pagedata</div>");
Copy after login

Parameters in iScroll:

When instantiating iScroll, you can pass in two parameters. The first parameter is the ID of the instantiated outer DOM, and the second parameter is the object of the iScroll execution method:

var myscroll=new iScroll("wrapper",{hScrollbar:false});
或者
var opts = {
    vScroll:false,//禁止垂直滚动
    snap:true,//执行传送带效果
    hScrollbar:false//隐藏水平方向上的滚动条
   };
var myscroll = new iScroll("wrapper",opts);
Copy after login

The content of the second parameter is as follows. This parameter will control the effect of iScroll:

hScroll  false 禁止横向滚动 true横向滚动 默认为true
vScroll  false 禁止垂直滚动 true垂直滚动 默认为true
hScrollbar  false隐藏水平方向上的滚动条
vScrollbar  false 隐藏垂直方向上的滚动条
fadeScrollbar false 指定在无渐隐效果时隐藏滚动条
hideScrollbar 在没有用户交互时隐藏滚动条 默认为true
bounce   启用或禁用边界的反弹,默认为true
momentum  启用或禁用惯性,默认为true,此参数在你想要保存资源的时候非常有用
lockDirection false取消拖动方向的锁定,true拖动只能在一个方向上(up/down 或者left/right)
Copy after login

Methods in iScroll:

Of course, in the second parameter, there are also some methods that can be executed:
(1)scrollTo(x, y, time, relative) method: Pass in 4 parameters: X-axis scrolling distance, Y-axis scrolling distance, effect time, and whether it is relative to the current position. So for example:

//在200毫秒的时间内,Y轴向上滚动100像素;
uw3c.scrollTo(0, -100, 200)
//在200毫秒的时间内,相对于当前位置,X轴向左滚动100像素;
uw3c.scrollTo(-100, 0, 200, true)
Copy after login

(2)refresh() method: After the DOM structure changes, iScroll needs to be refreshed, otherwise the scrolling plug-in will be instantiated inaccurately:

uw3c.refresh();//刷新iScroll
Copy after login

(3)onPosChange, is there a method that can return the change of position? You can check if there is an onPosChange method in the iScroll you are using:

onPosChange:function(x,y){
 if(y < -200){
  //如果Y周向上滚动200像素,$("#uw3c")就显示,否则就隐藏。
  $("#uw3c").show();
 }else{
  $("#uw3c").hide();
 }
}
Copy after login

(4) onScrollEnd: event executed when the scroll ends. If you want to trigger an event when the scroll ends, this method will be useful:

//滚动结束后,执行的方法,滚动后会出现提示框alert("uw3c.com")
onScrollEnd:function(){
 alert("uw3c.com");
}
Copy after login

(5) onRefresh: After the DOM structure changes, iScroll needs to be refreshed, otherwise the scrolling plug-in will be instantiated inaccurately. onRefresh is the method that will be executed after iScroll is refreshed.
(6) onBeforeScrollStart: The time callback before starting scrolling. The default is to prevent the browser's default behavior.
(7) onScrollStart: callback to start scrolling.
(8) onBeforeScrollMove: Callback before content moves.
(9) onScrollMove: callback for content movement.
(10) onBeforeScrollEnd: callback before the end of scrolling.
(11) onTouchEnd: callback after the hand leaves the screen.
(12) onDestroy: callback to destroy the instance.

The above is the entire content of this article. I hope it will help everyone to use the iScroll.js mobile scroll bar plug-in proficiently.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to set the scroll bar to always show on Mac system - How to set the scroll bar to always show How to set the scroll bar to always show on Mac system - How to set the scroll bar to always show Mar 18, 2024 pm 06:22 PM

Recently, some friends have consulted the editor about how to set the scroll bar of the Mac system to always display. The following will bring you the method of setting the scroll bar of the Mac system to always display. Friends who need it can learn more. Step 1: In the system start menu, select the [System Preferences] option. Step 3: On the System Preferences page, select the [General] option. Step 3: On the general page, select [Always] to display scroll bars.

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

See all articles