2016-04-18 189 views
1

CODEPEN例 http://codepen.io/dada78/pen/b50de869b75b32e220956bb36052733b如何在這種情況下使變量全局可訪問?

我試圖找出如何使在codepen我在我的功能「fadeOutUnselected(notThisId)」訪問highlightSelection功能使用第40行的selectedId變量?

function fadeOutUnselected(notThisId) { 
var tl = new TimelineMax(); 

tl.to(".options:not([id=" + notThisId +"]), input[type='radio']", 0.5, {autoAlpha:0}, "getSlidesReady+=4") //fade out all options but the selected one 
//.to("#"+ selectedId, 0.5, {y:0}) //animate selectedId option up 
    .set(".options:not([id=" + notThisId +"]), input[type='radio']", {y:0}) 
return tl; 
} 

任何幫助表示讚賞。我想要做的只是將用戶選擇的選項(「selectedId」變量)設置爲位置y:0。

謝謝!

+0

嘗試宣告它在最頂端,你的比例接近,然後在hightlightSelection功能只是修改它,這樣它在全球上市。 –

+0

感謝Caleb,我試着把它加到頂部,正好在百分比變量的下面:'var selectedId =「option-」+ label.getAttribute(「for」)。slice(-1); '並且在highlightSelection函數內部,我只是添加了'var tl = new TimelineMax();'但我仍然無法使它工作...... :-( – user2163224

回答

1

你應該這樣做

var globalSelectedId; 

function highlightSelection(label) { 
    // same code 
    globalSelectedId = selectedId; 
} 

function fadeOutUnselected(notThisId) { 
    // same code, you can acces globalSelectedID 
} 
+0

太棒了!非常感謝Tim,這個工作! – user2163224

+0

你應該有一個看看這篇文章雖然:http://stackoverflow.com/questions/2613310/ive-heard-global-variables-are-bad-what-alternative-solution-should-i-use –

相關問題