当前位置:网站首页>Case summary of rotation chart moving speed (constant speed, slow motion)
Case summary of rotation chart moving speed (constant speed, slow motion)
2022-07-19 05:12:00 【Create splendid -- hand in hand with you】
js The content part :
var box1 = document.querySelector(".box1");
var box2 = document.querySelector(".box2");
box1.onclick = function(){
move(this,100);
}
box2.onclick = function(){
slow(this,100);
}
// uniform
function move(obj,target){
obj.myInter = setInterval(function(){
// The first step is to obtain the current distance to the left
var offsetLeft = obj.offsetLeft;
var num = 10;// go 10px
if(offsetLeft==target){
clearInterval(obj.myInter);// Clear timer
}else{
obj.style.left = offsetLeft + num + "px";
}
},1000)
}
// Slow motion
function slow(obj,target){
obj.myInter = setInterval(function(){
// The first step is to obtain the current distance to the left
var offsetLeft = obj.offsetLeft;
var num = (target - offsetLeft)/10;
num>0?num = Math.ceil(num):num=Math.floor(num);
if(offsetLeft==target){
clearInterval(obj.myInter);// Clear timer
}else{
obj.style.left = offsetLeft + num + "px";
}
},500)
}
The above is to make the rotation chart uniform and slow js effect , Extract below . In the process of programming, you can extract the functions , Just call it directly .
Extract part :
uniform
// uniform
function move(obj,target){
obj.myInter = setInterval(function(){
// The first step is to obtain the current distance to the left
var offsetLeft = obj.offsetLeft;
var num = 10;// go 10px
if(offsetLeft==target){
clearInterval(obj.myInter);// Clear timer
}else{
obj.style.left = offsetLeft + num + "px";
}
},1000)
}
Slow motion :
// Slow motion
function slow(obj,target){
obj.myInter = setInterval(function(){
// The first step is to obtain the current distance to the left
var offsetLeft = obj.offsetLeft;
var num = (target - offsetLeft)/10;
num>0?num = Math.ceil(num):num=Math.floor(num);
if(offsetLeft==target){
clearInterval(obj.myInter);// Clear timer
}else{
obj.style.left = offsetLeft + num + "px";
}
},500)
}
边栏推荐
猜你喜欢

vscode终端无法使用解决的办法

Use of transactions - Django, SQL tools

Mysql database experiment training 6, data view (detailed)

模拟库函数

实习项目1-个性化主页配置

es6新增-运算符的扩展

Internship project 3- change owner

Teddy Cup title a full version optimization update (4/23)

Applet cloud development form submission and data acquisition in the page

Asynchronous data SMS verification code
随机推荐
【C语言—零基础第十一课】旋转大转盘之指针
热更新及其原理
PAT乙级1002:写出这个数
Asynchronous data SMS verification code
滚动轮加载的两种js方法及模态框拖拽归总
Uniapp uses uview to realize folding panel
模拟库函数
【C语言—零基础_学习_复习_第五课】基本运算符的运算性质
硬核结构体,暴力解读
PCA feature dimensionality reduction of machine learning + case practice
实习项目3-更改所有者
Class object automatic injection attribute operation tool
机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
Internship project 3- change owner
[batch] batch delete intermediate folder - personal research script
【LeetCode——编程能力入门第二天】运算符(位1的个数/整数的各位积和之差)
决策树原理和案例应用-泰坦尼克号生存预测
小程序云开发表单提交并在页面中获取数据
C语言初学者之初识代码专项练习
(精讲)Es6 剩余参数,ES6内置对象,模板字符串内容(详例宝典)及灵活运用项目的实战案例