js基础之弹性运动(四)
一、滑动菜单、图片
var iSpeed=0;
var left=0;
function startMove(obj,iTarg){
clearInterval(obj.timer);//记得先关定时器
obj.timer=setInterval(function(){
iSpeed+=(iTarg-obj.offsetLeft)/5;
iSpeed*=0.7;
left+=iSpeed;//用一个变量left解决小数误差的问题
if(Math.abs(iSpeed)<1 && Math.abs(iTarg-obj.offsetLeft)<1){
obj.style.left=iTarg+'px';
}else{
obj.style.left=left+'px';//console.log(obj.offsetLeft+'/'+iTarg+'/'+iSpeed);
}
},30);
}
二、运动过界
function startMove(obj,iTarget){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
iSpeed+=(iTarget-height)/5;
iSpeed*=0.7;
if(Math.abs(iSpeed<1) && Math.abs(iTarget-height)<1){
clearInterval(obj.timer);
}else{
height+=iSpeed;
if(height<0){
height=0;//如果不做处理,打印出的高度会为负值,这种情况就是运动过界,在IE下会报错
}
document.getElementById('txt1').value+=height+'\n';
obj.style.height=height+'px';
}
},30);
}
三、碰撞+拖拽+重力
var iSpeedX=0;
var iSpeedY=0;
var timer=null;
function startMove(){
clearInterval(timer);
timer=setInterval(function(){
var oDiv=document.getElementById('div1');
iSpeedY+=3;//重力
var l=oDiv.offsetLeft+iSpeedX;
var t=oDiv.offsetTop+iSpeedY;
if(l>=document.documentElement.clientWidth-oDiv.offsetWidth){
iSpeedX*=-0.8;//反弹+摩擦力
l=document.documentElement.clientWidth-oDiv.offsetWidth;
}else if(l<=0){
iSpeedX*=-0.8;//alert(iSpeedX+'--'+iSpeedY);
l=0;
}
if(t>=document.documentElement.clientHeight-oDiv.offsetHeight){
iSpeedY*=-0.8;
iSpeedX*=0.8;//alert(iSpeedX+'--'+iSpeedY);
t=document.documentElement.clientHeight-oDiv.offsetHeight;
}else if(t<=0){
iSpeedY*=-1;
iSpeedX*=0.8;
t=0;
}
if(Math.abs(iSpeedX)<1){
iSpeedX=0;
}
if(Math.abs(iSpeedY)<1){
iSpeedY=0;
}
if(iSpeedX==0 && iSpeedY==0 && t==document.documentElement.clientHeight-oDiv.offsetHeight){
clearInterval(timer);alert(0);
}else{
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';//document.title=iSpeedX+'--'+iSpeedY;
}
},30);
}
window.onload=function(){
var oDiv = document.getElementById('div1');
//拖拽
var lastX=0;
var lastY=0;
oDiv.onmousedown=function(ev){
var oEvent=ev||event;
var disX=oEvent.clientX-oDiv.offsetLeft;
var disY=oEvent.clientY-oDiv.offsetTop;
document.onmousemove=function(ev){
var oEvent=ev||event;
var l=oEvent.clientX-disX;
var t=oEvent.clientY-disY;
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';
iSpeedX=l-lastX;
iSpeedY=t-lastY;
lastX=l;
lastY=t;//document.title=iSpeedX+'/'+iSpeedY;
//div左上角轨迹,类似画笔
/*var oBox=document.createElement('div');
oBox.style.left=l+'px';
oBox.style.top=t+'px';
document.body.appendChild(oBox);//console.log(l+'--'+t);*/
};
document.onmouseup=function(){
document.onmousemove=null;
document.onmouseup=null;
startMove();
};
clearInterval(timer);
};
}
js基础之弹性运动(四)的更多相关文章
- JS运动基础(三) 弹性运动
加减速运动速度不断增加或减少速度减小到负值,会向反方向运动 弹性运动在目标点左边,加速:在目标点右边,减速根据距离,计算加速度 带摩擦力的弹性运动弹性运动+摩擦力 弹性:速度 += (目标点 - 当前 ...
- js基础学习笔记(四)
4.1 什么是数组 我们知道变量用来存储数据,一个变量只能存储一个内容,存储多个值时,就需要用到数组. 数组是一个值的集合,每个值都有一个索引号,从0开始,每个索引都有一个相应的值,根据需要添加更多数 ...
- JS基础入门篇(四)—this的使用,模拟单选框,选项卡和复选框
1.this的使用 this js中的关键字 js内部已经定义好了,可以不声明 直接使用 this的指向问题 1. 在函数外部使用 this指向的是window 2. 在函数内部使用 有名函数 直接调 ...
- JS基础入门篇(四十三)—ES6(二)
1.对象简洁表示法 原来写法 var name = "lzf"; var gender = "male"; var fn = function(){consol ...
- js 运动函数篇(二) (加速度运动、弹性运动、重力场运动(多方向+碰撞检测+重力加速度+能量损失运动)拖拽运动)层层深入
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS写加速度运动.弹性运动.重力场运 ...
- [Js]弹性运动
描述:像弹簧一样左右弹动,最后缓慢停下来 一.加减速运动 1.加速运动 var iSpeed=0;iSpeed++; 速度越来越快,最后冲出去 2.减速运动 var iSpeed=20;iSpeed- ...
- javascript每日一练(十四)——弹性运动
一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- JS学习之路,之弹性运动框架
弹性运动:顾名思义,就如同物理中的加速减速运动,当开始时速度过大,到达终点时,速度不会立刻停下,而是再前进一段距离,而后再向相反方向运动,如此往复. var timer=null; var speed ...
- javascript运动系列第五篇——缓冲运动和弹性运动
× 目录 [1]缓冲运动 [2]弹性运动 [3]距离分析[4]步长分析[5]弹性过界[6]弹性菜单[7]弹性拖拽 前面的话 缓冲运动指的是减速运动,减速到0的时候,元素正好停在目标点.而弹性运动同样是 ...
随机推荐
- python 将页面保存为word
将博客或者留言页面保存为word文档 -----------2016-5-11 14:40:04-- source:http://blog.csdn.net/how8586/article/detai ...
- 01 Developing Successful Oracle Applications
varchar2 类型定义时, 个人认为应该选择byte 类型, 即 varchar2(20), oracle 支持的最大的字符串是 varchar2(4000), 同时, 个人认为, 当你定义一个v ...
- SVM(支持向量机)与统计机器学习 & 也说一下KNN算法
因为SVM和统计机器学习内容很多,所以从 http://www.cnblogs.com/charlesblc/p/6188562.html 这篇文章里面分出来,单独写. 为什么说SVM和统计学关系很大 ...
- golang编码转换
在网上搜索golang编码转化时,我们经常看到的文章是使用下面一些第三方库: https://github.com/djimenez/iconv-go https://github.com/qiniu ...
- mysql 建立索引场合及索引使用
索引建立场合: ① where后边字段 适合建立索引 ② order by 排序字段适合建立索引 ③ 索引覆盖 即 所要查询的字段本身就是索引 直接在索引中查询数据. 例如 select name,a ...
- Android GestureDetector方法详解
为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single ...
- 如何让JS的变量名变量化
unction message() { var k=0; // var olk+k="sdasdasd"; eval("var olk"+k+"='a ...
- eclipse不能打断点的问题
今天突然eclipse不能打断点了,按ctrl+左键也不能进行方法导向了.查了很多资料还是不清楚怎么回事. 我把原来的文件再重新复制下,这个副本竟然是正常的. 结论:把原来的文件重新编译生成class ...
- Mvc4_@RenderBody()和@RenderSection()
@RenderBody():呈现子页的主体内容 @RenderSection():呈现特别的节部分. HelperResult RenderSection(string name, bool requ ...
- 每日一笔记之2:QT之坐标系统:
以前一直多单片机开发,也没怎么使用过大的显示器,第一次学习,备忘: QT画图系统. 绘图,通过QPainter类实现. Qt的绘图系统对底层函数进行了良好的封装,使得在屏幕和设备的绘图功能可能使用相同 ...