移动元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
* {
margin: 0;
padding: 0;
}
input {
margin-top: 20px;
}
div {
margin-top: 30px;
width: 200px;
height: 100px;
background-color: green;
position: absolute;
}
</style>
</head>
<body>
<input type="button" value="移动到400px" id="btn1"/>
<input type="button" value="移动到800px" id="btn2"/>
<div id="div"></div>
<script>
document.getElementById("btn1").onclick = function () {
var timeId= setInterval(function () {
var current = document.getElementById("div").offsetLeft;
var step = 9;
current += step;
if(current<=400){
document.getElementById("div").style.left=current+"px";
}else{
clearInterval(timeId);
}
}, 20);
};
document.getElementById("btn2").onclick = function () {
var timeId= setInterval(function () {
var current = document.getElementById("div").offsetLeft;
var step = 10;
current += step;
if(current<=800){
document.getElementById("div").style.left=current+"px";
}else{
clearInterval(timeId);
}
}, 20);
};
</script>
</body>
</html>
封装动画函数
function animate(element, target) {
clearInterval(element.timeId);
element.timeId = setInterval(function () {
var current = element.offsetLeft;
var step = 10;
step = current < target ? step : -step;
current += step;
if (Math.abs(target - current) > Math.abs(step)) {
element.style.left = current + "px";
} else {
clearInterval( element.timeId );
element.style.left = target + "px";
}
}, 20);
}
封装变速动画函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
margin-top: 20px;
width: 200px;
height: 100px;
background-color: green;
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<input type="button" value="移动到400px" id="btn1"/>
<input type="button" value="移动到800px" id="btn2"/>
<div id="dv">
<script src="common.js"></script>
<script>
document.getElementById("btn1").onclick = function () {
animate(my$("dv"), 400);
};
document.getElementById("btn2").onclick = function () {
animate(my$("dv"), 800);
};
function animate(element, target) {
clearInterval(element.timeId);
element.timeId = setInterval(function () {
var current = element.offsetLeft;
var step = (target-current)/10;
step = step>0?Math.ceil(step):Math.floor(step);
current += step;
element.style.left = current + "px";
if(current==target) {
clearInterval(element.timeId);
}
console.log("目标位置:"+target+",当前位置:"+current+",每次移动步数:"+step);
}, 20);
}
</script>
</div>
</body>
</html>
封装变速动画函数(任意一个属性)
function animate(element,attr ,target) {
clearInterval(element.timeId);
element.timeId = setInterval(function () {
var current = parseInt(getStyle(element,attr));
var step = (target-current)/10;
step = step>0?Math.ceil(step):Math.floor(step);
current += step;
element.style[attr] = current + "px";
if(current==target) {
clearInterval(element.timeId);
}
console.log("目标位置:"+target+",当前位置:"+current+",每次移动步数:"+step);
}, 20);
}
function getStyle (element,prop) {
if(window.getComputedStyle) {
return window.getComputedStyle(element,null)[prop];
}else{
return element.currentStyle[prop];
}
}
封装变速动画函数(针对多个属性)
function getStyle(element,attr) {
return window.getComputedStyle? window.getComputedStyle(element,null)[attr]:element.currentStyle[attr]||0;
}
function animate(element,json) {
clearInterval(element.timeId);
element.timeId=setInterval(function () {
var flag=true;
for(var attr in json){
var current=parseInt(getStyle(element,attr));
var target=json[attr];
var step=(target-current)/10;
step=step>0?Math.ceil(step):Math.floor(step);
current+=step;
element.style[attr]=current+"px";
if(current!=target){
flag=false;
}
}
if(flag){
clearInterval(element.timeId);
}
console.log("目标:"+target+",当前:"+current+",每次的移动步数:"+step);
},20);
}
封装变速动画函数(针对多个属性并且加上回调函数:达到一定条件才会触发的函数,并且不影响主函数的执行)
function getStyle(element,attr) {
return window.getComputedStyle? window.getComputedStyle(element,null)[attr]:element.currentStyle[attr]||0;
}
function animate(element,json,fn) {
clearInterval(element.timeId);
element.timeId=setInterval(function () {
var flag=true;
for(var attr in json){
var current=parseInt(getStyle(element,attr));
var target=json[attr];
var step=(target-current)/10;
step=step>0?Math.ceil(step):Math.floor(step);
current+=step;
element.style[attr]=current+"px";
if(current!=target){
flag=false;
}
}
if(flag){
clearInterval(element.timeId);
if(fn){
fn();
}
}
console.log("目标:"+target+",当前:"+current+",每次的移动步数:"+step);
},20);
}
document.getElementById("btn1").onclick=function () {
var json1={"width":400,"height":500,"left":500,"top":80};
animate(document.getElementById("div"),json1,function () {
var json2={"width":40,"height":50,"left":50,"top":800};
animate(document.getElementById("div"),json2,function () {
var json3={"width":450,"height":550,"left":550,"top":600};
animate(document.getElementById("div"),json3);
});
});
};
封装变速动画函数(针对多个属性并且加上回调函数:达到一定条件才会触发的函数,并且不影响主函数的执行 再加上层级和透明度的改变)
function animate(element, json, fn) {
clearInterval(element.timeId);
element.timeId = setInterval(function () {
var flag = true;
for (var attr in json) {
if (attr == "opacity") {
var current = getStyle(element, attr) * 100;
var target = json[attr] * 100;
var step = (target - current) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
current += step;
element.style[attr] = current / 100;
} else if (attr == "zIndex") {
element.style[attr] = json[attr];
} else {
var current = parseInt(getStyle(element, attr));
var target = json[attr];
var step = (target - current) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
current += step;
element.style[attr] = current + "px";
}
if (current != target) {
flag = false;
}
}
if (flag) {
clearInterval(element.timeId);
if (fn) {
fn();
}
}
console.log("目标:" + target + ",当前:" + current + ",每次的移动步数:" + step);
}, 20);
}