Example of seamless scrolling using native javascript _ javascript tips-js tutorial

Source: Internet
Author: User
Native javascript seamless scrolling currently supports vertical and horizontal scrolling. The following is a good example. You can refer to the currently supported vertical and horizontal scrolling.

Http://lgyweb.com/marScroll/

The following figure shows the basic idea for achieving seamless seams (vertical example ):

HTML structure:

The Code is as follows:




  • 01

  • 02

  • 03

  • 04

  • 05




CSS:

The Code is as follows:








(1) first, you need to determine whether the ul structure in the content is higher than the outer p # marScrolll, and then perform seamless rolling:

The Code is as follows:


// Write it in an anonymous function to prevent global variable contamination
(Function (){
Var target = document. getElementById ('marscroll '),
OUl = target. getElementsByTagName ('ul ') [0];
// If the content is small, exit the function directly.
If (oUl. offsetHeight })();


(2) The non-intersecret is the infinite scrolling display of the content. First, you need to copy the content, and then use the scrollTop ++ attribute of the outer layer to execute it cyclically using the setInterval function.

The Code is as follows:


Target. innerHTML + = target. innerHTML;
/* When the distance between each scroll is equal to the height of the first ul, set scrollTop to 0, and then such a cyclic operation is to scroll without any errors.
Optional ---------------------------------------------------------------------------------------------*/
// Extract the function to facilitate calling.
Var fn = function (){
If (target. scrollTop = oUl_h ){
Target. scrollTop = 0;
} Else {
Target. scrollTop ++;
}
}
// SetInterval Loop
Var timer = setInterval (function (){
Fn ();
}, 30 );


(3) When the mouse passes through the content block, the scroll stops.

The Code is as follows:


// Hover
Target. onmouseover = function (){
ClearTimeout (timer );
}
Target. onmouseout = function (){
Timer = setInterval (function (){
Fn ();
}, 30 );
}


Sample JS Code:

The Code is as follows:


// Write it in an anonymous function to prevent global variable contamination
(Function (){
Var target = document. getElementById ('marscroll '),
OUl = target. getElementsByTagName ('ul ') [0],
OUl_h = oUl. offsetHeight;
// If the content is small, exit the function directly.
If (oUl_h
Target. innerHTML + = target. innerHTML;

/* When the distance between each scroll is equal to the height of the first ul, set scrollTop to 0, and then such a cyclic operation is to scroll without any errors.
Optional ---------------------------------------------------------------------------------------------*/
// Extract the function to facilitate calling.
Var fn = function (){
If (target. scrollTop = oUl_h ){
Target. scrollTop = 0;
} Else {
Target. scrollTop ++;
}
}
// SetInterval Loop
Var timer = setInterval (function (){
Fn ();
}, 30 );
// Hover
Target. onmouseover = function (){
ClearTimeout (timer );
}
Target. onmouseout = function (){
Timer = setInterval (function (){
Fn ();
}, 30 );
}
})();


A simple vertical seamless slit has been completed. To meet more requirements, it is recommended to encapsulate it into functions that can be called in vertical, horizontal, or multiple times.

The following is an online example of personal encapsulation:

Http://lgyweb.com/marScroll/

The Code is as follows:


Function GyMarquee (opt ){
This. opt = opt;
If (! Document.getElementById(this.opt.tar getID) return;
This.tar get = document.getElementById(this.opt.tar getID );
This. dir = this. opt. dir = 'crosswise '? 'Crosswise ': 'version ';
This. effect = this. opt. effect = 'scroll '? 'Scroll ': 'marque ';
This. scrollHeight = this. opt. scrollHeight;
This. init ();
}
GyMarquee. prototype = {
Marquee: function (){
Var _ that = this,
Direction = 'scrolltop ',
Judge = this.tar get. scrollHeight,
Timer = null;
If (this. dir = 'crosswise '){
Direction = 'rollleft ';
Judge = this. itemLen * this. opt. itemWidth;
This.tar getChild. style. width = this. itemLen * this. opt. itemWidth * 2 + 'px ';
}
Var doFn = function (){
If(_that.tar get [direction] = judge ){
_That.tar get [direction] = 0;
}
_That.tar get [direction] ++;
}
Timer = setInterval (function (){
DoFn ();
}, 38 );
This.tar get. onmouseover = function (){
If (timer) clearTimeout (timer );
}
This.tar get. onmouseout = function (){
Timer = setInterval (function (){
DoFn ();
}, 38 );
}
},
Scroldrop: function (){
Var can = true,
_ That = this;
This.tar get. onmouseover = function () {can = false };
This.tar get. onmouseout = function () {can = true };
New function (){
Var stop=_that.tar get. scrollTop % _ that. scrollHeight = 0 &&! Can;
If (! Stop)_that.target.scrolltopheight partition parseint(_that.tar get. scrollHeight/2 )? _That.tar get. scrollTop = 0: _that.tar get. scrollTop ++;
SetTimeout(arguments.callee,_that.tar get. scrollTop % _ that. scrollHeight? 20:2500 );
};
},
GetByClassName: function (className, parent ){
Var elem = [],
Node = parent! = Undefined & parent. nodeType = 1? Parent. getElementsByTagName ('*'): document. getElementsByTagName ('*'),
P = new RegExp ("(^ | \ s)" + className + "(\ s | $ )");
For (var n = 0, I = node. length; n If (p. test (node [n]. className )){
Elem. push (node [n]);
}
}
Return elem;
},
Init: function (){
Var val = 0;
If (this. dir = 'crosswise '& this. effect = 'marque' & this. opt. itemName! = ''){
This. itemLen = this.tar get. getElementsByTagName (this. opt. itemName). length;
Val = this. itemLen * this. opt. itemWidth;
} Else {
Val = this.tar get. scrollHeight;
}
Var holderHTML = this.tar get. innerHTML;
This.tar get. innerHTML ='

'+ HolderHTML +'

';
This.tar getChild = this.getbyclassname('j_scrollinner', this.tar get) [0];
Var attr = this. dir = 'version '? 'Offsetheight': 'offsetwidth ';
If (val> this.tar get [attr]) {
If (this. effect = 'scroll '){
This. scroldrop ();
} Else {
This. marquee ();
}
This.tar getChild. innerHTML + = this.tar getChild. innerHTML;
}
}
}
Related Article

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.