The native javascript enables seamless scrolling of images

  • 2020-12-20 03:28:18
  • OfStack

Picture horizontal seamless scrolling effect in a large number of websites have applications, especially some enterprise websites in the display of products, because it is a dynamic effect, so can add a lot of color to the site, compared to the static picture display can attract the attention of users, the following through the example code to introduce 1 how to achieve this effect.
The code is as follows:


<html>
<head>
<meta charset="gb2312">
<title> The home of the script </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="#"> The home of the script 1</a> 
  <a href="#"> The home of the script 2</a> 
  <a href="#"> The home of the script 3</a> 
  <a href="#"> The home of the script 4</a> 
  <a href="#"> The home of the script 5</a> 
  <a href="#"> The home of the script 6</a> 
 </div>
 <div id="demo2"></div>
 </div>

I hope it helps you learn javascript programming.


Related articles: