Editing User:Toad64/SandboxGive feedback
Jump to navigation
Jump to search
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
| Latest revision | Your text | ||
| Line 1: | Line 1: | ||
Using Javascript, It is possible to modify the default audio element used to play audio. | Using Javascript (and a set of ::root colors i had), It is possible to modify the default audio element used to play audio. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| Line 6: | Line 6: | ||
// In your Common.js | // In your Common.js | ||
$(document).ready(function() { | $(document).ready(function() { | ||
// Find every native audio player generated by the wiki | |||
$('audio').each(function() { | $('audio').each(function() { | ||
// Create your custom component | |||
const | const myPlayer = document.createElement('audio-player'); | ||
// | // Transfer the source | ||
myPlayer.setAttribute('track', $(this).attr('src')); | |||
// Replace the native player with your custom one | |||
$(this).replaceWith(myPlayer); | |||
}); | }); | ||
}); | }); | ||
| Line 33: | Line 28: | ||
this.innerHTML = ` | this.innerHTML = ` | ||
<style> | <style> | ||
.player-controls { | .player-controls { | ||
| Line 40: | Line 33: | ||
align-items: center; | align-items: center; | ||
gap: 12px; | gap: 12px; | ||
width: 100%; | width: 100%; | ||
max-width: 400px; | max-width: 400px; | ||
background: var(--box-color, #222); | |||
padding: 10px 15px; | |||
border-radius: 50px; | border-radius: 50px; | ||
color: white; | |||
color: | |||
font-family: sans-serif; | font-family: sans-serif; | ||
} | } | ||
.playBtn { | .playBtn { | ||
width: 30px; | width: 30px; | ||
height: 30px; | height: 30px; | ||
background: var(--green-color, #4caf50); | |||
border: none; | border: none; | ||
color: white; | |||
border-radius: 50%; | border-radius: 50%; | ||
cursor: pointer; | cursor: pointer; | ||
flex-shrink: 0; | |||
} | } | ||
.seek { | .seek { | ||
-webkit-appearance: none; | |||
flex: 1; | |||
height: 6px; | height: 6px; | ||
background: #7e7e7e; | |||
border-radius: 5px; | border-radius: 5px; | ||
cursor: pointer; | cursor: pointer; | ||
} | } | ||
.time { font-size: 12px; opacity: 0.8; white-space: nowrap; } | |||
.time { | |||
</style> | </style> | ||
<div class="player-controls"> | <div class="player-controls"> | ||
<audio class="audio"><source src="${src}" type="${mime}"></audio> | <audio class="audio"><source src="${src}" type="${mime}"></audio> | ||
<button class="playBtn"> | <button class="playBtn">▶</button> | ||
<input class="seek" type="range" value="0" min="0" max="100"> | <input class="seek" type="range" value="0" min="0" max="100"> | ||
<span class="time">0:00 / 0:00</span> | <span class="time">0:00 / 0:00</span> | ||
| Line 131: | Line 76: | ||
playBtn.addEventListener("click", () => { | playBtn.addEventListener("click", () => { | ||
if (audio.paused) { audio.play(); playBtn. | if (audio.paused) { audio.play(); playBtn.textContent = '⏸'; } | ||
else { audio.pause(); playBtn. | else { audio.pause(); playBtn.textContent = '▶'; } | ||
}); | }); | ||
| Line 156: | Line 101: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This code creates a styleized media player (in this example the Old Deadlock Title Screen): | |||
[[File:Music menu lp.mp3]] | [[File:Music menu lp.mp3]] | ||