原生javascript實現(xiàn)圖片無縫滾動效果
更新時間:2016年02月12日 19:14:21 作者:愚蠢的神
這篇文章主要介紹了原生javascript實現(xiàn)圖片無縫滾動效果的相關(guān)資料,需要的朋友可以參考下
圖片水平無縫滾動效果在大量的網(wǎng)站都有應(yīng)用,特別是一些企業(yè)網(wǎng)站在展示產(chǎn)品的時候,因為是動態(tài)效果,所以能夠給網(wǎng)站增色不少,相比靜態(tài)圖片展示更能夠吸引用戶的注意力,下面就通過實例代碼介紹一下如何實現(xiàn)此效果。
代碼如下:
<html>
<head>
<meta charset="gb2312">
<title>腳本之家</title>
<style type="text/css">
#demo{
background:#FFF;
overflow:hidden;
border:1px dashed #CCC;
width:500px;
}
#indemo{
float:left;
width:2000px;
}
#indemo a{
width:100px;
height:100px;
float:left;
background-color:blue;
margin-left:5px;
text-align:center;
line-height:100px;
color:red;
text-decoration:none;
}
#demo1{float:left;}
#demo2{float:left;}
</style>
<script type="text/javascript">
window.onload=function(){
var speed=10;
var tab=document.getElementById("demo");
var tab1=document.getElementById("demo1");
var tab2=document.getElementById("demo2");
tab2.innerHTML=tab1.innerHTML;
function Marquee(){
if(tab2.offsetWidth-tab.scrollLeft<=0){
tab.scrollLeft-=tab1.offsetWidth
}
else{
tab.scrollLeft++;
}
}
var MyMar=setInterval(Marquee,speed);
tab.onmouseover=function() {clearInterval(MyMar)};
tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)};
}
</script>
</head>
<body>
<div id="demo">
<div id="indemo">
<div id="demo1">
<a href="#">腳本之家一</a>
<a href="#">腳本之家二</a>
<a href="#">腳本之家三</a>
<a href="#">腳本之家四</a>
<a href="#">腳本之家五</a>
<a href="#">腳本之家六</a>
</div>
<div id="demo2"></div>
</div>
希望對大家學(xué)習(xí)javascript程序設(shè)計有所幫助。

