為啥我的鼠標移出來的效果不顯示。。。。。
window.onload = function(){
var box = document.getElementById('box') ;?
box.onmouseover = function(){
play(1);
}
box.onmouseout = function(){
play(0.3);
}
}
var alpha = 0.3 ;
function play(target){
var timer = null ;
var box = document.getElementById('box') ;?
clearInterval(timer);
timer = setInterval(function(){
var speed = 0 ;
if(alert > target){
speed = -0.1;
}else{
speed = 0.1 ;
}
if(alpha == target){
clearInterval(timer);
}else{
alpha = alpha + speed;
box.style.opacity = alpha;
}
},30);
}
就是看不出是哪里錯了。。。。
2016-07-25
window.onload = function() {
var box = document.getElementById('box');
box.onmouseover = function() {
play(1);
}
box.onmouseout = function() {
play(0.3);
}
}
var alpha = 0.3;
var timer = null;
function play(target) {
clearInterval(timer);
var box = document.getElementById('box');
var speed = 0;
timer = setInterval(function() {
if(alpha > target) {
speed = -0.01;?
} else {
speed = 0.01;
}
if(alpha == target) {
clearInterval(timer);
} else {
alpha = alpha + speed;
box.style.opacity = alpha;
}
}, 3);
}
這個是我復制你的代碼之后拉過去改的 ?測試可以 你對照下
2016-07-25
window.onload = function() {
var box = document.getElementById('box');
box.onmouseover = function() {
play(100);
}
box.onmouseout = function() {
play(30);
}
var timer = null;//這倆貨既不在上面的{}里
var alpha = 30;//也不在下面的{}里
function play(target) {
clearInterval(timer);
timer = setInterval(function() {
var speed = 0;
if(alpha > target) {
speed = -10;
} else {
speed = 10;
}
if(alpha == target) {
clearInterval(timer);
} else {
alpha += speed;
box.style.opacity = alpha / 100;
}
}, 30);
}
}
2016-07-25
window.onload = function(){
var box = document.getElementById('box') ;?
var timer = null ;
box.onmouseover = function(){
play(100);
}
box.onmouseout = function(){
play(30);
}
function play(target){
var alpha = 30 ;
clearInterval(timer);
timer = setInterval(function(){
var speed = 0 ;
if(alpha > target){
speed = -10;
}else{
speed = 10 ;
}
if(alpha == target){
clearInterval(timer);
}else{
alpha += speed;
box.style.opacity = alpha/100;
}
},30);
}
}
2016-07-25
第一:
if(alert > target){//這里應該是 alpha
speed = -0.1;
}else{
speed = 0.1 ;
}
第二:
function play(target){
var timer = null ;//這個變量應該聲明在方法體外面。
var box = document.getElementById('box') ;?
clearInterval(timer);
第三:
speed的絕對值為0.1 最后鼠標移出div可能會閃屏 ?建議縮小 然后同時縮小相應倍數計時器的間隔時間 達到效果不變