前端JS制作弹幕问题,style.left和style.top的值无法赋上

韧心 韧心 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script>
<style type="text/css">
.container {
background-color: #FFFFFF;
width: 600px;
height: 500px;
}

.tanmu {
width: 600px;
height: 500px;
background-color: #060606;
}

#textstyle {
font-size: 11px;
color: red;
}
</style>
<script type="text/javascript">
function tanmu() {
var text = document.getElementById("text");
var tanmu = document.getElementById("tanmu");

var textinnelHTML = "<font id=\"textstyle\">" + text.value + "</font>";
alert(textinnelHTML);
var x = tanmu.offsetWidth;

var y = Math.round(Math.random() * tanmu.offsetHeight);

tanmu.innerHTML = textinnelHTML;

var textobj = document.getElementById("textstyle");
alert(textobj.style.left);
textobj.style.left = x + "px";
textobj.style.top = y + "px";
}
</script>
</head>

<body>
<div class="container">
<div class="tanmu" id="tanmu"></div>
</div>
<input type="text" value="这是弹幕" id="text" />
<input type="button" value="发送" onclick="tanmu();" />
</body>

</html>


以上是我的所有代码,想要制作类似于弹幕的功能。但是在这罩层div中,我设定了style.left和style.top的属性值,可是好像无法赋值上去,x,和y都是有值的。
...全文

相关推荐

原生js获取left值和top值的三种方法

在用js做动态效果时,通常需要获取元素绝对定位中的left和top属性值。比如做一个碰撞广告,就要不停的获取元素的top和left属性值。 需要注意的事:取值的元素必须要设置position:absolute绝对定位属性,才能取的到left值。 第一种方法,比较简单,就是直接通过obj.style.left和obj.style.top,但是有局限性,这种获取的方法只能获取到行内样式的left和top的属性值,不能获取到style标签和link 外部引用的left和top属性值。 第二种方法 只读,可以获取所有style样式,存在兼容性问题,在标准浏览器中可以通过window.getComput

weixin_38624556

js精确定位HTML标签的TOP和LEFT值

js精确定位 一个HTML 标签的 TOP 和 LEFT值

kaka.mr

简单实现JavaScript弹幕效果

主要帮助大家简单实现JavaScript弹幕效果

weixin_38650516

js取得DIV的top,left,width,height值.doc

js取得DIV的top,left,width,height值 ClientInfo = ClientInfo + "userAgent : "+window.navigator.userAgent+ " "; ClientInfo = ClientInfo + "appName : "+window.navigator.appName+ " "; ClientInfo = ClientInfo + "appVersion : "+window.navigator.appVersion+ " "; ClientInfo = ClientInfo + "appMinorVersion : "+window.navigator.appMinorVersion+ " "; ClientInfo = ClientInfo + "appCodeName : "+window.navigator.appCodeName+ " "; ClientInfo = ClientInfo + "platform : "+window.navigator.platform+ " "; ClientInfo = ClientInfo + "language : "+window.navigator.language+ " "; ClientInfo = ClientInfo + "userLanguage : "+window.navigator.userLanguage+ " "; ClientInfo = ClientInfo + "cpuClass : "+window.navigator.cpuClass+ " "; ClientInfo = ClientInfo + "onLine : "+window.navigator.onLine+ " "; //ClientInfo = ClientInfo + "javaEnabled : "+window.navigator.javaEnabled+ " ";

zhangaijing

JB3-7-Vue(一)

NodeJs,NVM,NPM,VueCli,Vite,VueRouter,SCSS,Vue基础语法,Vue生命周期,Axios,Pinia,ElementPlus,ECharts,XGPlayer,QuillEditor,MavonEditor,Marked,MonacoEditor 等

周航宇JoeZhou
评论 7
收藏 已收藏
分享
返回顶部
韧心
JavaScript 87,984 成员 224,681 内容