2017-08-02 14 views
0

ミュージックプレーヤーにカスタムボリュームバーとミュートボタンがあります。ミューティングは非常に簡単ですが、もう一度ボタンをクリックすると前の音量に戻ります。else文中のJavascript変数

例:現在のボリュームが50%であるとします。ミュートボタンをクリックすると0に変わります。もう一度クリックすると50%に戻ります。

この

は、私はそれを試してみました方法です:

var music = document.getElementById('music'); 
var volumehead = document.getElementById('volume-head'); 
var volumebar = document.getElementById('volume-bar'); 
var mute = document.getElementById('mute'); 

mute.addEventListener("click", muteSound); 

function muteSound(){ 
    if(mute.classList.contains('not-muted')){ 
     // Save current values before changing them 
     var lastHead = volumehead.style.marginLeft; 
     var lastVolume = music.volume; 

     // Change classname for appearance and next click 
     mute.className = "muted"; 

     // Change values to 0 
     volumehead.style.marginLeft = "0px"; 
     music.volume = 0; 
    } else { 
     // Change classname for appearance and next click 
     mute.className = "not-muted"; 

     // Use saved values 
     volumehead.style.marginLeft = lastHead; 
     music.volume = lastVolume; 
    } 
} 

ハンドラとボリュームの位置を保持する2つの変数は、彼らがelse-statementで1を持っていないという意味、if-statement内の値を与えています。

文の外で宣言すると、値は0によって上書きされます。

値を保存してボタンを次にクリックするために使用する方法はありますか?


編集:if-statementvariablesに割り当てvariablesfunctionの外宣言された場合、唯一else-statementによって使用することができます。

+1

*ハンドラの位置とボリュームを保持する2つの変数が*彼らが他の文にはアクセスできませんよという意味、if文の中に宣言されている - これはではありません'var'を使うときは真ですが、関数の先頭に吊り上げられます。変数は、 'let'または' const'を使用するときに文ブロックにのみスコープされます。とにかく、あなたの問題の解決策は、関数の外に変数を宣言することです。 –

+0

関数 'muteSound'の外の変数にボリュームを保存する必要があります。つまり、他のすべての' volumebar'変数などが一緒です。概念的には、イベントハンドラをボリュームバーにアタッチして、移動するたびにその 'volume'変数を更新する必要があります。 'volume'は常に現在選択されているボリュームを含んでいます。 'ミュート'をクリックすると、音量がミュートされ、ミュートされた音量を示すUIが表示されます。 **あなたは 'volume'の値を変更しません。** unmutingすると、ボリュームを復元し、UIを再度更新します。 – deceze

+0

@MikaelLennholm if-とelse-ステートメントの両方の変数をconsole.logにすると、elseステートメントの変数は 'undefined'を返します。 – PeterPrakker

答えて

1
var music = document.getElementById('music'); 
var volumehead = document.getElementById('volume-head'); 
var volumebar = document.getElementById('volume-bar'); 
var mute = document.getElementById('mute'); 

var lastHead, lastVolume; 

mute.addEventListener("click", muteSound); 

function muteSound() { 
    if (mute.classList.contains('not-muted')) { 
     // Save current values before changing them 
     lastHead = volumehead.style.marginLeft; 
     lastVolume = music.volume; 

     // Change classname for appearance and next click 
     mute.className = "muted"; 

     // Change values to 0 
     volumehead.style.marginLeft = "0px"; 
     music.volume = 0; 
    } else { 
     // Change classname for appearance and next click 
     mute.className = "not-muted"; 

     // Use saved values 
     volumehead.style.marginLeft = lastHead; 
     music.volume = lastVolume; 
    } 
} 
+1

このコードは質問に答えるかもしれませんが、_how_および/または_why_に関する追加のコンテキストを提供することで、問題を解決することで回答の長期的価値が向上します。 – JosefZ

0

ループ外のグローバル変数を設定し、それをボリュームに割り当ててから0に設定します。その後、その変数をelse文で参照してください!

1

lastHeadlastVolumeを超える閉鎖を持つIIFE(immediately-invoked function expression)を使用できます。

var muteSound = function() { 
    var lastHead, lastVolume; 

    return function() { 
     if (mute.classList.contains('not-muted')) { 
      // Save current values before changing them 
      lastHead = volumehead.style.marginLeft; 
      lastVolume = music.volume; 

      // Change classname for appearance and next click 
      mute.className = "muted"; 

      // Change values to 0 
      volumehead.style.marginLeft = "0px"; 
      music.volume = 0; 
     } else { 
      // Change classname for appearance and next click 
      mute.className = "not-muted"; 

      // Use saved values 
      volumehead.style.marginLeft = lastHead; 
      music.volume = lastVolume; 
     } 
    }; 
}(); 

mute.addEventListener("click", muteSound); 
+0

@decezeは、この問題を解決するために静的な関数を作成しますか? – Jerry

1
mute.addEventListener("click", muteSound); 
var lastHead = "10px"; 
var lastVolume = 10; 

function muteSound(){ 
if(mute.classList.contains('not-muted')){ 
    // Save current values before changing them 
    lastHead = volumehead.style.marginLeft; 
    lastVolume = music.volume; 

    // Change classname for appearance and next click 
    mute.className = "muted"; 

    // Change values to 0 
    volumehead.style.marginLeft = "0px"; 
    music.volume = 0; 
} else { 
    // Change classname for appearance and next click 
    mute.className = "not-muted"; 

    // Use saved values 
    volumehead.style.marginLeft = lastHead; 
    music.volume = lastVolume; 
} 
}