js off 缓动动画
动画也有很多种,一起来学习,缓动动画吧
缓动动画
var but=document.getElementsByTagName("button")[];
var box=document.getElementsByTagName("div")[];
but.onclick=function(){
setInterval(function(){
//缓动效果,如果缓动,步子越来越小
// 步长的公式=盒子现在的位置+(盒子目标位置-现在的位置)/10;
var target=;
var step=(target-box.offsetLeft)/;
//动画原理:盒子未来的位置 = 盒子当前的位置+步长
box.style.left=box.offsetLeft+step+"px"
//清除定时器,当目标值减去盒子的位置 小于步子 清除
if(box.offsetLeft-<(-box.offsetLeft)/){
clearInterval(strme)
}
},)
}
<script>
var but=document.getElementsByTagName("button")[];
var div=document.getElementsByTagName("div")[];
var timer=null;
but.onclick=function(){
//要用定时器先请定时器
clearInterval(timer);
timer=setInterval(function() {
//缓动效果,如果缓动,步子越来越小
// 步长的公式=盒子现在的位置+(盒子目标位置-现在的位置)/10;
var step=(-div.offsetLeft)/;
//最后10像素的时候都是1像素1像素的向目标位置移动,就能够到达指定位置
//拓展,目标值 - 现在值 差值大于0时向上曲整,小于0的时候,向小取整
// step=(0-div.offsetLeft)<0 ? Math.floor(step) : Math.ceil(step); //对的结果在前面
//step=0<div.offsetLeft ? Math.floor(step) : Math.ceil(step);
step= step> ? Math.ceil(step) : Math.floor(step);
div.style.left=div.offsetLeft+ step + "px";
console.log();
//跳出条件 目标值-当前位置绝对值<步长绝对值,因为与可能是
if( Math.abs(-div.offsetLeft)< Math.abs(step)){
div.style.left =+"px";
clearInterval(timer)
}
},)
}

<script>
var but=document.getElementsByTagName("button");
var box=document.getElementsByTagName("div");
var timer=null;
//调用函数
but[].onclick=function () {
animate(box[],)
};
but[].onclick=function () {
animate(box[],)
};
//分装代码
function animate(ele,target){
//用定时器先清除定时器
clearInterval(ele.timer);
//定义定时器
ele.timer=setInterval(function(){
//定义缓动 动画步长 越来越慢
// 步长=(目标值-现在值)/10
var step=(target-ele.offsetLeft )/;
//对步长二次加工(小于步长相下去值 大于)
step=step> ? Math.ceil(step):Math.floor(step);
//赋值给left
console.log();
ele.style.left=ele.offsetLeft+step+"px";
//清除定时器
if(Math.abs(target-ele.offsetLeft)<=Math.abs(step)){
//处理小数赋值
ele.style.left=target+"px";
clearInterval(ele.timer)
}
},)
}
</script>

<style>
*{margin:; padding:; }
body{background:rgba(,,,0.8)}
.box{width:800px; height:42px; border-radius: 8px; background: #fff url("../image/wifi.png") no-repeat right center ; margin:200px auto; position: relative;}
.box ul{position: relative;}
.box li{float:left; list-style: none; width:83px; height:42px; text-align: center; font: 16px/42px "simsun"; cursor: pointer;}
.box span{width:83px; height:42px; background: url("../image/cloud.gif") no-repeat ; position: absolute; left:; }
</style>
<script>
//需求1:属于移到哪个li上,span就停在那个li上,离开后,span会到原点
//需求2;鼠标点击哪个li就记录li标签,移开的时候,span就会到记录的li上
window.onload=function () {
//获取元素
var liArr=document.getElementsByTagName("li");
var span=document.getElementsByTagName("span")[];
var liWidth=liArr[].offsetWidth;
var coun=;
//绑定事件
for(var i=; i<liArr.length; i++){
//绑定属性index
liArr[i].index=i;
liArr[i].onmouseover=function () {
animate(span,this.index*liWidth)
}
liArr[i].onmouseout=function () {
animate(span,coun*liWidth)
}
liArr[i].onclick=function () {
coun=this.index;
animate(span,coun*liWidth)
}
}
}
//分装代码
function animate(ele,target){
clearInterval(ele.timer);
ele.timer=setInterval(function(){
var step=(target-ele.offsetLeft )/;
step=step> ? Math.ceil(step):Math.floor(step);
console.log();
ele.style.left=ele.offsetLeft+step+"px";
if(Math.abs(target-ele.offsetLeft)<=Math.abs(step)){
ele.style.left=target+"px";
clearInterval(ele.timer)
}
},)
}
</script>
</head>
<body>
<div class="box">
<span></span>
<ul>
<li>首页新闻</li>
<li>搜狐新闻</li>
<li>腾讯新闻</li>
<li>河北新闻</li>
<li>北京新闻</li>
<li>湖南新闻</li>
<li>山东新闻</li>
<li>湖北新闻</li>
</ul>
</div>
<style>
*{margin:; padding:; }
body{background:rgba(,,,0.8)}
.box{width:800px; height:42px; border-radius: 8px; background: #fff url("../image/wifi.png") no-repeat right center ; margin:200px auto; position: relative;}
.box ul{position: relative;}
.box li{float:left; list-style: none; width:83px; height:42px; text-align: center; font: 16px/42px "simsun"; cursor: pointer;}
.box span{width:83px; height:42px; background: url("../image/cloud.gif") no-repeat ; position: absolute; left:; }
</style>
<script>
//需求1:属于移到哪个li上,span就停在那个li上,离开后,span会到原点
//需求2;鼠标点击哪个li就记录li标签,移开的时候,span就会到记录的li上
window.onload=function () {
//获取元素
var liArr=document.getElementsByTagName("li");
var span=document.getElementsByTagName("span")[];
var liWidth=liArr[].offsetWidth;
var coun=;
//绑定事件
for(var i=; i<liArr.length; i++){
//绑定属性index
liArr[i].index=i;
liArr[i].onmouseover=function () {
animate(span,this.index*liWidth)
}
liArr[i].onmouseout=function () {
animate(span,coun*liWidth)
}
liArr[i].onclick=function () {
coun=this.index;
animate(span,coun*liWidth)
}
}
}
//分装代码
function animate(ele,target){
clearInterval(ele.timer);
ele.timer=setInterval(function(){
var step=(target-ele.offsetLeft )/;
step=step> ? Math.ceil(step):Math.floor(step);
console.log();
ele.style.left=ele.offsetLeft+step+"px";
if(Math.abs(target-ele.offsetLeft)<=Math.abs(step)){
ele.style.left=target+"px";
clearInterval(ele.timer)
}
},)
}
</script>
</head>
<body>
<div class="box">
<span></span>
<ul>
<li>首页新闻</li>
<li>搜狐新闻</li>
<li>腾讯新闻</li>
<li>河北新闻</li>
<li>北京新闻</li>
<li>湖南新闻</li>
<li>山东新闻</li>
<li>湖北新闻</li>
</ul>
</div>

js off 缓动动画的更多相关文章
- JS实现缓动动画效果
原理如下: 假设要从数值A变化到数值B,如果是线性运动,则每次移动距离是一样:如果是缓动,每次移动距离不一样.那如何才能不一样呢?很简单,按比例移动就可以. 例如:每次移动剩余距离的一半. 对吧,超容 ...
- JS基础知识——缓动动画
基于距离的缓动动画 原理:设定起始位置 start 和终止位置 end,变化会越来越慢. 公式:start=start+(end-start)/10; 这个10不是固定的,想分成多少份就分成 ...
- js简单动画:匀速动画、缓动动画、多物体动画以及透明度动画
主要实现以下几种简单的动画效果(其实原理基本相同): 1.匀速动画:物体的速度固定 2.缓动动画:物体速度逐渐变慢 3.多物体动画 4.透明度动画 效果实现: 1.匀速动画(以物体左右匀速运动为例) ...
- JS-特效 ~ 04. client对象、网页可视区域的宽高、client / offset / scroll 三大家族的区别、冒泡事件、事件委托、获取内嵌式和外链式属性getStyle(ele,attr) ;、缓动动画封装
知识点: 模拟滚动条的解除事件问题 : event内置对象,包含 了大量事件: page兼容性: pageX || clientX + scool().top : if (true === a)tr ...
- jQuery-1.9.1源码分析系列(十五) 动画处理——缓动动画核心Tween
在jQuery内部函数Animation中调用到了createTweens()来创建缓动动画组,创建完成后的结果为: 可以看到上面的缓动动画组有四个原子动画组成.每一个原子动画的信息都包含在里面了. ...
- 背水一战 Windows 10 (15) - 动画: 缓动动画
[源码下载] 背水一战 Windows 10 (15) - 动画: 缓动动画 作者:webabcd 介绍背水一战 Windows 10 之 动画 缓动动画 - easing 示例演示缓动(easing ...
- WPF界面设计技巧(7)—模拟电梯升降的缓动动画
原文:WPF界面设计技巧(7)-模拟电梯升降的缓动动画 如同Flash一样,WPF的亮点之一也在于其擅于表现平滑的动画效果,但以移动动画来说,仅凭简单的起始位置.目标位置,所产生的动画仍会非常生硬,这 ...
- Windows Phone开发(42):缓动动画
原文:Windows Phone开发(42):缓动动画 前面在讨论关键帧动画的时候,我有意把几个带缓动动画的关键帧动画忽略掉,如EasingColorKeyFrame.EasingDoubleKeyF ...
- 重新想象 Windows 8 Store Apps (19) - 动画: 线性动画, 关键帧动画, 缓动动画
原文:重新想象 Windows 8 Store Apps (19) - 动画: 线性动画, 关键帧动画, 缓动动画 [源码下载] 重新想象 Windows 8 Store Apps (19) - 动画 ...
随机推荐
- MySQL连接使用
在mysql查询中,我们会通过排序,分组等在一张表中读取数据,这是比较简单的,但是在真正的应用中经常需要从多个数据表中读取数据.下面就为大家介绍这种方式,链接查询join. INNER JOIN(内连 ...
- 大数据的前世今生【Hadoop、Spark】
一.大数据简介 大数据是一个很热门的话题,但它是什么时候开始兴起的呢? 大数据[big data]这个词最早在UNIX用户协会的会议上被使用,来自SGI公司的科学家在其文章“大数据与下一代基础架构 ...
- Python 网络爬虫的常用库汇总
爬虫的编程语言有不少,但 Python 绝对是其中的主流之一.下面就为大家介绍下 Python 在编写网络爬虫常常用到的一些库. 请求库:实现 HTTP 请求操作 urllib:一系列用于操作URL的 ...
- kvm虚拟化环境中的时区设置
guest OS时间保持 kvm技术是全虚拟化,guest OS并不需要做修改就可以直接运行,然而在计时方面却存在问题,guest OS计时的一种方式是通过时钟中断计数,进而换算得到,但host产生的 ...
- flask基础2
一个装饰器无法装饰多个函数的解决方法 当我们想在flask的多个视图函数前添加同一个装饰器时,如果什么都不做会报一个错 AssertionError: View function mapping is ...
- Druid Spring Boot Starter 从配置到简单运行 -解决zone不匹配 -解决dataSource加载失败
Druid Spring Boot Starter 中文 | English Druid Spring Boot Starter 用于帮助你在Spring Boot项目中轻松集成Druid数据库连接池 ...
- P1801 黑匣子[堆]
题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...
- iframe使用总结(实战)
说在前面的话,iframe是可以做很多事情的.例如:a>通过iframe实现跨域;b>使用iframe解决IE6下select遮挡不住的问题c>通过iframe解决Ajax的前进后退 ...
- 2019-2020-1 20199312《Linux内核原理与分析》第十二周作业
实验背景 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发.在本实验中,学生需要亲手重现攻击来理解该漏洞,并回答一些问题. ...
- javascript单一复制粘贴
<div id="copy-txt">内容内容</div> <button type="submit" onclick=" ...