2017-11-09 7 views
0

私はポンドとドルの間で変換が可能でなければならない値段表のために何かを作成していますが、私はこれを基礎としています。スライディングメカニックが、スターリング」の両方の価格との間の変換、それの機能を生成するために複数回、私はそれが唯一のDIVの内容を切り替えるボタン

var shown = 'sterling'; 
 

 
function swap() { 
 
    if (shown === 'sterling') { 
 
    document.getElementById('dollars-1').style.display = "inline-block"; 
 
    document.getElementById('dollars-2').style.display = "inline-block"; 
 
    document.getElementById('dollars-3').style.display = "inline-block"; 
 
    document.getElementById('sterling-1').style.display = "none"; 
 
    document.getElementById('sterling-2').style.display = "none"; 
 
    document.getElementById('sterling-3').style.display = "none"; 
 
    shown = 'dollars'; 
 
    } else { 
 
    document.getElementById('sterling-1').style.display = "inline-block"; 
 
    document.getElementById('sterling-2').style.display = "inline-block"; 
 
    document.getElementById('sterling-3').style.display = "inline-block"; 
 
    document.getElementById('dollars-1').style.display = "none"; 
 
    document.getElementById('dollars-2').style.display = "none"; 
 
    document.getElementById('dollars-3').style.display = "none"; 
 
    shown = 'sterling'; 
 
    } 
 
};
body { 
 
    background-color: #707070; 
 
} 
 

 
.switch { 
 
    position: relative; 
 
    height: 26px; 
 
    width: 120px; 
 
    background: rgba(0, 0, 0, 0.25); 
 
    border-radius: 3px; 
 
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
} 
 

 
.switch-label { 
 
    position: relative; 
 
    z-index: 2; 
 
    float: left; 
 
    width: 58px; 
 
    line-height: 26px; 
 
    font-size: 11px; 
 
    color: rgba(255, 255, 255, 0.35); 
 
    text-align: center; 
 
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.45); 
 
    cursor: pointer; 
 
} 
 

 
.switch-label:active { 
 
    font-weight: bold; 
 
} 
 

 
.switch-label-off { 
 
    padding-left: 2px; 
 
} 
 

 
.switch-label-on { 
 
    padding-right: 2px; 
 
} 
 

 

 
/* 
 
* Note: using adjacent or general sibling selectors combined with 
 
*  pseudo classes doesn't work in Safari 5.0 and Chrome 12. 
 
*  See this article for more info and a potential fix: 
 
*  http://css-tricks.com/webkit-sibling-bug/ 
 
*/ 
 

 
.switch-input { 
 
    display: none; 
 
} 
 

 
.switch-input:checked+.switch-label { 
 
    font-weight: bold; 
 
    color: rgba(0, 0, 0, 0.65); 
 
    text-shadow: 0 1px rgba(255, 255, 255, 0.25); 
 
    -webkit-transition: 0.15s ease-out; 
 
    -moz-transition: 0.15s ease-out; 
 
    -o-transition: 0.15s ease-out; 
 
    transition: 0.15s ease-out; 
 
} 
 

 
.switch-input:checked+.switch-label-on~.switch-selection { 
 
    left: 60px; 
 
    /* Note: left: 50% doesn't transition in WebKit */ 
 
} 
 

 
.switch-selection { 
 
    display: block; 
 
    position: absolute; 
 
    z-index: 1; 
 
    top: 2px; 
 
    left: 2px; 
 
    width: 58px; 
 
    height: 22px; 
 
    background: #65bd63; 
 
    border-radius: 3px; 
 
    background-image: -webkit-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: -moz-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: -o-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: linear-gradient(to bottom, #9dd993, #65bd63); 
 
    -webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2); 
 
    box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2); 
 
    -webkit-transition: left 0.15s ease-out; 
 
    -moz-transition: left 0.15s ease-out; 
 
    -o-transition: left 0.15s ease-out; 
 
    transition: left 0.15s ease-out; 
 
}
<button id="sterling-1">£100.00</button> 
 
<button id="sterling-2">£100.00</button> 
 
<button id="sterling-3">£100.00</button> 
 
<button id="dollars-1" style="display:none">$131.00</button> 
 
<button id="dollars-2" style="display:none">$131.00</button> 
 
<button id="dollars-3" style="display:none">$131.00</button> 
 
<br/> 
 
<br/> 
 

 
<div class="switch"> 
 
    <input type="radio" class="switch-input" name="view" value="week" id="week" checked> 
 
    <label for="week" class="switch-label switch-label-off" id="swap" onclick="swap()">Sterling</label> 
 
    <input type="radio" class="switch-input" name="view" value="month" id="month"> 
 
    <label for="month" class="switch-label switch-label-on" id="swap" onclick="swap()">Dollars</label> 
 
    <span class="switch-selection"></span> 
 
</div>
この問題を取り除くために、一度機能を実行できるように、私はそれを作ることができますどのように思ったんだけど

助けていただけたら幸いです!

答えて

1

ラジオボタンのchangeイベントのコールバックとしてswap関数を設定します。そのため、スイッチの状態が変更された後にのみ呼び出されます。

さらに、ラジオボタンに意味のある名前を付けることをお勧めします。 "week""month"はあまり意味がないようです。 ;-)また、id="swap"を2回使用していますが、IDは一意でなければなりません。その後、

document.getElementById('currency_sterling').addEventListener('change', swap); 
 
document.getElementById('currency_dollars').addEventListener('change', swap); 
 

 
function swap(ev) { 
 
    if (ev.target.value === 'dollars') { 
 
    document.getElementById('dollars-1').style.display = "inline-block"; 
 
    document.getElementById('dollars-2').style.display = "inline-block"; 
 
    document.getElementById('dollars-3').style.display = "inline-block"; 
 
    document.getElementById('sterling-1').style.display = "none"; 
 
    document.getElementById('sterling-2').style.display = "none"; 
 
    document.getElementById('sterling-3').style.display = "none"; 
 
    } else { 
 
    document.getElementById('sterling-1').style.display = "inline-block"; 
 
    document.getElementById('sterling-2').style.display = "inline-block"; 
 
    document.getElementById('sterling-3').style.display = "inline-block"; 
 
    document.getElementById('dollars-1').style.display = "none"; 
 
    document.getElementById('dollars-2').style.display = "none"; 
 
    document.getElementById('dollars-3').style.display = "none"; 
 
    } 
 
};
body { 
 
    background-color: #707070; 
 
} 
 

 
.switch { 
 
    position: relative; 
 
    height: 26px; 
 
    width: 120px; 
 
    background: rgba(0, 0, 0, 0.25); 
 
    border-radius: 3px; 
 
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
} 
 

 
.switch-label { 
 
    position: relative; 
 
    z-index: 2; 
 
    float: left; 
 
    width: 58px; 
 
    line-height: 26px; 
 
    font-size: 11px; 
 
    color: rgba(255, 255, 255, 0.35); 
 
    text-align: center; 
 
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.45); 
 
    cursor: pointer; 
 
} 
 

 
.switch-label:active { 
 
    font-weight: bold; 
 
} 
 

 
.switch-label-off { 
 
    padding-left: 2px; 
 
} 
 

 
.switch-label-on { 
 
    padding-right: 2px; 
 
} 
 

 

 
/* 
 
* Note: using adjacent or general sibling selectors combined with 
 
*  pseudo classes doesn't work in Safari 5.0 and Chrome 12. 
 
*  See this article for more info and a potential fix: 
 
*  http://css-tricks.com/webkit-sibling-bug/ 
 
*/ 
 

 
.switch-input { 
 
    display: none; 
 
} 
 

 
.switch-input:checked+.switch-label { 
 
    font-weight: bold; 
 
    color: rgba(0, 0, 0, 0.65); 
 
    text-shadow: 0 1px rgba(255, 255, 255, 0.25); 
 
    -webkit-transition: 0.15s ease-out; 
 
    -moz-transition: 0.15s ease-out; 
 
    -o-transition: 0.15s ease-out; 
 
    transition: 0.15s ease-out; 
 
} 
 

 
.switch-input:checked+.switch-label-on~.switch-selection { 
 
    left: 60px; 
 
    /* Note: left: 50% doesn't transition in WebKit */ 
 
} 
 

 
.switch-selection { 
 
    display: block; 
 
    position: absolute; 
 
    z-index: 1; 
 
    top: 2px; 
 
    left: 2px; 
 
    width: 58px; 
 
    height: 22px; 
 
    background: #65bd63; 
 
    border-radius: 3px; 
 
    background-image: -webkit-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: -moz-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: -o-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: linear-gradient(to bottom, #9dd993, #65bd63); 
 
    -webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2); 
 
    box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2); 
 
    -webkit-transition: left 0.15s ease-out; 
 
    -moz-transition: left 0.15s ease-out; 
 
    -o-transition: left 0.15s ease-out; 
 
    transition: left 0.15s ease-out; 
 
}
<button id="sterling-1">£100.00</button> 
 
<button id="sterling-2">£100.00</button> 
 
<button id="sterling-3">£100.00</button> 
 
<button id="dollars-1" style="display:none">$131.00</button> 
 
<button id="dollars-2" style="display:none">$131.00</button> 
 
<button id="dollars-3" style="display:none">$131.00</button> 
 
<br/> 
 
<br/> 
 

 
<div class="switch"> 
 
    <input type="radio" class="switch-input" name="currency" value="sterling" id="currency_sterling" checked> 
 
    <label for="currency_sterling" class="switch-label switch-label-off">Sterling</label> 
 
    <input type="radio" class="switch-input" name="currency" value="dollars" id="currency_dollars"> 
 
    <label for="currency_dollars" class="switch-label switch-label-on">Dollars</label> 
 
    <span class="switch-selection"></span> 
 
</div>

+0

ありがとうございます!ダウンボートは私のものではなかったので、誰がそれをしたのか分からない - これは完璧なトリックだ!私はあなたに両方正しい答えの刻みを与えることができたらいい – Aaron

+0

助けて嬉しい!グローバル変数 'shown'を取り除き、代わりにラジオの' value'変数を使うようにコードを更新しました。 – Connum

+0

ありがとうございました:) – Aaron

2

のではなく、手動で毎回交換のいずれかのオプションをクリックすると、変数に示されているものに保ち、スワップ機能に中価格タイプを渡し、ベースにあなたが欲しいものを表示それのオフ。こうすることで、いずれかのボタンを好きなだけ何回でもクリックすることができますが、他のオプションをクリックすると変更されます。私はあなたの例を変更しました。

function swap(priceType) { 
 
    if (priceType === 'dollars') { 
 
    document.getElementById('dollars-1').style.display = "inline-block"; 
 
    document.getElementById('dollars-2').style.display = "inline-block"; 
 
    document.getElementById('dollars-3').style.display = "inline-block"; 
 
    document.getElementById('sterling-1').style.display = "none"; 
 
    document.getElementById('sterling-2').style.display = "none"; 
 
    document.getElementById('sterling-3').style.display = "none"; 
 
    } else { 
 
    document.getElementById('sterling-1').style.display = "inline-block"; 
 
    document.getElementById('sterling-2').style.display = "inline-block"; 
 
    document.getElementById('sterling-3').style.display = "inline-block"; 
 
    document.getElementById('dollars-1').style.display = "none"; 
 
    document.getElementById('dollars-2').style.display = "none"; 
 
    document.getElementById('dollars-3').style.display = "none"; 
 
    } 
 
};
body { 
 
    background-color: #707070; 
 
} 
 

 
.switch { 
 
    position: relative; 
 
    height: 26px; 
 
    width: 120px; 
 
    background: rgba(0, 0, 0, 0.25); 
 
    border-radius: 3px; 
 
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
} 
 

 
.switch-label { 
 
    position: relative; 
 
    z-index: 2; 
 
    float: left; 
 
    width: 58px; 
 
    line-height: 26px; 
 
    font-size: 11px; 
 
    color: rgba(255, 255, 255, 0.35); 
 
    text-align: center; 
 
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.45); 
 
    cursor: pointer; 
 
} 
 

 
.switch-label:active { 
 
    font-weight: bold; 
 
} 
 

 
.switch-label-off { 
 
    padding-left: 2px; 
 
} 
 

 
.switch-label-on { 
 
    padding-right: 2px; 
 
} 
 

 

 
/* 
 
* Note: using adjacent or general sibling selectors combined with 
 
*  pseudo classes doesn't work in Safari 5.0 and Chrome 12. 
 
*  See this article for more info and a potential fix: 
 
*  http://css-tricks.com/webkit-sibling-bug/ 
 
*/ 
 

 
.switch-input { 
 
    display: none; 
 
} 
 

 
.switch-input:checked+.switch-label { 
 
    font-weight: bold; 
 
    color: rgba(0, 0, 0, 0.65); 
 
    text-shadow: 0 1px rgba(255, 255, 255, 0.25); 
 
    -webkit-transition: 0.15s ease-out; 
 
    -moz-transition: 0.15s ease-out; 
 
    -o-transition: 0.15s ease-out; 
 
    transition: 0.15s ease-out; 
 
} 
 

 
.switch-input:checked+.switch-label-on~.switch-selection { 
 
    left: 60px; 
 
    /* Note: left: 50% doesn't transition in WebKit */ 
 
} 
 

 
.switch-selection { 
 
    display: block; 
 
    position: absolute; 
 
    z-index: 1; 
 
    top: 2px; 
 
    left: 2px; 
 
    width: 58px; 
 
    height: 22px; 
 
    background: #65bd63; 
 
    border-radius: 3px; 
 
    background-image: -webkit-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: -moz-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: -o-linear-gradient(top, #9dd993, #65bd63); 
 
    background-image: linear-gradient(to bottom, #9dd993, #65bd63); 
 
    -webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2); 
 
    box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2); 
 
    -webkit-transition: left 0.15s ease-out; 
 
    -moz-transition: left 0.15s ease-out; 
 
    -o-transition: left 0.15s ease-out; 
 
    transition: left 0.15s ease-out; 
 
}
<button id="sterling-1">£100.00</button> 
 
<button id="sterling-2">£100.00</button> 
 
<button id="sterling-3">£100.00</button> 
 
<button id="dollars-1" style="display:none">$131.00</button> 
 
<button id="dollars-2" style="display:none">$131.00</button> 
 
<button id="dollars-3" style="display:none">$131.00</button> 
 
<br/> 
 
<br/> 
 

 
<div class="switch"> 
 
    <input type="radio" class="switch-input" name="view" value="week" id="week" checked> 
 
    <label for="week" class="switch-label switch-label-off" id="swap" onclick="swap('sterling')">Sterling</label> 
 
    <input type="radio" class="switch-input" name="view" value="month" id="month"> 
 
    <label for="month" class="switch-label switch-label-on" id="swap" onclick="swap('dollars')">Dollars</label> 
 
    <span class="switch-selection"></span> 
 
</div>

+0

ありがとうございました! – Aaron

関連する問題