Pure JavaScript to achieve seamless scrolling effect of four directional text

Source: Internet
Author: User
Tags prev relative setinterval

This article is mainly to share the use of pure JavaScript to achieve the control of four directional text seamless scrolling code, the effect is very good, the need for small partners can refer to.

To achieve a seamless text scrolling effect:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 <! DOCTYPE html> <!--[if LT IE 7]> <html lang= "ZH-CN" class= "IE6" > <! [endif]--> <!--[if IE 7]> <html lang= "ZH-CN" class= "IE7" > <! [endif]--> <!--[if IE 8]> <html lang= "ZH-CN" class= "IE8" > <! [endif]--> <!--[if IE 9]> <html lang= "ZH-CN" class= "IE9" > <! [endif]--> <!--[if (GT IE 9) |! (IE)] ><!--> <html lang= "ZH-CN" > <!--<! [endif]--> <head> <title> text scrolling </title> <meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 "/>   <style type=" text/css "> *{margin:0;padding:0;} body{padding:20px; Textbox{border : 1px solid #ddd; width:auto;overflow:hidden;} . textbox ul{list-style:none;position:relative; textbox ul li{padding:5px 0; </style> </head> <body cl ass= "Home-page" >   <div id= "textbox" class= "TextBox" > <ul> <li> Auto | Sports B-Class car drop 30,005 </li> <li> Home | Such a great decoration woman really can't sit. </li> <li> Education | The first three candidates for the provinceChinese laborers handsomely 100,000 yuan/person </li> <li> Auto | Sports B-Class car drop 30,005 parallel import car fare increase 110,000 </li> <li> Health | The 14-Year-old boy is not long in 10 years of abuse of hormones </li> <li> Digital | The latest mobile phone quotes say good broadband drop fee speed? </li> <li> Auto | Parallel import car fare increase 110,000 </li> <li> Auto | Sports B-Class car drop 30,000 5</li> <li> car | Parallel import car fare increase 110,000 </li> <li> Sports | Evergrande's life and death war guess score win genuine jerseys </li> </ul> <a href= "#" class= "Btnprev" > Left </a> <a href= "#" class= "Btnnext" > Right </a> </div> <br> <br> <div id= "TextBox2" class= "textbox" > <ul> <li> Auto | Sports B-Class car drop 30,005 </li> <li> Home | Such a great decoration woman really can't sit. </li> <li> Education | The top three candidates for Chinese handsomely 100,000 yuan/person </li> <li> Auto | Sports B-Class car drop 30,005 parallel import car fare increase 110,000 </li> <li> Health | The 14-Year-old boy is not long in 10 years of abuse of hormones </li> <li> Digital | The latest mobile phone quotes say good broadband drop fee speed? </li> <li> Auto | Parallel import car fare increase 110,000 </li> <li> Auto | Sports B-Class car drop 30,000 5</li> <li> car | Parallel import car fare increase 110,000 </li> <li> Sports | Evergrande's life and death war guess score win genuine jerseys </li> </ul> <a href= "#" class= "Btnprev" > Up </a> <a href= "#" CLass= "Btnnext" > Down </a> </div> <script type= "Text/javascript" src= "Script/jquery-1.11.1.min.js" > </script> <script type= "Text/javascript" >  //Four Direction seamless scrolling scroll (' #textbox ', {vis:2,btnhidden:false,dir : ' prev ', type: ' H '}; Scroll (' #textbox2 ', {vis:3,btnhidden:false,dir: ' prev ', type: ' V '});   function Scroll (container,options) {var box = $ (container); var boxul = Box.find (' ul '). EQ (0); var liheight = 0;//No Contains clone li list height var cloneliheight = 0; Contains clone li list height var liwidth = 0; Does not contain clone Li list width var cloneliwidth = 0; Contains clone Li list width var Lis = Boxul.children (); var Btnprev = Box.find ('. Btnprev '); var btnnext = Box.find ('. Btnnext ');  //default parameter var defult= {vis:2,//Display number autoplay:true,//Auto play speed:50,//scrolling speed dir: ' prev ',//scrolling direction btnhidden:false,// Whether the button hides type: ' V '//horizontal or vertical  }; var opt = $.extend ({},defult,options);    //Building DOM structure var lastclone=0; Finally clone element Var lastcloneheight=0;//the last clone element height var allcloneheight=0;//all clone element total height var lastclonewidth=0; var Allclonewidth=0; var visheight=0; Visual height var viswidth=0; var boxul_wrap;   if (Opt.type = = "V") {//Vertical direction Lis.each (function () {$ (this). Height ($ (this). Height ()); Liheight + = $ (this). Outerheight (True); }); Lastclone= Boxul.children (': Last '). Clone (). addclass (' clone '). Prependto (BOXUL); Lastcloneheight = Lastclone.outerheight (true); Allcloneheight = Lastclone.outerheight (true);   for (var i = 0; i < Opt.vis i++) {lis.eq (i). Clone (). addclass (' clone '). Appendto (BOXUL); allcloneheight = Lis.eq (i). Outerheight (True); }   visheight = Allcloneheight-lastcloneheight; Cloneliheight = Liheight + allcloneheight;   Boxul_wrap = $ (' <div></div> '). css ({' width ': ' 100% ', ' height ': visheight, ' overflow ': ' Hidden ', ' Position ': ' relative '}). attr (' id ', ' ulwrap '); Boxul.css ({' Height ': cloneliheight, ' top ':-lastcloneheight}). Wrap (Boxul_wrap);  }else if (Opt.type = = "H") {//Horizontal direction lis.css ({' whitespace ': ' nowrap ', ' float ': ' Left ', ' paddingright ': ' 10px '}); Lis.each (function () {$ (this). Width (.). width ()); Liwidth + = $ (this). Outerwidth (True); });   lastclone= Boxul.children (': Last '). Clone (). addclass (' clone '). Prependto (BOXUL); Lastclonewidth= Lastclone.outerwidth (TRUE); Allclonewidth = Lastclone.outerwidth (true);   for (var j = 0; J < Opt.vis J + +) {Lis.eq (j). Clone (). addclass (' clone '). Appendto (Boxul); Allclonewidth + = Lis.eq ( j). Outerwidth (True); } visheight = Lis.eq (0). Outerheight (True); Viswidth = Allclonewidth-lastclonewidth; Cloneliwidth = Liwidth + allclonewidth;   Boxul_wrap = $ (' <div></div> '). css ({' width ': viswidth, ' height ': visheight, ' overflow ': ' Hidden ', ' Position ': ' relative '}). attr (' id ', ' ulwrap '); Boxul.css ({' width ': cloneliwidth, ' Left ':-lastclonewidth}). Wrap (Boxul_wrap); Box.css ({' Width ': viswidth}); }    //Add event handling var timer = null; var scrolltop = function () {clearinterval (timer); timer = setinterval (function () {var tmp = parseint boxul.css (' top '). Rep Lace (' px ', "")); tmp--; boxul.animate {' Top ': tmp},0,function () {if (TMP <=-liheight + lastcloneheight)) {boxul.css (' top ',-lastcloneheight);}}); },opt.speed); };   var scrolldown = function () {clearinterval (timer); timer = setinterval (function () {var tmp = parseint boxul.css (' to P '). Replace (' px ', "")); tmp++; Boxul.animate ({' Top ': tmp},0,function () {if (tmp >= 0) {boxul.css (' top ',-(Liheight));}); },opt.speed); };   var scrollleft = function () {clearinterval (timer); timer = setinterval (function () {var tmp = parseint (boxul.css (' Le FT '). Replace (' px ', ' ")); tmp--; Boxul.animate ({' Left ': tmp},0,function () {if (TMP <=-(liwidth + lastclonewidth)) {boxul.css ("left '),-( Lastclonewidth)); } }); },opt.speed); };   var scrollright = function () {clearinterval (timer); timer = setinterval (function () {var tmp = parseint (Boxul.css (' l EFT '). Replace (' px ', "")); tmp++; Boxul.animate ({' Left ': tmp},0,function () {if (tmp >= 0) {boxul.css (' left ',-(Liwidth));}); },opt.speed); };   var scrolltype = function (Type,dir) {if (lis.length >= Opt.vis) {//display number cannot be more than list number, otherwise no effect will be displayed var sdir = typeof dir!== "undefined"? Dir:opt.dir; Switch (type) {case "V": if (Sdir = = "Prev") {scrolltop ();} Else{scrolldown ();} break; Case "H": if (Sdir = = "Prev") {scrollleft ();} Else{scrollright ();} } };     if (opt.autoplay) {scrolltype (opt.type);}  //Add Event handling Box.find (' #ulWrap '). Hover (function () { Clearinterval (timer); },function () {scrolltype (opt.type);});  //button whether to hide if (!opt.btnhidden) {  Btnprev.unbind (' mouseover '); Btnnext.unbind (' mouseover ');   Btnprev.mouseover (function () {Scrolltype (Opt.type, "prev");}); Btnnext.mouseover (function () {Scrolltype (Opt.type, Next);}); }else{btnprev.hide (); Btnnext.hide ();}  } </script> </body> </html>

Some questions:

There is no problem with local testing, but after document.write (), the height of Li in vertical mode () gets a problem. The reason is unknown, very puzzled.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.