很久以前就有这个问题,总是找不到通用的,或比较简单的“图片连续无间隙向左滚动,无间隙向右滚动,符合W3C Web标准”,这次又遇上一个这样的问题,找了老半天终于从网上找到一个不错的,看了下代码,思路很简单,但代码太多,通用性也不行,于是乎,自己又发明 了两个函数,用起来感觉还真不错,与大家共享:

<!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>
<title>图片连续无间隙向左滚动,无间隙向右滚动,符合W3C Web标准</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<div )
function $(id){return document.getElementById(id)}
//向左滚动函数,demo包含demo1与demo2,speed是滚动速度,flag一个网页内有多个时设置为不同的任意字符。
function toleft(demo,demo1,demo2,speed,flag){
demo=$(demo);demo1=$(demo1);demo2=$(demo2)
demo2.innerHTML=demo1.innerHTML
function Marquee(){
if(demo2.offsetWidth-demo.scrollLeft<=0){
demo.scrollLeft-=demo1.offsetWidth
}
else{
demo.scrollLeft++
}
}
flag=setInterval(Marquee,speed)
demo.onmouseover=function(){clearInterval(flag);}
demo.onmouseout=function(){flag=setInterval(Marquee,speed);}
}
//向右滚动函数,demo包含demo1与demo2,speed是滚动速度,flag一个网页内有多个时设置为不同的任意字符。
function toright(demo,demo1,demo2,speed,flag){
demo=$(demo);demo1=$(demo1);demo2=$(demo2)
demo2.innerHTML=demo1.innerHTML
function Marquee(){
if(demo.scrollLeft<=0){
demo.scrollLeft=demo2.offsetWidth
}
else{
demo.scrollLeft--
}
}
flag=setInterval(Marquee,speed)
demo.onmouseover=function(){clearInterval(flag);}
demo.onmouseout=function(){flag=setInterval(Marquee,speed);}
}
</script>
</body>
</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>
<title>Untitled Document</title>
</head>
<body>
<img src="images/butt_arrowl.gif" style="cursor:pointer" onmouseover="clearright();toleft()"/> <img src="images/butt_arrowr.gif"style="cursor:pointer" onmouseover="clearleft();toright()"/>
<div>
<div ).onmouseout()}
righthit=1    //设置右移按钮,当按了一次右移按钮后(不管按多少次),在没按左移按钮之前,toright方法里的的代码不会再被执行
lefthit=0    //把左移按钮设为0,这样当下次点了左移按钮的时候,以便toleft方法被调用
}
}
</script>

相关文章: