标签:
//根据元素id
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>社群空间</title>
<link href="css/font-awesome.css" rel="stylesheet">
<style>
div{
overflow: hidden;
}
#marquee{
display: inline-block;
white-space: nowrap;
}
</style>
</head>
<body>
<div><span id="marquee">过 translate() 方法,元素从其当前位置移动王二小是儿童团员她常常是哪个:</span></div>
<script>
var speed = 20;
var c=0;
var obj =document.getElementById(‘marquee‘)
var w= obj.offsetWidth;
var p = obj.parentNode.offsetWidth;
function Marquee(){
c++;
if(c>w){
c=0;
}
obj.style.webkitTransform = ‘translateX(-‘+ c +‘px)‘
};
if(w>p){
var mq = setInterval(Marquee, speed);
obj.appendChild(document.createTextNode(obj.innerText))
};
</script>
</body>
</html>
--根据类名
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>社群空间</title>
<link href="css/font-awesome.css" rel="stylesheet">
<style>
div{
overflow: hidden;
}
.marquee{
display: inline-block;
white-space: nowrap;
}
</style>
</head>
<body>
<div><span class="marquee">过 translate() 方法,元素从其当前位置移动王二小是儿童团员她常常是哪个:</span></div>
<div><span class="marquee">adsfadsf 小是儿童团员她常常sdfasdf:</span></div>
<div><span class="marquee">过 translate() 方法,元素从其当</span></div>
<script>
function scroll(obj){
var speed = 20;
var c=0;
var w= obj.offsetWidth;
var p = obj.parentNode.offsetWidth;
function Marquee(){
c++;
if(c>w){
c=0;
}
obj.style.webkitTransform = ‘translateX(-‘+ c +‘px)‘
};
if(w>p){
var mq = setInterval(Marquee, speed);
obj.appendChild(document.createTextNode(obj.innerText))
};
};
for(var i=0;i<document.querySelectorAll(‘.marquee‘).length;i++){
scroll(document.querySelectorAll(‘.marquee‘)[i]);
};
</script>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/ghelement/p/4949510.html