用div实现向左右无缝滚动图片效果
2008年07月05日 星期六 上午 11:01
div+css学习笔记22(用div实现向左无缝滚动图片效果)JavaScript实现走马灯效果[无缝连接、循环滚动] 无缝跑马灯效果

以下代码在IE6、Firefox+Win2k环境下测试通过

网页走马灯连续循环滚动

废话少说,代码贴出来:

<div id="imgmarquee" style="OVERFLOW: hidden; WIDTH: 580px; align: left; background-color:#0099CC;">

<div style="width:1200px">

<!--id="marquePic1"里面的宽度一定要大于id="imgmarquee"的宽度才能看到效果 width:600px;>WIDTH: 580px;

大的div宽度要为span的两倍才可以,不然会换行width:1200px zdz的作品,流风的作品 -->

   <span id="marquePic1" style="width:600px; background-color:#990033;">

   <img src="../images/dialog/4.gif" />

   <img src="../images/dialog/4.gif" />

   <img src="../images/dialog/4.gif" />

   <img src="../images/dialog/4.gif" />

   <img src="../images/dialog/4.gif" />

   <img src="../images/dialog/4.gif" />

   <img src="../images/dialog/4.gif" />

   <img src="../images/dialog/4.gif" />

   </span>

   <span id="marquePic2" style="width:600px;background-color:#990033;"></span>

</div>

</div>

<script type="text/javascript">

var imgmarquee = document.getElementById('imgmarquee');

var marquePic2 = document.getElementById('marquePic2');

var marquePic1 = document.getElementById('marquePic1');

var speed=10;//控制移动的速度,数越大越慢

marquePic2.innerHTML=marquePic1.innerHTML;//把marquePic1的内容复制到marquePic2里面

function Marquee(){

if(imgmarquee.scrollLeft>=marquePic1.scrollWidth){

imgmarquee.scrollLeft=0;

}else{

//demo.scrollLeft++;

imgmarquee.scrollLeft++;

}

}

var marqueetemp=setInterval(Marquee,speed);

imgmarquee.onmouseover=function() {clearInterval(marqueetemp)} //鼠标移到上面停止并清除计数

imgmarquee.onmouseout=function() {marqueetemp=setInterval(Marquee,speed)} //鼠标离开重新计数

</script>

------------

参考资料(向右,向上,向下代码同理,只是控制的js不同而已,把下面的id改成对应的就ok了)

------------

向右

------------

<script language="javascript"

type="text/javascript">

<!--

var demo = document.getElementById("demo");

var demo1 = document.getElementById("demo1");

var demo2 = document.getElementById("demo2");

var speed=10;     //数值越大滚动速度越慢

demo2.innerHTML = demo1.innerHTML;

demo.scrollLeft = demo.scrollWidth;

function Marquee(){

     if(demo.scrollLeft<=0)

         demo.scrollLeft+=demo2.offsetWidth

     else{

         demo.scrollLeft--

     }

}

var MyMar = setInterval(Marquee,speed)

demo.onmouseover = function(){clearInterval(MyMar)}

demo.onmouseout = function(){MyMar = setInterval(Marquee,speed)}

-->

</script>

-------------

向下

-------------

<script language="javascript"

type="text/javascript">

<!--

var demo = document.getElementById("demo");

var demo1 = document.getElementById("demo1");

var demo2 = document.getElementById("demo2");

var speed=10;     //数值越大滚动速度越慢

demo2.innerHTML = demo1.innerHTML;

demo.scrollTop = demo.scrollHeight;

function Marquee(){

     if(demo1.offsetTop-demo.scrollTop>=0)

         demo.scrollTop+=demo2.offsetHeight

     else{

         demo.scrollTop--

     }

}

var MyMar = setInterval(Marquee,speed);

demo.onmouseover = function(){clearInterval(MyMar)}

demo.onmouseout = function(){MyMar = setInterval(Marquee,speed)}

-->

</script>

-----------

向上

-----------

<script language="javascript"

type="text/javascript">

<!--

var demo = document.getElementById("demo");

var demo1 = document.getElementById("demo1");

var demo2 = document.getElementById("demo2");

var speed=10;     //滚动速度值,值越大速度越慢

demo2.innerHTML = demo1.innerHTML     //克隆demo2为demo1

function Marquee(){

     if(demo2.offsetTop-demo.scrollTop<=0)  

//当滚动至demo1与demo2交界时

         demo.scrollTop-=demo1.offsetHeight     //demo跳到最顶端

     else{

         demo.scrollTop++

     }

}

var MyMar = setInterval(Marquee,speed);         //设置定时器

demo.onmouseover = function(){clearInterval(MyMar)}  

//鼠标经过时清除定时器达到滚动停止的目的

demo.onmouseout = function(){MyMar = setInterval(Marquee,speed)}  

//鼠标移开时重设定时器

-->

</script>



<DIV id=demo_1 style="OVERFLOW: hidden; HEIGHT: 136px">
<DIV id=demo1_1>
<div id=gleft>
<a href=""><img src="" border="0" width=150 height="120"/></a>
</div>
<div id=gleft>
<a href=""><img src="" border="0" width=150 height="120"/></a>
</div>
<div id=gleft>
<a href=""><img src="" border="0" width=150 height="120"/></a>
</div>
<div id=gleft>
<a href=""><img src="" border="0" width=150 height="120"/></a>
</div>
</DIV>
<DIV id=demo2_1></DIV></DIV>

<SCRIPT>
var speed=25
demo2.innerHTML=demo1.innerHTML
function Marquee(){
if(demo2.offsetWidth-demo.scrollLeft<=0)
demo.scrollLeft-=demo1.offsetWidth
else{
demo.scrollLeft++
}
}
var MyMar=setInterval(Marquee,speed)
demo.onmouseover=function() {clearInterval(MyMar)}
demo.onmouseout=function() {MyMar=setInterval(Marquee,speed)} </SCRIPT>


其中<div id=gleft>这里的gleft的样式你自己定义下就OK了
posted on 2008-07-19 15:04  johngeng  阅读(2699)  评论(0编辑  收藏  举报