分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

js运动框架

发布时间:2023-09-06 01:42责任编辑:林大明关键词:js

最近写个运动框架,确实好用,来附上,具体就不说了,注释比较详细

简洁版:包括链式运动,没有同时运动,不需要json格式

 ???????//获取非行间样式 ???????function getStyle(obj,attr){ ????????????if(obj.currentStyle){ ??????????????????return obj.currentStyle[attr] ????????????} ????????????else{ ??????????????????return getComputedStyle(obj,false)[attr] ????????????} ???????} ???????//startMove运动函数 fn为链式运动 ???????function startMove(obj,attr,target,fn){ ???????????????clearInterval(obj.timer) ???????????????obj.timer=setInterval(function(){ ??????????????????????var getCur=0 ??????????????????????if(attr==‘opacity‘){ ???????//兼容透明度 ?????????????????????????getCur=Math.round(parseFloat(getStyle(obj,attr))*100) ??????????????????????} ??????????????????????else{ ????????????????????????????getCur=parseInt(getStyle(obj,attr)) ??????????????????????} ??????????????????????var speed=(target-getCur)/8 //缓动运动 ??????????????????????speed=speed>0?Math.ceil(speed):Math.floor(speed) ??????????????????????if(getCur==target){ ??????????????????????????????clearInterval(obj.timer) ???????????????????????????????if(fn){ ????????????????????????????????????fn.call(obj) ?????//回调函数作用域指向obj ???????????????????????????????} ??????????????????????}else{ ??????????????????????????????if(attr==‘opacity‘){ ??????????????????????????????????obj.style.filter=‘alpha(opacity:‘+(getCur+speed)+‘)‘ ??????????????????????????????????obj.style.opacity=(getCur+speed)/100 ??????????????????????????????}else{ ??????????????????????????????????obj.style[attr]=getCur+speed+‘px‘ ??????????????????????????????} ??????????????????????} ???????????????},30) ???????}

完整版:包括链式运动、同时运动,涉及到同时运动则需要利用json辅助完成

 ???????//获取非行间样式 ???????function getStyle(obj,attr){ ????????????if(obj.currentStyle){ ?????????????????return obj.currentStyle[attr] ????????????} ????????????else{ ?????????????????return getComputedStyle(obj,false)[attr] ????????????} ???????} ???????//startMove运动函数 fn为链式运动 ?json for in 循环 ???????function startMove(obj,json,fn){ ???????????????clearInterval(obj.timer) ???????????????obj.timer=setInterval(function(){ ???????????????????var flag=true ?????????????????//设置一个标签 ???????????????????for(var attr in json){ ??????????????????????var getCur=0 ??????????????????????if(attr==‘opacity‘){ ???????//兼容透明度 ?????????????????????????getCur=Math.round(parseFloat(getStyle(obj,attr))*100) ??????????????????????} ??????????????????????else{ ????????????????????????????getCur=parseInt(getStyle(obj,attr)) ??????????????????????} ??????????????????????var speed=(json[attr]-getCur)/8 //缓动运动 ???????????????????????speed=speed>0?Math.ceil(speed):Math.floor(speed) ??????????????????????if(getCur!=json[attr]){ ????//判断标签 ?要的是最后的值 ??????????????????????????????flag=false ??????????????????????} ???????????????????????????if(attr==‘opacity‘){ ????????????????????????????????obj.style.filter=‘alpha(opacity:‘+(getCur+speed)+‘)‘ ????????????????????????????????obj.style.opacity=(getCur+speed)/100 ???????????????????????????}else{ ????????????????????????????????obj.style[attr]=getCur+speed+‘px‘ ???????????????????????????} ?????????????????} ????????????????if(flag){ ??????????????????????//判断标签 ??设置在for in外面意思是for in到全部的attr才执行接下操作 ??????????????????????????????????clearInterval(obj.timer) ?????????????????????????if(fn){ ??????????????????????????????fn.call(obj) ?????//回调函数作用域指向obj ?????????????????????????} ??????????????????????} ???????????????},30) ???????}

需要注意的几点

获取非行间样式,注意浏览器的兼容问题

常用的Math方法、parseInt等的转换

回调函数,作用域的问题

json格式

js运动框架

原文地址:https://www.cnblogs.com/iDouble/p/8446494.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved