Last active
September 12, 2015 01:36
-
-
Save aulizko/f81d3cd8109eb713d0ba to your computer and use it in GitHub Desktop.
Внизу тэга head есть <script>, в котором старая функция play(). Вот вместо нее надо вставить это.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Убираем старый обработчик нажатий | |
var forEach = Array.prototype.forEach; | |
forEach.call(document.getElementsByClassName('glyphicon-volume-up'), | |
removeAllEventListeners); | |
function play(e) { | |
if (!e) { | |
return; | |
} | |
var target = e.target; | |
var audio = iterateChildren(target.firstChild, isAudio); | |
if (!audio) { | |
return; | |
} | |
addEvent(audio, 'ended', function () { | |
target.style.backgroundColor = 'white'; | |
target.style.color = '#333333'; | |
audio.currentTime = 0; | |
}); | |
if (audio.paused) { | |
audio.play(); | |
target.style.backgroundColor = '#333333'; | |
target.style.color = 'white'; | |
} else { | |
audio.pause(); | |
target.style.backgroundColor = 'white'; | |
target.style.color = '#333333'; | |
} | |
} | |
function removeAllEventListeners(el) { | |
var elClone = el.cloneNode(true); | |
el.parentNode.replaceChild(elClone, el); | |
elClone.style.cursor = 'pointer'; | |
elClone.style.padding = '0.2em'; | |
elClone.style.marginLeft = '-.3em'; | |
elClone.style.borderRadius = '50%'; | |
addEvent(elClone, 'click', play); | |
} | |
// add event cross browser | |
function addEvent(elem, event, fn) { | |
if (elem.addEventListener) { | |
elem.addEventListener(event, fn, false); | |
} else { | |
elem.attachEvent("on" + event, function () { | |
// set the this pointer same as addEventListener when fn is called | |
return (fn.call(elem, window.event)); | |
}); | |
} | |
} | |
function iterateChildren(el, fn) { | |
while (el) { | |
if (fn(el)) return el; | |
el = el.nextSibling; | |
} | |
} | |
function predicate(el, tagName) { | |
return el.tagName && el.tagName.toLowerCase() === tagName; | |
} | |
function isAudio(el) { | |
return predicate(el, 'audio'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment