Home > Web Front-end > JS Tutorial > Native JS implements marquee effect

Native JS implements marquee effect

高洛峰
Release: 2017-02-21 14:51:42
Original
2096 people have browsed it

This article mainly shares the sample code for realizing the marquee effect using native JS. It has a very good reference value, let’s take a look with the editor below

The effect is as follows: (You can copy the code to view the dynamic effect, and you can choose to add the pictures in the case)

Native JS implements marquee effect

The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title></title>
 <style>
  *{
   margin:0;
   padding:0;
   list-style: none;
  }
  p{
   position: relative;
   width: 800px;
   height: 200px;
   border: 5px solid lightgreen;
   margin:10px auto;
   overflow: hidden;
  }
  p ul{
   position: absolute;
   left:0;
   top:0;
  }
  p ul li{
   width: 200px;
   height: 200px;
   float: left;
  }
  p ul li img{
   width:100%;
   height: 100%;
  }
 </style>
</head>
<body>
<p id="p1">
 <ul>
  <li><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=b4251f00ef925cba01ed49ca117e14a7" alt=""/></li>
  <li><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=d38403825519770211acbf49459ae7d7" alt=""/></li>
  <li><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=53c83d39c62af65db998f4945bacaec2" alt=""/></li>
  <li><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=5509f1a642644b40c8aa98c9e2c77a83" alt=""/></li>
 </ul>
</p>
<script>
 var oUl=document.getElementsByTagName(&#39;ul&#39;)[0];
 var lis=oUl.getElementsByTagName(&#39;li&#39;);
 oUl.innerHTML+=oUl.innerHTML;
 oUl.style.width=lis.length*lis[0].offsetWidth+&#39;px&#39;;
 var left=null;
 var timer=setInterval(function(){
  left-=3;
  if(left<-oUl.offsetWidth/2){
   left=0;
  }
  oUl.style.left=left+&#39;px&#39;
 },10)
</script>
</body>
</html>
Copy after login

For more articles related to native JS implementing marquee effect, please pay attention to PHP Chinese website !

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template