jQuery实现的页面弹幕效果【测试可用】

 更新时间:2018年08月17日 09:17:40   作者:不会唱歌的演员不是好程序猿  
这篇文章主要介绍了jQuery实现的页面弹幕效果,涉及jQuery事件响应与页面元素属性动态操作相关实现技巧,需要的朋友可以参考下

本文实例讲述了jQuery实现的页面弹幕效果。分享给大家供大家参考,具体如下:

<!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.jb51.net/code/HtmlJsRun,测试一下运行效果。

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery切换特效与技巧总结》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结

希望本文所述对大家jQuery程序设计有所帮助。

相关文章

  • EasyUI中实现form表单提交的示例分享

    EasyUI中实现form表单提交的示例分享

    这里给大家分享的是一段使用EasyUI中实现form表单提交的方法的核心代码,小伙伴们根据自己的需求补全form部分吧,希望大家能够喜欢。
    2015-03-03
  • JQuery省市联动效果实现过程详解

    JQuery省市联动效果实现过程详解

    这篇文章主要介绍了JQuery省市联动效果实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • JQuery实现网页右侧随动广告特效

    JQuery实现网页右侧随动广告特效

    本文给大家分享的是2则使用jquery实现网页右侧随动广告特效的代码,非常的简单实用,有需要的小伙伴可以参考下。
    2016-01-01
  • jQuery编写设置和获取颜色的插件

    jQuery编写设置和获取颜色的插件

    本文主要分享了编写设置和获取颜色的插件,该插件用来实现两个功能:1.设置元素的颜色;2.获取元素的颜色。 具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • jquery animate实现鼠标放上去显示离开隐藏效果

    jquery animate实现鼠标放上去显示离开隐藏效果

    本文为大家介绍下使用jquery animate实现鼠标放上去显示,离开就隐藏的效果,感兴趣的朋友可以参考下哈,希望对大家有所帮助
    2013-07-07
  • 初窥JQuery(二)事件机制(2)

    初窥JQuery(二)事件机制(2)

    上篇文章我简单的描述了加载页面、事件委派、事件切换三种JQuery的事件机制,这篇文章我讲下在JQuery事件机制中占主导地位并且在我们的实际工作中最常用到的机制-事件处理
    2010-12-12
  • jQuery中的on与bind绑定事件区别实例详解

    jQuery中的on与bind绑定事件区别实例详解

    bind与on的区别就在于–事件冒泡,关于jquery中的on与bind绑定事件的区别通过本文给大家实例讲解,需要的朋友参考下吧
    2017-02-02
  • jQuery基础_入门必看知识点

    jQuery基础_入门必看知识点

    下面小编就为大家带来一篇jQuery基础_入门必看知识点。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • 基于jQuery实现的双11天猫拆红包抽奖效果

    基于jQuery实现的双11天猫拆红包抽奖效果

    这篇文章主要介绍了基于jQuery实现的双11天猫拆红包抽奖效果,在即将到来的双十二活动中大家也可以使用拆红包抽奖吸引消费者,需要的朋友可以参考下
    2015-12-12
  • Jquery拖拽并简单保存的实现代码

    Jquery拖拽并简单保存的实现代码

    今闲着无聊 顺便看了下jquery ui的拖拽插件,实现拖拽的方法很简单,看到效果后兴奋小下...
    2010-11-11

最新评论