ここでは、クラスを一切使用せずにopacity
とオーディオボリュームを一度に変更するためのスクリプトです。
代わりに、各要素の実際の「レベル」値を格納するためにいくつかのdata
属性を使用します。
あなたのHTMLやCSSには触れていません。 これで遊ぶには素晴らしいスタータースクリプトがあります。
// Set a level to each data attribute elements (to start with).
$(".element").each(function(){
$(this).data("opacityLevel","0.8");
$(this).data("volumeLevel","1");
});
// On click, check the level.
$(".element").on("click", function(){
var opacityLevel = parseFloat($(this).data("opacityLevel"));
console.log("Opacity before: "+opacityLevel);
var volumeLevel = parseFloat($(this).data("volumeLevel"));
console.log("Volume before: "+volumeLevel);
// Increment it and ensure to stay between 0.2 and 0.8
opacityLevel += 0.2;
if(opacityLevel == 1){
opacityLevel = 0.2;
}
if(opacityLevel >= 0.4){
volumeLevel +=0.3333
}else{
volumeLevel = 0
}
// Store new levels
$(this).data("opacityLevel",opacityLevel);
$(this).data("volumeLevel",volumeLevel);
console.log("Opacity after: "+opacityLevel);
console.log("Volume after: "+volumeLevel);
// Apply the new level to opacity and audio volume.
$(this).css("opacity",opacityLevel);
$(this).find("audio")[0].volume = volumeLevel;
});
* {
box-sizing: border-box;
}
body {
padding: 0;
margin: 0;
}
.player {
display: flex;
flex-wrap: wrap;
position: relative;
margin: auto;
top: 50vh;
transform: translateY(-50%);
width: 256px;
height: 256px;
background-color: hsla(0, 0%, 80%, 1);
}
.element {
padding: 8px;
width: 128px;
height: 128px;
background-color: hsla(0, 0%, 60%, 1);
}
.o-1 {opacity: 0.2;}
.o-2 {opacity: 0.4;}
.o-3 {opacity: 0.6;}
.o-4 {opacity: 0.8;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="player">
<div class="element">
<audio autoplay loop>
<source src="blabla-1.mp3" type="audio/mpeg">
</audio>
</div>
<div class="element">
<audio autoplay loop>
<source src="blabla-2.mp3" type="audio/mpeg">
</audio>
</div>
<div class="element">
<audio autoplay loop>
<source src="blabla-3.mp3" type="audio/mpeg">
</audio>
</div>
<div class="element">
<audio autoplay loop>
<source src="blabla-4.mp3" type="audio/mpeg">
</audio>
</div>
</div>
EDIT(よりスムーズに音量変更はボーナスの質問)
だから、この部分は変更します。これに
if(opacityLevel >= 0.4){
volumeLevel +=0.3333
}else{
volumeLevel = 0
}
を:
var volumeChangeDelay;
if(opacityLevel >= 0.4){
volumeLevel +=0.3333
volumeChangeDelay = 2000; // Slow increase
}else{
volumeLevel = 0;
volumeChangeDelay = 500; // Faster decrease
}
そして、 "適用量は、" 次のようになります。
// Apply the new level to opacity and audio volume.
$(this).css("opacity",opacityLevel);
$(this).find("audio").animate({"volume":volumeLevel},volumeChangeDelay);
私はCodePenでそれを働きました。
あなたのJSはどこですか?不透明度を変更するには、それぞれを個別にクリックする必要がありますか?ブロック1をクリックするとブロック1の不透明度が上がりますか? – Gezzasa
はい、それぞれ個別に要素1をクリックすると、要素1の不透明度と音量が増加します。 –
あなたはJavascriptを試しましたか?達成したいことはかなり簡単です。ボリュームはVIA(セレクタ).volume = 0.2; – Gezzasa