我正在使用代码突出显示使用html5音频读取的文本单词,并且它当前突出显示正在读取的句子。现在我也需要它来执行以下操作(另一个选项):当我单击该句子时,它会跳转到音频的相邻部分并播放它。
使用的代码是:
var textHighlightOn = true,
btnToggle = document.getElementById('toggleTxt'),
textDiv = document.querySelector('.text-highlight')
spns = document.getElementsByTagName("SPAN"),
audi = document.getElementById("adi");
audi.addEventListener("timeupdate", f1);
function f1(){
var i;
for (i = 0 ; i< spns.length ; i++){
var time = Number(spns[i].id.slice(2));
if(time < audi.currentTime){
if (i>0) {
spns[i -1].classList.remove('active');
spns[i -1].classList.add('active-prev');
}
spns[i].classList.add('active');
}
}
}
btnToggle.addEventListener("click", function(){
if(textHighlightOn){
textDiv.classList.add('off');
} else {
textDiv.classList.remove('off');
}
this.innerHTML = 'Highlight ' + (textHighlightOn ? 'off ' : ' on');
textHighlightOn = !textHighlightOn;
});
&#13;
body {
background: #008000;
}
.text-highlight span.active-prev {
background: #fff;
}
.text-highlight span.active {
background: #03a9f4;
}
.text-highlight.off span {
background: transparent;
}
&#13;
<audio id="adi" controls>
<source src="https://ia802508.us.archive.org/5/items/testmp3testfile/mpthreetest.mp3"/>
</audio>
<button id="toggleTxt">Highlight on</button>
<div class="text-highlight">
<pre>
<span id="ts0.5">Ok, we're trying this for a second time</span> ,
<br><span id="ts3">to test the ability</span>
<br><span id="ts6">to upload an M P</span>
<br><span id="ts9">3 file.</span>
<br><span id="ts10">Hopefully this will work!</span>
</pre>
</div>
&#13;
答案 0 :(得分:2)
我建议加载jQuery以使代码更具可读性。
var textHighlightOn = true,
btnToggle = document.getElementById('toggleTxt'),
textDiv = document.querySelector('.text-highlight')
spns = document.querySelectorAll('span'),
audi = document.getElementById("adi");
audi.addEventListener("timeupdate", f1);
function f1(){
// remove all previous actives;
[].forEach.call(spns, function(e){
e.classList.remove('active');
e.classList.remove('active-prev');
});
var i;
for (i = 0 ; i< spns.length ; i++){
var time = Number(spns[i].id.slice(2));
if(time < audi.currentTime){
if (i>0) {
spns[i-1].classList.remove('active');
spns[i-1].classList.add('active-prev');
}
spns[i].classList.add('active');
}
}
}
// listen for clicks on the spans.
[].forEach.call(spns, function(spn) {
spn.addEventListener("click", function() {
for(var i in spns){
}
var time = Number(this.id.slice(2));
audi.currentTime = time;
});
});
// Toggle text highlight
btnToggle.addEventListener("click", function(){
if(textHighlightOn){
textDiv.classList.add('off');
} else {
textDiv.classList.remove('off');
}
this.innerHTML = 'Highlight ' + (textHighlightOn ? 'off ' : ' on');
textHighlightOn = !textHighlightOn;
});
body {
background: #008000;
}
.text-highlight span.active-prev {
background: #fff;
}
.text-highlight span.active {
background: #03a9f4;
}
.text-highlight.off span {
background: transparent;
}
<audio id="adi" controls>
<source src="https://ia802508.us.archive.org/5/items/testmp3testfile/mpthreetest.mp3"/>
</audio>
<button id="toggleTxt">Highlight on</button>
<div class="text-highlight">
<pre>
<span id="ts0.5">Ok, we're trying this for a second time</span> ,
<br><span id="ts3">to test the ability</span>
<br><span id="ts6">to upload an M P</span>
<br><span id="ts9">3 file.</span>
<br><span id="ts10">Hopefully this will work!</span>
</pre>
</div>