当前位置:首页 >> 网络编程

基于javascript的无缝滚动动画实现2

以更少的代码实现相同的效果应该是我们写程序的究极目标,当然可读性不能丢。在基于javascript的无缝滚动动画实现1最后一个运行框,我已经做了这样的探索。不过换汤不换药,还是来来回回在scrollTop/scrollLeft与offsetTop/offsetLeft上做文章。总的思路基本是这样,让某个元素整体向某个方向移动,这样它里面的内容(图片或文字)就跟着移动,当元素移动到某一个距离后就回到原点。为了防止内容移着移着就没有了,我们需要两套相同的内容。在第一部分,第二套内容是动态生成的,并复制到另一个兄弟元素中,这在水平滚动时,我们需要花一些周折来让所有内容水平排列在一起。我就想,如果把第二套内容直接加在相同的元素是否效果会更好呢?起码CSS也少写一点。用scrollTop/scrollLeft实现滚动,还有一个缺点,它对父容器的宽度与高度有严格的规定的。比如我们想上下滚动图片,ul作为容器,li的图片为滚动内容,那么ul的高至少为图片的高的两倍(我们假设每张图片的尺寸都是相等的),否则就动不了。鉴此,我开发了新一代的无缝滚动函数。

<ul id="marquee">
 <li>
 <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">


#marquee ,#marquee li {
 padding:0;
 margin:0;
}
#marquee {
 position:relative;
 list-style:none;
 height:150px;
 width:200px;
 overflow:hidden;
 border:10px solid #369;
}
#marquee li {
 position:absolute;
}
#marquee img {
 display:block;
 border:0;
}
var Marquee = function(id){
 try{document.execCommand("BackgroundImageCache", false, true);}catch(e){};
 var container = document.getElementById(id),
 slide = container.getElementsByTagName("li")[0],
 speed = arguments[1] || 10,
 delta = 0,//当前滚动的位置
 critical = slide.offsetHeight;//临界值
 slide.innerHTML += slide.innerHTML;
 var rolling = function(){
 delta == -critical "px";
 }
 var timer = setInterval(rolling,speed)//设置定时器
 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动
 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器
}
window.onload = function(){
 Marquee("marquee");
}
 
<!doctype html>
<title>javascript无缝滚动 by 司徒正美</title>
<meta charset="utf-8"/>
<meta name="keywords" content="javascript无缝滚动 by 司徒正美" />
<meta name="description" content="javascript无缝滚动 by 司徒正美" />
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<style type="text/css">
 h1 {
 font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun
 }
 #marquee ,#marquee li {
 padding:0;
 margin:0;
 }
 #marquee {
 position:relative;
 list-style:none;
 height:150px;
 width:200px;
 overflow:hidden;
 border:10px solid #369;
 }
 #marquee li {
 position:absolute;
 }
 #marquee img {
 display:block;
 border:0;
 }
</style>
<script type="text/javascript">
 var Marquee = function(id){
 try{document.execCommand("BackgroundImageCache", false, true);}catch(e){};
 var container = document.getElementById(id),
 slide = container.getElementsByTagName("li")[0],
 speed = arguments[1] || 10,
 delta = 0,//当前滚动的位置
 critical = slide.offsetHeight;//临界点
 slide.innerHTML += slide.innerHTML;
 var rolling = function(){
  delta == -critical "px";
 }
 var timer = setInterval(rolling,speed)//设置定时器
 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动
 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器
 }
 window.onload = function(){
 Marquee("marquee");
 }
</script>
<h1>javascript无缝滚动(向上滚动) by 司徒正美</h1>
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<ul id="marquee">
 <li>
 <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">

向下滚动也很容易,只不过一开始把它定位到第一套内容与第二套内容之间,也就是slide.offsetHeight,不过我们需要把它改成负数让向整体向下移动,然后再慢慢向上移动。

 
<!doctype html>
<title>javascript无缝滚动 by 司徒正美</title>
<meta charset="utf-8"/>
<meta name="keywords" content="javascript无缝滚动 by 司徒正美" />
<meta name="description" content="javascript无缝滚动 by 司徒正美" />
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<style type="text/css">
 h1 {
 font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun
 }
 #marquee ,#marquee li {
 padding:0;
 margin:0;
 }
 #marquee {
 position:relative;
 list-style:none;
 height:150px;
 width:200px;
 overflow:hidden;
 border:10px solid #B45B3E;
 }
 #marquee li {
 position:absolute;
 }
 #marquee img {
 display:block;
 border:0;
 }
</style>
<script type="text/javascript">
 var Marquee = function(id){
 try{document.execCommand("BackgroundImageCache", false, true);}catch(e){};
 var container = document.getElementById(id),
 slide = container.getElementsByTagName("li")[0],
 speed = arguments[1] || 10,
 critical = slide.offsetHeight,//临界值
 delta = -critical;
 slide.innerHTML += slide.innerHTML;
 var rolling = function(){
  delta == 0 "px";
 }
 var timer = setInterval(rolling,speed)//设置定时器
 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动
 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器
 }
 window.onload = function(){
 Marquee("marquee");
 }
</script>
<h1>javascript无缝滚动(向下滚动) by 司徒正美</h1>
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<ul id="marquee">
 <li>
 <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">

实现水平移动也比第一部分容易,结构层完全不用改动,表现层也改动很少。

#marquee ,#marquee li {
 padding:0;
 margin:0;
}
#marquee {
 position:relative;
 list-style:none;
 height:150px;
 width:200px;
 overflow:hidden;
 border:10px solid #B45B3E;
}
#marquee li {
 position:absolute;
 width:1000%;/*★★新增加★★*/
}
#marquee a {
 float:left;/*★★新增加★★*/
}
#marquee img {
 display:block;
 border:0;
}

难点是如何获取那个临界值,也就是夹在第一套内容与第二个套内容之间的那个位置。由于第二套内容我们并没有外套一个元素,因此我们需要从滚动对象下手。滚动对象都是a元素,我们需要计算出第二套内容的第一个a元素到达父元素左边的距离。这个值就是我们要求的临界值。明白这点,就轻松了。

 //*************略********* 
 slide.innerHTML += slide.innerHTML;
 var item = slide.getElementsByTagName("a"),
 critical = item[item.length/2].offsetLeft,//临界值
//*************略********* 
 
<!doctype html>
<title>javascript无缝滚动 by 司徒正美</title>
<meta charset="utf-8"/>
<meta name="keywords" content="javascript无缝滚动 by 司徒正美" />
<meta name="description" content="javascript无缝滚动 by 司徒正美" />
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<style type="text/css">
 h1 {
 font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun
 }
 #marquee ,#marquee li {
 padding:0;
 margin:0;
 }
 #marquee {
 position:relative;
 list-style:none;
 height:150px;
 width:200px;
 overflow:hidden;
 border:10px solid #8080C0;
 }
 #marquee li {
 position:absolute;
 width:1000%;/*新增加*/
 }
 #marquee a {
 float:left;/*新增加*/
 }
 #marquee img {
 display:block;
 border:0;
 }
</style>
<script type="text/javascript">
 var Marquee = function(id){
 try{document.execCommand("BackgroundImageCache", false, true);}catch(e){};
 var container = document.getElementById(id),
 slide = container.getElementsByTagName("li")[0],
 speed = arguments[1] || 10;
 slide.innerHTML += slide.innerHTML;
 var item = slide.getElementsByTagName("a"),
 critical = item[item.length/2].offsetLeft,//临界值
 delta = 0;
 var rolling = function(){
  delta == -critical "px";
 }
 var timer = setInterval(rolling,speed)//设置定时器
 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动
 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器
 }
 window.onload = function(){
 Marquee("marquee");
 }
</script>
<h1>javascript无缝滚动(向左滚动) by 司徒正美</h1>
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<ul id="marquee">
 <li>
 <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">

下面是向右滚动,由于都是不足20行的脚本,应该一看源码就明白,不详述了。

 
<!doctype html>
<title>javascript无缝滚动 by 司徒正美</title>
<meta charset="utf-8"/>
<meta name="keywords" content="javascript无缝滚动 by 司徒正美" />
<meta name="description" content="javascript无缝滚动 by 司徒正美" />
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<style type="text/css">
 h1 {
 font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun
 }
 #marquee ,#marquee li {
 padding:0;
 margin:0;
 }
 #marquee {
 position:relative;
 list-style:none;
 height:150px;
 width:200px;
 overflow:hidden;
 border:10px solid #6cc;
 }
 #marquee li {
 position:absolute;
 width:1000%;
 }
 #marquee a {
 float:left;
 }
 #marquee img {
 display:block;
 border:0;
 }
</style>
<script type="text/javascript">
 var Marquee = function(id){
 try{document.execCommand("BackgroundImageCache", false, true);}catch(e){};
 var container = document.getElementById(id),
 slide = container.getElementsByTagName("li")[0],
 speed = arguments[1] || 10;
 slide.innerHTML += slide.innerHTML;
 var item = slide.getElementsByTagName("a"),//临界点
 critical = item[item.length/2].offsetLeft,
 delta = -critical;
 var rolling = function(){
  delta == 0 "px";
 }
 var timer = setInterval(rolling,speed)//设置定时器
 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动
 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器
 }
 window.onload = function(){
 Marquee("marquee");
 }
</script>
<h1>javascript无缝滚动(向右滚动) by 司徒正美</h1>
<base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<ul id="marquee">
 <li>
 <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">

我们可以把上面四个函数合并成一个函数,结构层与表现层参照向上滚动。

var Marquee = function(id){
 try{document.execCommand("BackgroundImageCache", false, true);}catch(e){};
 var container = document.getElementById(id),
 slide = container.getElementsByTagName("li")[0],
 options = arguments[1] || {},
 speed = options.speed || 10,
 direction = options.direction || "left";
 slide.innerHTML += slide.innerHTML;
 var item = slide.getElementsByTagName("a"),
 critical,delta,rolling,field;
 if(direction == "left" || direction == "right"){
 slide.style.cssText = "width:1000%";
 for(var i=0 ,l=item.length; i<l;i++){
  item[i].style.cssText = "float:left";
 }
 field = "left";
 critical = item[item.length/2].offsetLeft;
 }else if(direction == "up" || direction == "down"){
 field = "top";
 critical = item[item.length/2].offsetTop;
 }
 if(direction == "up" || direction == "left"){
 delta = 0;
 rolling = function(){
  delta == -critical "px";
 }
 }else if(direction == "down" || direction == "right"){
 delta = -critical;
 rolling = function(){
  delta == 0 "px";
 }
 }
 var timer = setInterval(rolling,speed)//设置定时器
 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动
 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器
}

使用:

Marquee("marquee",{speed:35,direction:"up"});

有了这个我们实现另一种图片轮换就容易了,这里先做个预告吧。本文到此为止。