一、JS向上滚动的文字特效代码
这个向上滚动的文字特效JS代码比较简洁,代码量很少,你可以根据具体情况使用,做一个下载链接滚动的效果
<!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> </head> <body> <div class =" sidebox_bd bdbtm bdgray"> <c:if test="${null!=attForm&&6 >attForm.size()}">//第一种情况,正常显示List中的对象(在类中获取,再set到request) <ul class = "download_list"> <c:forEach items="${attForm}" var="pur"> <li> <a href="javascript:downloadFile('${pur.pk_file}','${pur.file_name}');">${pur.file_name}</a> </li> </c:forEach> </ul> </c:if> <c:if test="${null!=attForm&&5 <attForm.size()}"> <div id="demo" style="overflow:hidden;height:121px;width:217px;position:absolute;border:1px solid #e2e3ea;"> <ul id="demo1" class = "download_list2"> <c:forEach items="${attForm}" var="pur"> <li> <a href="javascript:downloadFile('${pur.pk_file}','${pur.file_name}');">${pur.file_name}</a> </li> </c:forEach> </ul> <ul id="demo2" class = "download_list2"></ul> </div> </c:if> </div> <script style="text/javascript"> //JS无缝向上滚动20170810 var speed=30;//数值越大,速度越慢 var demo2=document.getElementById("demo2"); var demo1=document.getElementById("demo1"); var demo=document.getElementById("demo"); demo2.innerHTML=demo1.innerHTML; function MarqueeUp(){ if(demo2.offsetTop-demo.scrollTop <= 0){ demo.scrollTop-=demo1.offsetHeight; } else{ demo.scrollTop++; } } var MyMar=setInterval(MarqueeUp,speed); demo.onmouseover=function() {clearInterval(MyMar);} demo.onmouseout=function() {MyMar=setInterval(MarqueeUp,speed);} </script> </body> </html>
这段代码的核心处理是应用setInterval每speed(30)毫秒执行一次页面更新.更新的函数是Marquee,通常执行的是demo.scrollTop++,也就是说通常情况下是每30毫秒向下移动一个像素,这样子连续起来就成了动画了.当动画移动到边界时,执行的时marquee里面的另外一个分支,将动画移动到最初位置.实现无缝,其实是借用了两个相同的dom块(demo1和demo2),两者同时向下移动,一个移出可视范围多少,另外一个就移入可是范围多少。
遇到的问题及解决方法:
JS无缝滚动只滚动2次就停止了:
1、当元素的父容器没有指定定位方式时,指元素与body元素之间的偏移距离;
2、当对父容器指定定位方式(如:position:relative;)时,则指元素与父容器之间的偏移距离;
所以,当没有指定定位方式时,代码中的colee2.offsetTop值已经是元素colee2与body元素之间的偏移距离了。当这部分代码置入页面上方top:0处时,自然没有问题。但是,如果插入到页面下方时,colee2.offsetTop值就已经不再是top:0了,自然就出现位移偏差了,以致滚动执行不正确!所以只需在colee的css样式中加入"position:absolute"即可。
即:<div id="colee" style="overflow:hidden;height:100px;width:200px;position:absolute;">以上希望对遇到同样问题的朋友有所帮助。
JS无缝滚动只滚动1次就停止了:
手动写死的数据要超过你的窗口高度,需要内容比窗口大才能循环滚动。
二、JSP中marquee标签向上滚动
<marquee scrollAmount=2 width=450>实现滚动消息-----</marquee>
参数详解:
a)scrollAmount。它表示速度,值越大速度越快。如果没有它,默认为6,建议设为1~3比较好。
b)width和height,表示滚动区域的大小,width是宽度,height是高度。特别是在做垂直滚动的时候,一定要设height的值。
c)direction。表示滚动的方向,默认为从右向左:可选的值有right、down、up。
d)scrollDelay,这也是用来控制速度的,默认为90,值越大,速度越慢。通常scrollDelay是不需要设置的。
e)behavior。用它来控制属性,默认为循环滚动,可选的值有alternate(交替滚动)、slide(幻灯片效果,指的是滚动一次,然后停止滚动)
实现消息停顿: 光标悬浮时停顿,光标离开时开始
<marquee onmouseout=this.start() onmouseover=this.stop() behavior="scroll" direction="up" width="660px" height="80px" SCROLLDELAY="320" >实现滚动消息-----</marquee>
参考文献:
http://bbs.csdn.net/topics/390174892
http://blog.csdn.net/my773962804/article/details/51681535
https://zhidao.baidu.com/question/1447404431243396780.html
http://www.51xuediannao.com/js/texiao/xiangshanggundong.html
http://blog.csdn.net/xttxqjfg/article/details/7873179