2014-02-14 89 views
0

請記住,我是非常新的編碼,並試圖淡入淡出一些圖片。當我在控制檯中輸入fadeOut()時,「疊加」將消失。但是,當我在控制檯中輸入fadeIn()時,什麼都不會發生。FadeIn FadeOut錯誤

<html> 
<head> 
<title>Weather</title> 
<style> 
    body{ 
     margin: 0 auto; 
     width: 800px; 
     background-position: center; 
     background-repeat: no-repeat; 
     background-size: 100% 100%; 
     background-color: black; 
    } 
     body.CLEAR_DAY {background-image: url(Clear_Day.jpg);} 
     body.PARTLY_CLOUDY_DAY {background-image: url(Sun.jpg);} 
     body.FOG {background-image: url();} 
     body.SNOW {background-image: url();} 
     body.WIND {background-image: url();} 
     body.PARTLY_CLOUDY_NIGHT {background-image: url();} 
     body.RAIN {background-image: url();} 
div.box{ 
    width:600px; 
    height:650px; 
    margin:30px 50px; 
    background:#3399FF; 
     border-bottom: 6px solid #000; 
    border-left: 3px solid #000; 
    border-right: 3px solid #000; 
    border-radius: 0px 0px 50px 50px; 
    border-top:6px solid #000; 
    -moz-border-radius:50px 50px; 
    -webkit-border-radius:50px 50px; 
    float:left; 
    /*padding:10px;*/ 
    text-align: center; 
    position:relative; 
    } 
div.box h1 
    { 
    margin: 0 auto; 
    font-weight:bold; 
    color:#000; 
    font-size:75px; 
    text-align: center; 
    } 
    div.box h2 
    { 
    margin: 0 auto; 
    font-weight:bold; 
    color:#000; 
    font-size:40px; 
    text-align: center; 
    } 
    div 
{ 
padding:10px 40px; 
background:#dddddd; 
width:300px; 
border-radius:0px 20px 0px 20px; 
} 
div.CLEAR_DAY 
{ 
    background-image: url(); 
    background-position: center; 
    background-repeat: no-repeat; 
} 
div.box canvas 
{ 
    position:absolute; 
    top:325px; 
    margin-top:-100px; 
    left:40px; 
} 
#overlay 
{ 
    background-color: red; 
    opacity:1; 
    width:100%; 
    height: 100%; 
    position:absolute; 
    left:0; 
    top:0; 
} 
</style> 
</head> 
<body onload="updateWeatherDisplay()"> 
<div id="overlay"></div> 
<div id="box" class="box"> 
<select id="choice" onchange="updateWeatherDisplay()"> 
    <option value="Monday">Monday</option> 
    <option value="Tuesday">Tuesday</option> 
    <option value="Wednesday">Wednesday</option> 
    <option value="Thursday">Thursday</option> 
    <option value="Friday" selected>Friday</option> 
    <option value="Saturday">Saturday</option> 
    <option value="Sunday">Sunday</option> 
</select> 
<h1 id="day"></h1> 
<h2 id="temperature"></h2> 
<canvas id="condition" width="600" height="300"></canvas> 
</div> 
<script src="skycons.js"></script> 
<script> 
var skycons = new Skycons({"color": "black"}); 
skycons.add("condition", Skycons.CLEAR_DAY); 
skycons.play(); 
var temperatures=["127&#176;","34&#176;","79&#176;","-183&#176;","0&#176;","91&#176;","213&#176;"]; 
var icons=[Skycons.WIND,Skycons.PARTLY_CLOUDY_NIGHT,Skycons.RAIN,Skycons.CLEAR_DAY,Skycons.PARTLY_CLOUDY_DAY,Skycons.FOG,Skycons.SNOW]; 
var backgrounds=["WIND","PARTLY_CLOUDY_NIGHT","RAIN","CLEAR_DAY","PARTLY_CLOUDY_DAY","FOG","SNOW"]; 

function fadeOut() 
{ 
var overlay=document.getElementById("overlay"); 
var style = window.getComputedStyle(overlay); 
var opacity=style.getPropertyValue("opacity"); 
if(opacity>0){ 
overlay.style.opacity = opacity - 0.01 
setTimeout(fadeOut,10); 
} 
} 
function fadeIn() 
{ 
var overlay=document.getElementById("overlay"); 
var style = window.getComputedStyle(overlay); 
var opacity=style.getPropertyValue("opacity"); 
if(opacity<1){ 
overlay.style.opacity = opacity + 0.01 
setTimeout(fadeIn,10); 
} 
} 
function updateWeatherDisplay() 
{ 
var selectedDay = document.getElementById("choice").selectedIndex; 
var daysOfTheWeek = document.getElementById("choice").options; 
document.getElementById("day").innerHTML = daysOfTheWeek[selectedDay].text; 
document.getElementById("temperature").innerHTML = temperatures[selectedDay]; 
skycons.set("condition", icons[selectedDay]); 
document.getElementsByTagName("body")[0].className=backgrounds[selectedDay]; 
} 
</script> 
</body> 
</html> 

我也在做這個項目只是爲了好玩。提前致謝!

+2

你可以給小提琴演示 –

+0

將來,最好把它降低到可重複性最低的測試用例,並將它發佈到JSFiddle或JSBin上。 –

回答

0

問題是opacity是一個字符串,當你從DOM檢索 - 只需將其更改爲一個整數(當然,一個浮動,真)加入一元+overlay.style.opacity = opacity + 0.01線。甚至更好,使用parseFloat

overlay.style.opacity = parseFloat(opacity) + 0.01 

爲例見this JSBin(只需點擊預覽顯示和隱藏疊加)。

它在fadeOut工作,因爲動態轉換規則使其工作,'12' + 1 === '121''12' - 1 === 11。它不一致,但這就是它的工作原理。

0

你需要清除超時功能,

試試這個,

​​

var skycons = new Skycons({"color": "black"}); 
skycons.add("condition", Skycons.CLEAR_DAY); 
skycons.play(); 
var temperatures=["127&#176;","34&#176;","79&#176;","-183&#176;","0&#176;","91&#176;","213&#176;"]; 
var icons=[Skycons.WIND,Skycons.PARTLY_CLOUDY_NIGHT,Skycons.RAIN,Skycons.CLEAR_DAY,Skycons.PARTLY_CLOUDY_DAY,Skycons.FOG,Skycons.SNOW]; 
var backgrounds=["WIND","PARTLY_CLOUDY_NIGHT","RAIN","CLEAR_DAY","PARTLY_CLOUDY_DAY","FOG","SNOW"]; 

function updateWeatherDisplay() 
{ 
var selectedDay = document.getElementById("choice").selectedIndex; 
var daysOfTheWeek = document.getElementById("choice").options; 
document.getElementById("day").innerHTML = daysOfTheWeek[selectedDay].text; 
document.getElementById("temperature").innerHTML = temperatures[selectedDay]; 
skycons.set("condition", icons[selectedDay]); 
document.getElementsByTagName("body")[0].className=backgrounds[selectedDay]; 
} 

function fadeOut() 
{ 
var overlay=document.getElementById("overlay"); 
var style = window.getComputedStyle(overlay); 
var opacity=style.getPropertyValue("opacity"); 
if(opacity>0){ 
    console.log('fadeout') 
overlay.style.opacity = opacity - 0.01 
window.myVar = setTimeout(fadeOut,10); 
} 
    else{ 
     console.log('error') 
    clearTimeout(myVar) 
    } 
} 
function fadeIn() 
{ 
var overlay=document.getElementById("overlay"); 
var style = window.getComputedStyle(overlay); 
var opacity=style.getPropertyValue("opacity"); 
if(opacity<1){ 
    clearTimeout(myVar) 

overlay.style.opacity = parseFloat(opacity) + 0.01 
console.log(overlay.style.opacity) 
myVar1 = setTimeout(fadeIn,10); 
} 

} 


$('#click1').click(function(){ 

fadeOut() 
}); 
$('#click2').click(function(){ 
    alert('') 
fadeIn() 
}); 
0

您可以使用jQuery fadeToggle。所以它非常簡單易用。 事情是這樣的:

$("button:first").click(function() { 
    $("p:first").fadeToggle("slow", "linear"); 
}); 

Check this jquery Function !

0

你必須使用pareFloat()從字符串解析不透明度值,同時增加數據浮動。