Skip to content

Commit af305f4

Browse files
committed
add marquee
1 parent 4dcee4f commit af305f4

File tree

5 files changed

+140
-5
lines changed

5 files changed

+140
-5
lines changed

.DS_Store

2 KB
Binary file not shown.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,7 @@ study
8484
* 进度条
8585

8686
## stopwatch
87-
* 秒表
87+
* 秒表
88+
89+
## marquee
90+
* 文字跑马灯效果

loading/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
top: 0;
2626
left: 0;
2727

28-
-webkit-animation: bounce 2.0s infinite ease-in-out;
29-
animation: bounce 2.0s infinite ease-in-out;
28+
-webkit-animation: bounce 5.0s infinite ease-in-out;
29+
animation: bounce 5.0s infinite ease-in-out;
3030
}
3131

3232
.double-bounce2 {
33-
-webkit-animation-delay: -1.0s;
34-
animation-delay: -1.0s;
33+
-webkit-animation-delay: -3.0s;
34+
animation-delay: -3.0s;
3535
}
3636

3737
@-webkit-keyframes bounce {

marquee/marquee.html

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
6+
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
7+
<title>js实现文字超过显示宽度每间隔1s自动向左滚动显示</title>
8+
<style type="text/css">
9+
10+
11+
.broad-coast {
12+
display: flex;
13+
justify-content: center;
14+
}
15+
.icon-cost {
16+
width: 40px;
17+
height: 40px;
18+
background: url(./v_3.png) no-repeat center center;
19+
background-size: 20px 20px;
20+
}
21+
.broad{
22+
position: relative;
23+
width: 300px;
24+
font-size: 0.85rem;
25+
color: #333;
26+
white-space: nowrap;
27+
overflow: hidden;
28+
}
29+
.inner{ width:1000px;height:40px;line-height:40px;overflow:hidden;}
30+
.inner .txt{ display:inline-block;}
31+
32+
</style>
33+
</head>
34+
<body>
35+
<div id="marquee" class="broad-coast">
36+
<div class="icon-cost"></div>
37+
<div id="broad" class="broad">
38+
<div class="inner">
39+
<span class="txt">文字如果超出了宽度自动向左滚动文字如果超出了宽度自动向左滚动。</span>
40+
</div>
41+
</div>
42+
</div>
43+
44+
<script>
45+
/**
46+
* 文字跑马灯
47+
* @params options <Object>
48+
* id: 跑马灯所在的dom树最外层节点
49+
* broadName 包围圈样式名
50+
* speed 速度值 默认50
51+
* @author 蛙哥 <[email protected]>
52+
*/
53+
function Marquee(options){
54+
if(!options.id){
55+
alert('缺少id')
56+
return false;
57+
}
58+
var wrapper = this.wrapper = document.getElementById(options.id);
59+
this.extend(options);
60+
this.broad = wrapper.querySelector(options.broadName);
61+
this.inner = this.broad.querySelector(options.innerName);
62+
this.text = this.inner.querySelector(options.textName);
63+
this.textWidth = this.text.offsetWidth;
64+
this.broadWidth = this.broad.width;
65+
this.speed = options.speed;
66+
this.init();
67+
}
68+
/**
69+
* 初始化
70+
*/
71+
Marquee.prototype.init = function(){
72+
var that = this;
73+
if(this.broadWidth > this.textWidth) {
74+
return false;
75+
}
76+
this.inner.innerHTML += this.inner.innerHTML;
77+
setTimeout(function(){
78+
that.run()
79+
},2000)
80+
}
81+
82+
/**
83+
* 执行跑马灯
84+
*/
85+
Marquee.prototype.run = function(){
86+
var that = this;
87+
if(this.textWidth > this.broad.scrollLeft){
88+
this.broad.scrollLeft++;
89+
setTimeout(function(){
90+
that.run();
91+
},this.speed);
92+
}else{
93+
setTimeout(function(){
94+
that.reset();
95+
},this.speed);
96+
}
97+
}
98+
99+
/**
100+
* 复位
101+
*/
102+
Marquee.prototype.reset = function(){
103+
this.broad.scrollLeft=0;
104+
this.run();
105+
}
106+
107+
/**
108+
* 设置默认参数
109+
*/
110+
Marquee.prototype.extend = function(options) {
111+
var defaultConfig = {
112+
broadName:'.broad',
113+
innerName:'.inner',
114+
textName:'.txt',
115+
speed:50
116+
}
117+
for(var key in defaultConfig){
118+
if(!options.hasOwnProperty(key)){
119+
options[key]=defaultConfig[key]
120+
}
121+
}
122+
return options;
123+
}
124+
125+
var m = new Marquee({
126+
id:'marquee'
127+
})
128+
129+
130+
</script>
131+
</body>
132+
</html>

marquee/v_3.png

5.12 KB
Loading

0 commit comments

Comments
 (0)