Seamless scrolling of jQuery plug-ins by yourself

Source: Internet
Author: User

Seamless scrolling of jQuery plug-ins by yourself

Yesterday-Yanyi Junjun festival, today-Cowherd and Weaver girl festival. The bachelor can only refer to the code!

 

First, let's look at the html skeleton as follows:

<div class="box">        <ul>            <li>111</li>            <li>222</li>            <li>333</li>        </ul></div>

The structure is simple and clear.

 

The implementation principle is as follows:

Div box is the outermost box, and the width and height are specified for it. Remember to add an overflow: hidden (beyond content hiding) style to the box, because the scrolling will definitely exceed the box.

We use js to control the margin of ul labels for scrolling. Horizontal scrolling controls margin-left; vertical scrolling controls margin-top;

In the initial state, we also need to determine the conditions to determine whether to scroll. That is, if the ul length is smaller than the outer box length, scroll is not performed. Otherwise, scroll is performed.

The length of ul is calculated by multiplying the length of a single li in ul by the number of li. Ul_width = li_width * li_num;

The reason for seamless rolling is that when the length of each scroll is exactly greater than the length of a single li, we will move the first li of ul to the end of ul, repeating and infinite loops (about this

You can choose not to set overflow: hidden first ).

  

The principle is too TM to test my ability to express myself. I hope I can clarify it clearly.

 

Let's look at the implementation code of the plug-in:

(Function ($) {$. fn. scroll = function (options) {// Save the current context object to root var root = this; // The default var settings = {speed: 40, // Scroll speed, the greater the value, the slower the speed. ction: "x" // scroll direction ("x" or "y" [x horizontal; y vertical])}; // not empty, merge the parameter if (options) $. extend (settings, options); var timer = []; // timer var marquee; // The scroll (function) var isRoll; // determines whether to scroll (function) var _ ul = $ ("> ul", root); // ul label var _ li = $ ("> ul> li", root ); // li tag (SET) var li_num = _ li. length; // Number of li tags var li_first = _ li. first (); // obtain a single li tag // determine whether it is vertical or horizontal, and perform the corresponding operation if (settings. direction = "x "){
Var li_w = li_first.outerWidth (true); // The width of a single li label
Var ul_w = li_w * li_num; // The width of the ul label.
_Ul.css ({width: ul_w}); // set ul width marquee = function () {_ ul. animate ({marginLeft: "-= 1"}, 0, function () {var _ mleft = math.abs(parseint(detail (this).css ("margin-left"); if (_ mleft> li_w) {// The scroll length is greater than the length of a single li $ ("> li: first", $ (this )). appendTo ($ (this); // move the first li to the last vertex (this).css ("margin-left", 0); // The scroll length is 0 }});}; // if the ul length is smaller than the box length, the scroll isRoll = function (t) {if (ul_w <= root. width () clearInterval (t); else marquee () ;}} else {
Var li_h = li_first.outerHeight (true); // the height of a single li label
Var ul_h = li_h * li_num; // ul label height _ul.css ({height: ul_h}); // set ul height marquee = function () {_ ul. animate ({marginTop: "-= 1"}, 0, function () {var _ mtop = math.abs(parseint(%(this%.css ("margin-top "))); // obtain the absolute value if (_ mtop> li_h) {$ ("> li: first", $ (this )). appendTo ($ (this); then (this).css ("margin-top", 0) ;}};}; // if the length of ul is smaller than the length of box, no scrolling is performed, roll isRoll = function (t) {if (ul_h <= root. height () clearInterval (t); else marquee () ;}// follow the chained principle and initialize return root. each (function (I) {// beyond the content to hide, Prevent Users From Writing overflow styles (this).css ({overflow: "hidden"}); timer [I] = setInterval (function () {isRoll (timer [I]) ;}, settings. speed); // move the mouse to stop scrolling, and then move to continue scrolling $ (this ). hover (function () {clearInterval (timer [I]) ;}, function () {timer [I] = setInterval (function () {isRoll (timer [I]) ;}, settings. speed) ;}) ;};}}( jQuery );

 

The basic code description is clearly annotated. Next we will explain some knowledge points:

1 .) var timer = []; previously, timer was not declared as an array type, but when I wrote a demo, because there are two seamless scrolling applications on the page at the same time (to demonstrate both horizontal and vertical), a bug occurs.

Because the two of them share a timer, when the mouse enters one of them, the other timer is also clear. Then modify the code to declare it as an array object, and then use root. each () to implement

Every plug-in application has its own independent timer, which does not affect each other. That is to say, this plug-in supports multiple seamless scrolling applications on the page at the same time.

 

2.) outerWidth ()/outerHeight () function. This function is powerful, and it not only obtains the width/height of elements,

Actually outerWidth () = width + borderLeft + borderRight + marginLeft + marinRight;

When it is set to true, that is, outerWidth (true), it will also calculate the padding:

OuterWidth () = width + borderLeft + borderRight + marginLeft + marinRight + paddingLeft + paddingRight;

How about it? Is it very powerful!

 

The DEMO code is provided below:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <title> </title> <style type =" text/css "> * {margin: 0; padding: 0;} ul, ul li {list-style: none ;}. wrap {width: 1000px; margin: 50px auto ;}. box1 ,. box2 ,. box3 {overflow: hidden; float: left; border: 1px solid gray ;}. box1 {width: 200px; height: pixel PX ;}. box1 ul li {width: 200px; height: 100px ;}. box2 ,. box3 {width: pixel PX; height: 150px; margin: 40px ;}. box2 ul li ,. box3 ul li {width: 100px; height: 150px; float: left ;} </style> 

 

Slice:

 

Haha, it looks ugly, but the function is still okay.

 

  

 


How to Use jquery seamless rolling plug-in

Www.open-lib.com/Type/146-1.jsp

Is that true?
Slides?

How to Use and change the jquery plug-in?

If jQuery plug-ins want to use it, there are usually demos or APIs in the plug-ins for reference.
JQuery plug-ins usually release two versions of XXX. js and XXX. min. js to reduce the volume.

If you want to modify the plug-in, you need to use the XXX. js file.

First, you need to know how to use it. First, there is an available demo (you can write it yourself without a demo), and then you can use the webkit kernel or firefox for breakpoint viewing, this is mainly to find the plug-in entry point. Of course, you can also directly view the js Code, which requires a certain degree of foundation.
The last step is to slowly check the implementation function of his code. First, you have to understand how he implemented it, and then you will know how to change it.

I have also written many jQuery plug-ins like simulating alert/confirm/prompt error message prompt boxes to simulate the seamless marquee scrolling pagination controls to drag controls and so on.

The framework of jQuery plug-in is usually written

(Function ($ ){
$. Fn. extend ({
FnKey: function (){}
})
// Or
$. Fn. fnKey = function (){}
}) (JQuery)

The preceding two methods of calling are as follows:

$ ("XXX"). fnKey () for calling

There is also a way to write:
Var fnClass = function (){
This. fnKey = function (){
}
This. props = "";
}

In this way, the call method is
FnClass obj = new fnClass ();
Obj. fnKey ();

The second method is JavaScript object-oriented programming, which requires you to understand it slowly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.