当前位置:网站首页>使用js完成文字根据输入框内数字在屏幕上移动
使用js完成文字根据输入框内数字在屏幕上移动
2022-04-22 15:52:00 【雨浅听风吟】
基础案例为javascript dom编程艺术第10章动画效果,在此基础上增加了输入框和根据输入框移动的功能

一开始两个字符会根据计时器移动到一个位置,后面输入高和宽whoa会移动到指定位置
文件结构如下

addLoadEvent.js
function addLoadEvent(func){
var onload=window.onload
if(typeof onload!='function'){
window.onload=func;
}else{
window.onload=function(){
onload();
func();
}
}
}
moveElement.js
function moveElement(elementID,final_x,final_y,interval){
if (!document.getElementById) return false;
if (!document.getElementById(elementID)) return false;
var elem = document.getElementById(elementID);
var xpos = parseInt(elem.style.left);
var ypos = parseInt(elem.style.top);
if (xpos == final_x && ypos == final_y) {
return true;
}
if (xpos < final_x) {
xpos++;
}
if (xpos > final_x) {
xpos--;
}
if (ypos < final_y) {
ypos++;
}
if (ypos > final_y) {
ypos--;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")";
movement = setTimeout(repeat,interval);
}
positionMessage.js
function positionMessage(){
if(!document.getElementById)return false
if (!document.getElementById("message")) return false;
var elem = document.getElementById("message");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "100px";
moveElement("message",125,25,20);
if (!document.getElementById("message2")) return false;
var elem = document.getElementById("message2");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "50px";
moveElement("message2",125,75,20);
}
reMoveElement=function(){
var Height=document.getElementById("Height");
var Width=document.getElementById("Width");
moveElement("message",Height.value,Width.value,20);
return false ;
}
addLoadEvent(positionMessage);
message.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="scripts/addLoadEvent.js"></script>
<script type="text/javascript" src="scripts/positionMessage.js"></script>
<script type="text/javascript" src="scripts/moveElement.js"></script>
<title>Document</title>
</head>
<body>
<p id="message">whee!</p>
<p id="message2">whoa!</p>
<form >
Height : <input id="Height" type="number" name="Height" />
Width: <input id="Width" type="number" name="Width" />
<input type="submit" value="提交" onclick="reMoveElement();return false ;" />
<!-- οnclick= " moveElement('message',Height,Width.,20)" -->
</form>
</body>
</html>
版权声明
本文为[雨浅听风吟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_39289876/article/details/124301874
边栏推荐
- leetcode-----奇偶树
- Short link generator, ADF ly、shorte. st、ouo. io、adfoc. Which is better and what are the differences
- Redis thread model
- The translation uses Prometheus and grafana to realize SLO
- Interviewer: please talk about = = operator and equals (), from the perspective of basic data type and reference data type
- redis优化系列(一)基于docker搭建Redis主从
- jsp回显
- [in depth understanding of tcallusdb technology] sample code of scanning data - [generic table]
- 建筑业未来的发展方向:数字化工厂管理系统
- OSPF的详细讲解、分类,还有实验讲解
猜你喜欢

想做自媒体运营却不会写作?4个珍藏的运营技巧
![[in-depth understanding of tcallusdb technology] sample code for reading all data in the list - [list table]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in-depth understanding of tcallusdb technology] sample code for reading all data in the list - [list table]

Alibaba P9 handwritten 39 module redis core notes. I had a successful interview and got a salary increase of 7K

Interviewer: please talk about = = operator and equals (), from the perspective of basic data type and reference data type

Magic use of solve

图形学101 矩阵变换(2~4节)

社区运营书籍推荐

sql語句———多錶聯查

CASIA webface of dataset: a detailed introduction to the introduction, installation and use of CASIA webface dataset

This API hub is powerful. It contains open APIs such as nailing enterprise wechat, and can be debugged directly!
随机推荐
近期BSN开发常见问题答疑
LeetCode每日一题——824. 山羊拉丁文
redis优化系列(一)基于docker搭建Redis主从
电脑任务栏卡住
Fe of mL: combined with a case of kaggle competition, study the thinking framework of feature engineering
「译文」如何在YAML中输入多行字符串?
企业级知识管理(KM)建设方法及过程
Spark basic learning notes 23: dataframe and dataset
Dongfeng Nissan recalls some Xiaoke with potential safety hazards
[二叉数]相同的树
Redis optimization series (I) building redis master-slave based on docker
阿里P9手写39模块Redis核心笔记,吃透笔记我面试成功涨薪7K
Go 语言二维或多维数组 / 切片定义上的小坑
Displays whether it is urgent
服装私域+供应链的管理
【鲲鹏训练营】重庆2022开发者大赛
jsp回显
ML之FE:结合Kaggle比赛的某一案例细究特征工程(Feature Engineering)思路框架
Clothing private domain + supply chain management
For the professional development of teacher Guo, write down your experience