具体的方法和思路可以看上一篇日志《向上无缝滚动的封装》:https://www.html.cn/article.asp?id=577
在向上无缝滚动的封装的基础上增加了向下,向左,向右的滚动,第二个参数是top、bottom、left、right;
参看演示:https://www.html.cn/demo/Marquee/scroll_text_ok_2.html
感谢demo的鼎力帮助,如果您对JS也敢兴趣,对我们封装的以下代码有意见建议,或者想和我一起探讨JS技术,那么加入我们,我们的qq群:7617538
XML/HTML代码
- <!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>??? ??
- <meta?http-equiv=“Content-Type”?content=“text/html;?charset=utf-8”?/>??? ??
- <title>向上无缝滚动的封装的演示</title>??? ??
- <style?type=“text/css”>??? ??
- <!–???? ??
- ol,ul{list-style:none;?margin:0;padding:0}???? ??
- li{?height:25px}??? ??
- #demo3?ul,#demo4?ul{float:left;?width:400px;} ??
- #demo3?li,#demo4?li{?float:left;?width:50px}? ??
- —>??? ??
- </style>??? ??
- </head>??? ??
- ??? ??
- <body>? ??
- <div?style=“?height:50px;?line-height:50px;?color:#FFFFFF;?background:#333333;?margin-bottom:20px;?padding-left:30px;?font-size:16px”>?向上无缝滚动的封装的演示-WEB前端开发DOME–<a?style=“color:#FFFFFF”?href=“http://www.9demo.com”>www.9demo.com</a>—<a?style=“color:#FFFFFF”?href=“https://www.html.cn”>www.css88.com</a></div>?? ??
- <p>这个是速度为10的滚动</p>??
- <div?id=“demo”?style=“?overflow:?hidden;?width:?322px;?height:?50px”>??
- ????<ul>??? ??
- ????????<li>111</li>??? ??
- ????????<li>222</li>??? ??
- ????????<li>333</li>??? ??
- ????????<li>444</li>??? ??
- ????????<li>555</li>??? ??
- ????????<li>666</li>??? ??
- ????????<li>777</li>??? ??
- ????????<li>888</li>??? ??
- ????</ul>??
- </div>??
- <hr?/>??
- <p>这个是速度为50的向下滚动</p>??
- <div?id=“demo2”??style=“?overflow:?hidden;?width:?322px;?height:?50px”>??
- ????<ul>??? ??
- ????????<li>aaa</li>??? ??
- ????????<li>bbb</li>??? ??
- ????????<li>ccc</li>??? ??
- ????????<li>ddd</li>??? ??
- ????????<li>eee</li>??? ??
- ????????<li>fff</li>??? ??
- ????????<li>ggg</li>??? ??
- ????????<li>hhh</li>??? ??
- ????</ul>??
- </div>? ??
- <hr?/>??
- <p>这个是速度为10的向左滚动</p>??
- <div?id=“demo3”?style=“overflow:?hidden;?width:?100px;?height:?25px”>??
- ????<ul>??? ??
- ????????<li>111</li>??? ??
- ????????<li>222</li>??? ??
- ????????<li>333</li>??? ??
- ????????<li>444</li>??? ??
- ????????<li>555</li>??? ??
- ????????<li>666</li>??? ??
- ????????<li>777</li>??? ??
- ????????<li>888</li>??? ??
- ????</ul>??
- </div>??
- <hr?/>??
- <p>这个是速度为50的向右滚动</p>??
- <div?id=“demo4”?style=“overflow:?hidden;?width:?100px;?height:?25px”>??
- ????<ul>??? ??
- ????????<li>aaa</li>??? ??
- ????????<li>bbb</li>??? ??
- ????????<li>ccc</li>??? ??
- ????????<li>ddd</li>??? ??
- ????????<li>eee</li>??? ??
- ????????<li>fff</li>??? ??
- ????????<li>ggg</li>??? ??
- ????????<li>hhh</li>??? ??
- ????</ul>??
- </div>?? ??
- <script?type=“text/javascript”>??
- function?next(elem){???? ??
- ????????do{???? ??
- ??????????? elem=elem.nextSibling;???? ??
- ????????}while(elem&&elem.nodeType!=1);???? ??
- ????????return?elem;???? ??
- ????}???? ??
- ????//—查找第一个子元素的函数—//???? ??
- ????function?first(elem){???? ??
- ??????? elem=elem.firstChild;???? ??
- ????????return?elem?&&?elem.nodeType!=1?next(elem):elem;???? ??
- ????} ??
- ???? ??
- ????var?Marquee?=?{ ??
- ????????init:?function(id,?toFollow,?speed){ ??
- ????????????this.speed?=?speed?||?10; ??
- ????????????this.boxID?=?id; ??
- ????????????this.toFollow?=?toFollow; ??
- ????????????this.scrollBox?=?document.getElementById(id); ??
- ???????????? ??
- ???????????? ??
- ????????????if?(this.toFollow?==?”top”?||?this.toFollow?==?”bottom”)?{ ??
- ????????????????this.appendBox?=?first(this.scrollBox).cloneNode(true); ??
- ????????????????this.scrollBox.appendChild(this.appendBox); ??
- ????????????} ??
- ????????????else?{ ??
- ????????????????var?templateLeft?=?“<table?cellspacing=’0′?cellpadding=’0′?style=’border-collapse:collapse;display:inline;’><tr><td?noWrap=true?style=’white-space:?nowrap;word-break:keep-all;’>”?+?this.scrollBox.innerHTML?+?”</td><td?noWrap=true?style=‘white-space:?nowrap;word-break:keep-all;’>“?+?this.scrollBox.innerHTML?+?”</td></tr></table>“; ??
- ????????????????this.scrollBox.innerHTML?=?templateLeft; ??
- ????????????????this.appendBox?=?first(first(first(first(this.scrollBox)))); ??
- ???????????????? ??
- ????????????} ??
- ????????????this.objs?=?{ ??
- ????????????????scrollBox:?this.scrollBox, ??
- ????????????????appendBox:?this.appendBox, ??
- ????????????????toFollow:?this.toFollow, ??
- ????????????????speed:?this.speed, ??
- ????????????????id:?this.boxID ??
- ????????????}; ??
- ????????????return?this; ??
- ????????}, ??
- ???????? ??
- ????????scroll:?function(){ ??
- ????????????var?self?=?this.objs; ??
- ????????????self.begin?=?function(){ ??
- ???????????? ??
- ????????????????switch?(self[‘toFollow’])?{ ??
- ????????????????????case?”top”: ??
- ????????????????????????self.doScr?=?setInterval(function(){ ??
- ????????????????????????????if?(self[‘appendBox’].offsetHeight?<=?self[‘scrollBox’].scrollTop)?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollTop?–=?first(self[‘scrollBox’]).offsetHeight; ??
- ????????????????????????????} ??
- ????????????????????????????else?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollTop++; ??
- ????????????????????????????} ??
- ????????????????????????},?self.speed); ??
- ????????????????????????break; ??
- ????????????????????case?”bottom”: ??
- ????????????????????????self.doScr?=?setInterval(function(){ ??
- ????????????????????????????if?(self[‘scrollBox’].scrollTop?<=?0)?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollTop?=?self[‘appendBox’].offsetHeight; ??
- ????????????????????????????} ??
- ????????????????????????????else?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollTop–; ??
- ????????????????????????????} ??
- ????????????????????????},?self.speed); ??
- ????????????????????????break; ??
- ????????????????????case?”left”: ??
- ????????????????????????self.doScr?=?setInterval(function(){ ??
- ????????????????????????????if?(self[‘appendBox’].offsetWidth?<=?self[‘scrollBox’].scrollLeft)?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollLeft?–=?self[‘appendBox’].offsetWidth; ??
- ????????????????????????????} ??
- ????????????????????????????else?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollLeft++; ??
- ????????????????????????????} ??
- ????????????????????????},?self.speed); ??
- ????????????????????????break; ??
- ????????????????????case?”right”: ??
- ????????????????????????self.doScr?=?setInterval(function(){ ??
- ????????????????????????????if?(self[‘scrollBox’].scrollLeft?<=?0)?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollLeft?=?self[‘appendBox’].offsetWidth; ??
- ????????????????????????????} ??
- ????????????????????????????else?{ ??
- ????????????????????????????????self[‘scrollBox’].scrollLeft–; ??
- ????????????????????????????} ??
- ???????????????????????????? ??
- ????????????????????????},?self.speed); ??
- ????????????????????????break; ??
- ????????????????????}??? ??
- ????????????????} ??
- ????????????????self.begin(); ??
- ????????????????self.scrollBox.onmouseover?=?function(){ ??
- ????????????????????clearInterval(self.doScr); ??
- ????????????????} ??
- ???????????????? ??
- ????????????????self.scrollBox.onmouseout?=?function(){ ??
- ????????????????????self.begin(); ??
- ????????????????} ??
- ????????????} ??
- ???????????? ??
- ????????} ??
- ???? ??
- ????Marquee.init(“demo”,”top”).scroll(); ??
- ????Marquee.init(“demo2″,”bottom”,?50).scroll(); ??
- ????Marquee.init(“demo3″,”left”).scrollUp(); ??
- ????Marquee.init(“demo4″,”right”,?50).scroll(); ??
- </script>?? ??
- ???? ??
- ? ??
- </body>??? ??
- </html>??? ??
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂