Javascript运动基础
javascript的运动非常实用,通过控制需要运动块的实际距离与要到达的距离的关系,结合定时器来控制小方块的各种运动。
运动框架
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 100px;
position: absolute;
top: 50px;
left: 50px;
background-color: red;
}
</style>
</head>
<body>
<div id="div1"></div>
<input id="btn" type="button" value="start" onclick="move()"></input>
<script type="text/javascript">
var timer=null;
function move(){
var oDiv1=document.getElementById('div1');
clearInterval(timer); //进函数之前先清空一下其他定时器,保证每一次进入仅启用一个定时器。
timer=setInterval(function(){
var speed=10; //通过控制速度值的大小来决定运动的快慢
if(oDiv1.offsetLeft>=300){ //停止条件
clearInterval(timer); //符合条件则停止,清空定时器
}
else{
oDiv1.style.left=oDiv1.offsetLeft+speed+'px'; //不符合条件则继续运动
}
},30) //每隔30毫秒运动一次
}
</script>
</body>
</html>
运动框架,控制速度快慢的条件有两个:1.定时器的时间,2.速度 。 一般不建议第一种,时间一般都是通过精密计算思考,定了就不改,大多可采用修改speed变量来控制速度快慢。
eg1:分享到侧边栏效果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#div1{
background-color: red;
width: 150px;
height: 200px;
position: absolute;
left: -150px;
top: 50px;
}
#div1 span{
background-color: green;
width: 20px;
height: 80px;
position: absolute;
right: -20px;
top: 80px;
}
</style>
</head>
<body>
<div id="div1"><span>分享到</span>
</div>
<script type="text/javascript">
window.onload=function(){
var oDiv1=document.getElementById('div1');
oDiv1.onmouseover=function(){
move(0);
};
oDiv1.onmouseout=function(){
move(-150);
}
};
var timer=null;
function move(destion){
var oDiv1=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
var speed=0;
if(oDiv1.offsetLeft>destion) //通过实际距离与目标地址的差距来决定速度的正负,可省略函数的一个速度参数,若实际距离大于目标地址,则速度为负值
{
speed=-10;
}
else //否则,实际距离小于目标距离,速度为正值
{
speed=10;
}
if(oDiv1.offsetLeft==destion)
{
clearInterval(timer);
}
else{
oDiv1.style.left=oDiv1.offsetLeft+speed+'px';
}
},30)
}
</script>
</body>
</html>
eg2:图片的淡入淡出效果
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1{
height: 200px;
width: 200px;
background-color: red;
filter: alpha(opacity:30) ; /*兼容ie */
opacity: 0.3; /*火狐,谷歌*/
}
</style>
</head>
<body>
<div id="div1"></div>
<script type="text/javascript">
window.onload=function(){
var oDiv1=document.getElementById('div1');
oDiv1.onmouseover=function(){
move(100);
}
oDiv1.onmouseout=function(){
move(30);
}
}
var timer=null;
var alpha=30; //用参数存储透明度
function move(target){ //参数为目标值,需要成为的透明度数
var oDiv1=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
var speed;
if (alpha<target) //判断目前的透明度与目标透明度的差距决定速度正负
{
speed=10;
}
else{
speed=-10;
}
if(alpha==target)
{
clearInterval(timer);
}
else{
alpha+=speed;
oDiv1.style.filter='alpha(opacity:'+alpha+')';
oDiv1.style.opacity=alpha/100;
}
},30);
}
</script>
</body>
</html>
eg3:缓冲运动:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
top: 50px;
left: 0px;
}
#div2{
width: 1px;
height: 300px;
background-color: black;
position: absolute;
left: 300px;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<input type="button" value="start" onclick="move()"></input>
<script type="text/javascript">
function move(){
var oDiv1=document.getElementById('div1');
setInterval(function(){
var speed=(300- oDiv1.offsetLeft)/10; //在不同时刻距目标地的距离会越来越短,除一个固定的值,速度也会越来越小
speed=speed>0?Math.ceil(speed):Math.floor(speed); //向左向右 对速度向上取整或向下取整,px是最小像素值,计算机最小距离单位,会自动向下取整,不会四舍五入,速度成0.9的时候,计算机无法识别,因此不会走到你预期的位置,就会停。针对不同的方向,对他向上或向下取整。
oDiv1.style.left=oDiv1.offsetLeft+speed+'px';
},30)
}
</script>
</body>
</html>
eg4:缓冲运动使方块固定到右下角
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 150px;
position: absolute;
background-color: red;
right: 0;
bottom: 0;
}
</style>
</head>
<body style="height: 2000px">
<div id="div1"></div>
<script type="text/javascript">
window.onscroll=function(){
var oDiv1=document.getElementById('div1');
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; //做浏览器的兼容
//oDiv1.style.top=(document.documentElement.clientHeight - oDiv1.offsetHeight)/2+scrollTop+'px';
move(document.documentElement.clientHeight - oDiv1.offsetHeight+scrollTop);
};
var timer=null;
function move(target){
var oDiv1=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
var speed=(target - oDiv1.offsetTop)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(oDiv1.offsetTop==target)
{
clearInterval(timer);
}
else{
oDiv1.style.top=oDiv1.offsetTop+speed+'px';
}
},30)
}
</script>
</body>
</html>
eg5:缓冲运动固定到右侧中间
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 150px;
position: absolute;
background-color: red;
right: 0;
bottom: 0;
}
</style>
</head>
<body style="height: 2000px">
<div id="div1"></div>
<script type="text/javascript">
window.onscroll=function(){
var oDiv1=document.getElementById('div1');
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
//oDiv1.style.top=(document.documentElement.clientHeight - oDiv1.offsetHeight)/2+scrollTop+'px';
move(parseInt((document.documentElement.clientHeight - oDiv1.offsetHeight)/2+scrollTop));
}
var timer=null;
function move(target){
clearInterval(timer);
var oDiv1=document.getElementById('div1');
timer=setInterval(function(){
var speed=(target - oDiv1.offsetTop)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(oDiv1.offsetTop==target)
{
clearInterval(timer);
}
else{
oDiv1.style.top=oDiv1.offsetTop+speed+'px';
}
},30)
}
</script>
</body>
</html>
eg8:匀速运动 固定到某一具体位置
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
top: 50px;
left: 0px;
}
#div2{
width: 1px;
height: 300px;
background-color: black;
position: absolute;
left: 100px;
}
#div3{
width: 1px;
height: 300px;
background-color: black;
position: absolute;
left: 300px;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<input type="button" value="start100" onclick="move(100)"></input>
<input type="button" value="start300" onclick="move(300)"></input>
<script type="text/javascript">
var timer=null;
function move(target){
var oDiv1=document.getElementById('div1');
clearInterval(timer);
setInterval(function(){
var speed=0;
clearInterval(timer);
if(oDiv1.offsetLeft<target){
speed=7;
}
else{
speed=-7;
}
if(Math.abs(target- oDiv1.offsetLeft)<=7) //当遇到接近但到不了的情况,可将它的近似看做已到达,防止抖动
{
clearInterval(timer);
oDiv1.style.left=target+'px'; //手动将他的距离改为目标距离
}
else{
oDiv1.style.left=oDiv1.offsetLeft+speed+'px';
}
},30)
}
</script>
</body>
</html>
以上总结了js中运动的基础,可将代码复制查看效果
Javascript运动基础的更多相关文章
- Javascript 运动基础 01
JS运动基础 运动基础 让Div运动起来 速度——物体运动的快慢 运动中的Bug 不会停止 速度取某些值会无法停止 到达位置后再点击还会运动 重复点击速度加快 匀速运动 速度不变 <s ...
- day38—JavaScript的运动基础-匀速运动
转行学开发,代码100天——2018-04-23 一.运动基础框架 JavaScript的运动可以广义理解为渐变效果,直接移动效果等,图网页上常见的“分享到”,banner,透明度变化等.其实现的基本 ...
- 原生JavaScript运动功能系列(五):定时定点运动
原生JavaScript运动功能系列(一):运动功能剖析与匀速运动实现 原生JavaScript运动功能系列(二):缓冲运动 原生JavaScript运动功能系列(三):多物体多值运动 原生JavaS ...
- JS运动---运动基础(匀速运动)
[一]运动基础 (2)基础运动案例 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- javascript运动系列第一篇——匀速运动
× 目录 [1]简单运动 [2]定时器管理 [3]分享到效果[4]移入移出[5]运动函数[6]透明度[7]多值[8]多物体[9]回调[10]函数完善[11]最终函数 前面的话 除了拖拽以外,运动也是j ...
- JavaScript RegExp 基础详谈
前言: 正则对于一个码农来说是最基础的了,而且在博客园中,发表关于讲解正则表达式的技术文章,更是数不胜数,各有各的优点,但是就是这种很基础的东西,如果我们不去真正仔细研究.学习.掌握,而是抱着需要的时 ...
- Popmotion – 小巧,灵活的 JavaScript 运动引擎
Popmotion 是一个只有12KB的 JavaScript 运动引擎,可以用来实现动画,物理效果和输入跟踪.原生的DOM支持:CSS,SVG,SVG路径和DOM属性的支持,开箱即用.Popmoti ...
- JavaScript学习基础部分
JavaScript学习基础 一.简介 1.JavaScript 是因特网上最流行的脚本语言,并且可在所有主要的浏览器中运行,比方说 Internet Explorer. Mozilla.Firefo ...
- JavaScript入门基础
JavaScript基本语法 1.运算符 运算符就是完成操作的一系列符号,它有七类: 赋值运算符(=,+=,-=,*=,/=,%=,<<=,>>=,|=,&=).算术运 ...
随机推荐
- 一步一步将Vim打造成C++超级IDE
文/嶽永鹏 最近从MS Windows 转到了Liunx,花了一段时间熟悉和学习Liunx环境.有时候,真的很是怀念MS Vistual Studio那种超级智能的开发环境,总是想在Vim拾起那些曾进 ...
- 用 highlight.js 为文章中的代码添加语法高亮
来源:http://www.ghostchina.com/adding-syntax-highlighting-to-ghost-using-highlight-js/ --------------- ...
- linux环境下android-ndk下的ffmpeg编译
目前正在做手机底层播放器对的开发,需要用的ffmpeg,因为是新手,所以先从ffmpeg的编译开始做起.虽然是在前人的基础上,但是在linux上编译确实头一遭,因此在编译中,总有些坑是必须要填的,下面 ...
- cocoapod集成失败,无法找到头文件的解决办法
在终端更新pod的时候,提示警告: target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support ...
- Mongodb 故障分享 初始化时"errmsg" : "exception: new file allocation failure" 并且长时间处于STARTUP2
Hello,大家下午好. 近几天的项目有点赶,所以耽误了更新.现在给大家分享下,在安装mongodb的过程中,遇到的故障一则.其实很小白的问题,当时遇到这个问题的时候比较心慌,浪费了很多时间,跟大家分 ...
- 利用ajax向jsp传输数据
ajax代码 var obtn=document.getElementsByTagName('input')[0]; obtn.onclick=function () { var xhr=null; ...
- Linux x64 下 Matlab R2013a 300 kb 脚本文件调试的 CPU 占用过高问题的解决办法
(1) 系统+软件版本 CentOS 6.5 (Final), 64 位,内核initramfs-2.6.32-431.5.1.el6.x86_64, MATLAB Version: 8.1.0.60 ...
- 文顶顶iOS开发博客链接整理及部分项目源代码下载
文顶顶iOS开发博客链接整理及部分项目源代码下载 网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...
- 电信级的RSA加密后的密码的破解方法
一直以来,电信通过HTTP劫持推送广告的方式已经存在了很多年了,这种手段至今并未停止.这种手段月光博客曾经有多次曝光,见<电信级的网络弹出广告>.<获取了电信恶意弹出广告的罪证> ...
- μC/OS-Ⅲ中的临界段代码
临界段代码(critical sections),也叫临界区(critical region),是指那些必须完整连续运行,不可被打断的代码段.μC/OS-Ⅲ系统中存在大量临界段代码.采用两种方式对临界 ...