var box = document.getElementById('well_teacher_box1');
var angle=0;
var timeInterbox = null;
function startMove(){
timeInterbox = setInterval(function(){
degMove();
},10)
}
var timebox = setTimeout(function(){
startMove();
},3000);
function degMove(){
angle+=1;
setCss3(box,{transform:"rotateX("+angle+"deg)"});
if(angle % 45 == 0){
clearInterval(timeInterbox);
var timebox = setTimeout(function(){
startMove();
},3000);
}else{
angle+=1;
setCss3(box,{transform:"rotateX("+angle+"deg)"});
}
}
function setCss3 (obj,attrObj) {
按着你的思路写了一个:
var box = document.getElementById('rotate')
var angle = 0
var timer = null
function startMove() {
console.log('startMove');
timer = setInterval(doMove, 60)
}
function stopMove() {
console.log('stopMove');
clearInterval(timer)
}
function doMove() {
angle += 1
setCSS3(box, {'transform': 'rotateX('+ angle +'deg)'})
}
function setCSS3(obj, attrObj) {
for (var attr in attrObj) {
if (!attrObj.hasOwnProperty(attr)) {
continue
}
var newAttr = attr
if (attr.indexOf('-') > -1) {
var num = attr.indexOf('-')
newAttr = attr.replace(attr.substring(num, num + 2), attr.substring(num + 1, num + 2).toUpperCase())
}
obj.style[newAttr] = attrObj[attr]
newAttr = newAttr.replace(/(\w)/, function (match) {
return match.toUpperCase()
})
obj.style['webkit' + newAttr] = attrObj[attr]
obj.style['moz' + newAttr] = attrObj[attr]
obj.style['ms' + newAttr] = attrObj[attr]
obj.style['o' + newAttr] = attrObj[attr]
}
}
box.onmouseover = function () {
startMove()
}
box.onmouseout = function () {
stopMove()
}
有两个问题:
•用 setInterval 不好,要想怎么用 setTimeout 来代替。
•后面的 obj.style['webkit' + newAttr] 在浏览器里面并没有起作用。
var box = document.getElementById('rotate')
var angle = 0
var timer = null
function startMove() {
console.log('startMove');
timer = setInterval(doMove, 60)
}
function stopMove() {
console.log('stopMove');
clearInterval(timer)
}
function doMove() {
angle += 1
setCSS3(box, {'transform': 'rotateX('+ angle +'deg)'})
}
function setCSS3(obj, attrObj) {
for (var attr in attrObj) {
if (!attrObj.hasOwnProperty(attr)) {
continue
}
var newAttr = attr
if (attr.indexOf('-') > -1) {
var num = attr.indexOf('-')
newAttr = attr.replace(attr.substring(num, num + 2), attr.substring(num + 1, num + 2).toUpperCase())
}
obj.style[newAttr] = attrObj[attr]
newAttr = newAttr.replace(/(\w)/, function (match) {
return match.toUpperCase()
})
obj.style['webkit' + newAttr] = attrObj[attr]
obj.style['moz' + newAttr] = attrObj[attr]
obj.style['ms' + newAttr] = attrObj[attr]
obj.style['o' + newAttr] = attrObj[attr]
}
}
box.onmouseover = function () {
startMove()
}
box.onmouseout = function () {
stopMove()
}
有两个问题:
用 setInterval
不好,要想怎么用 setTimeout
来代替。
后面的 obj.style['webkit' + newAttr]
在浏览器里面并没有起作用。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。