本文实例讲述了jQuery实现的页面弹幕效果。分享给大家供大家参考,具体如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>juqery弹幕</title> <style> *{ margin: 0px; padding: 0px; } html,body{ width: 100%; height: 100%; font-family: "微软雅黑"; background-color: #ccc; margin: 0; padding: 0; } .boxDom{ width: 100%; height: 800px; position: relative; overflow: hidden; } .boxDom img{ width: 100%; height: 100%; } .idDom{ width: 100%; height: 50px; background-color: #666; position: fixed; bottom: 0px; } .content{ width: 600px; height: 50px; position: absolute; left: 500px; top:10px; } .title{ font-size: 25px; display: inline; vertical-align: bottom; color: #fff; } .text{ width: 300px; height: 30px; border:none; border-radius: 5px; text-indent: 2em; margin-left: 60px; } .btn{ width: 100px; height: 30px; margin-left: 20px; font-size: 20px; font-weight: 700; color: #fff; background-color: red; border:none; border-radius: 5px; cursor: pointer; } .string { width: 300px; height: 40px; margin-top: 20px; position: absolute; color: #000; font-size: 20px; font-family: "微软雅黑"; } </style> </head> <body> <div class="boxDom" id="boxDom"> <!-- <img src="7.jpg" alt=""> --> <div class="idDom"> <div class="content"> <p class="title">发送弹幕:</p> <input type="text" class="text"> <button class="btn" id="btn" type="button">发送</button> </div> </div> </div> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(function () { var boxDom = $("#boxDom"); var top, right; var pageWidth = parseInt($(document).width()); var pageHeight = parseInt($(document).height()); $("#btn").bind("click", auto); //绑定回车键按钮 document.onkeydown = function(event){ if(event.keyCode == 13){ auto(); } } function auto() { //获取输入的字符串 var str = $(".text").val(); //生成一个元素 var createSpan = $("<span class ='string'></span>"); //给元素赋值 createSpan.text(str); //为了页面友好,清空刚输入的内容 $(".text").val(""); //生成元素一个随机的位置 为了使每一条弹幕都出现不同的位置 top = Math.floor(Math.random()*pageHeight); createSpan.css({ "top": top, "right": -400, "color": getRandomColor() }); boxDom.append(createSpan); //元素在dom运动起来 //首先有一个span,只让最后一个动起来 var spandom = $("#boxDom>span:last-child");//找到最后一个span spandom.animate({"right":pageWidth+300},10000,function(){ $(this).remove(); }); } //定义一个可以生成随机颜色的方法 function getRandomColor(){ var colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']; var color = ""; for(var i=0;i<6;i++){ color += colorArr[Math.floor(Math.random()*16)]; } return "#"+color; } }) $(function () { var boxDom = $("#boxDom"); var top, right; var pageWidth = parseInt($(document).width()); var pageHeight = parseInt($(document).height()); $("#btn").bind("click", auto); //绑定回车键按钮 document.onkeydown = function(event){ if(event.keyCode == 13){ auto(); } } function auto() { //获取输入的字符串 var str = $(".text").val(); //生成一个元素 var createSpan = $("<span class ='string'></span>"); //给元素赋值 createSpan.text(str); //为了页面友好,清空刚输入的内容 $(".text").val(""); //生成元素一个随机的位置 为了使每一条弹幕都出现不同的位置 top = Math.floor(Math.random()*pageHeight); createSpan.css({ "top": top, "right": -400, "color": getRandomColor() }); boxDom.append(createSpan); //元素在dom运动起来 //首先有一个span,只让最后一个动起来 var spandom = $("#boxDom>span:last-child");//找到最后一个span spandom.animate({"right":pageWidth+300},10000,function(){ $(this).remove(); }); } //定义一个可以生成随机颜色的方法 function getRandomColor(){ var colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']; var color = ""; for(var i=0;i<6;i++){ color += colorArr[Math.floor(Math.random()*16)]; } return "#"+color; } }) </script> </body> </html>
运行效果如下图所示:
感兴趣的朋友还可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.uoften.com/code/HtmlJsRun,测试一下运行效果。
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery切换特效与技巧总结》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。
最后
以上就是聪慧歌曲最近收集整理的关于jQuery实现的页面弹幕效果【测试可用】的全部内容,更多相关jQuery实现内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复