If necessary I can explain in more detail but essentially what I need to do is effect CSS changes to HTML text in sync with an audio track - i.e., highlighting words/phrases in sync with the audio playback. I also need to control the audio playback by clicking on the text. I have good HTML/CSS chops, but I’m not as strong with raw js, so I’m hoping there’s a jQuery approach. I’m hoping someone can steer me in the best direction.
Many thanks in advance,
svs
For ease of use, I recommend a combination of jQuery and Popcorn.js for anything where you want to integrate media with HTML, and visa versa. See this jsfiddle post for an example.
For the record, should jsfiddle ever disappear, here's the code:
HTML
<audio id="greeting" src="https://dl.dropboxusercontent.com/u/17154625/greeting.ogg" controls></audio>
<div id="text">
<span id="w1" class="word" data-start="1.0">Hello</span>,
and <span id="w2" class="word" data-start="2.0">welcome</span>
to Stack <span id="w3" class="word" data-start="3.0">Overflow</span>.
Thank you for asking your question.
</div>
CSS
.word {
color: red;
}
.word:hover, .word.selected {
color: blue;
cursor: pointer;
}
JS
var pop = Popcorn("#greeting");
var wordTimes = {
"w1": { start: 1, end: 1.5 },
"w2": { start: 1.9, end: 2.5 },
"w3": { start: 3, end: 4 }
};
$.each(wordTimes, function(id, time) {
pop.footnote({
start: time.start,
end: time.end,
text: '',
target: id,
effect: "applyclass",
applyclass: "selected"
});
});
pop.play();
$('.word').click(function() {
var audio = $('#greeting');
audio[0].currentTime = parseFloat($(this).data('start'), 10);
audio[0].play();
});
Related
I am wondering whether there is any way to overcome the new autoplay policy by Google.
I want to play a short sound snippet when a link is hovered, which unfortunately just works in Firefox and not in Chrome and Safari anymore.
Is there any way to find a work around for that problem?
Probably not I guess, just thought to address this question to more educated people in that field. Maybe someone has an idea.
That's the Code which works in Firefox and used to work in Chrome and Safari as well - but not anymore.
html
<span class="hit hitme">Just hit me up!</span>
<audio id="HitMe">
<source src="sound/hitmeup.mp3">
</audio>
jQuery
var audio = $("#HitMe")[0];
$(".hitme").mouseenter(function() {
audio.play()
$(".hitme").mouseleave(function() {
audio.pause();
});
});
Your question is short but there are actually many things to be said.
First off, it's always nice to use VanillaJS™ instead of jQuery when it comes to policy changes, because standards get immediatly propagated to plain JavaScript, whereas it takes a while for the change to propagate up to third-party libs like jQuery. The nice thing with plain JavaScript is that you can create an audio object with new Audio(<source>) - no need for any HTML element! See below for an example:
const audio = new Audio("https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3");
// wait for the DOM to load
window.onload = () => {
// play audio on click
const clickToPlay = document.querySelector('#click-to-play');
clickToPlay.onclick = () => audio.play();
// play/pause audio on hover
const hoverToPlay = document.querySelector('#hover-to-play');
hoverToPlay.onmouseover = () => audio.play();
hoverToPlay.onmouseout = () => audio.pause();
}
/* just some styling, not useful for the solution */
#click-to-play {
padding: 1em;
background-color: steelblue;
}
#click-to-play:hover {
cursor: pointer;
}
#hover-to-play {
padding: 1em;
background-color: lightblue;
}
#hover-to-play:hover {
cursor: crosshair;
}
<div id="click-to-play">
Click to play
</div>
<div id="hover-to-play">
Hover in to play, hover out to pause
</div>
Great! Except, as you precised, the autoplay on hover that might be blocked by the 2017 update on autoplay in Chrome.
But it's not necessarily a bad thing. This update was made to make the web user experience better. If you're trying to find hacks on how to bypass it, you're doing it wrong ;) The update states that autoplay with sound is allowed if the user has interacted (eg with a click). Therefore, when designing your website, make sure the user clicks somewhere on your page before the autoplay appears. Here's an example with a two-steps click to authorize, hover to play user experience:
const audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3');
window.onload = () => {
const clickToAuthorize = document.querySelector('#click-to-authorize');
const hoverToPlay = document.querySelector('#hover-to-play');
clickToAuthorize.onclick = () => {
hoverToPlay.style.display = 'block';
}
hoverToPlay.onmouseover = () => audio.play();
hoverToPlay.onmouseout = () => audio.pause();
}
#click-to-authorize {
padding: 1em;
background-color: steelblue;
}
#click-to-authorize:hover {
cursor: pointer;
}
#hover-to-play {
padding: 1em;
background-color: lightblue;
display: none;
}
#hover-to-play:hover {
cursor: crosshair;
}
<div id="click-to-authorize">
Click if you want to hear a T-Rex roar!
</div>
<div id="hover-to-play">
Hover to play/pause
</div>
I am working with a Web Form (html) and a CSS file and I wanna know what do I need to write in the CSS to make an action on one class or id- affect an other class or id. For example: I have a
<p class="hh">
Hello!
</p>
(^^ this p tag's class is "hh")
And another one:
<p class="gb">
Goodbye!
</p>
(^^ this p tag's class is "gb")
I wanna write something in the CSS file so that whenever I click on whatever there is in the "hh" class, it will make something change in the "gb" class, so if I click on the text "Hello!" it will make the color of the text "Goodbye!" green. Please help me! I try to find out how to do it for a long long time...
Thank you!
This sounds more like you need a javascript solution. In general you are not really able to change something on a click event in CSS. Consider following solution:
const hh = document.getElementById("hh");
const gb = document.getElementById("gb");
hh.addEventListener("click", function() {
gb.style.color = "green";
});
gb.addEventListener("click", function() {
hh.style.color = "red";
});
<div id="hh">
Hello!
</div>
<div id="gb">
Goodbye!
</div>
A common practice for doing this is by using JavaScript, which is known as the programming language of the web. If you've never used JavaScript before it can be a little bit confusing but if you have experience in other general purpose programming languages such as Python or Java then it shouldn't take much time to pick up.
To do what you are asking, there are a few possible ways to do this. I will share what I believe to be the most simple although not the most robust. You can use JavaScript events to fire off certain functions when certain particular things happen to your elements. For example, you can modify your HTML like so:
<p class="hh" onclick="doSomething()">Hello!</p>
Then, either in a separate JavaScript file linked back to your html file or in the of your html file, you would define the doSomething() function:
function doSomething(){
document.getElementsByClassName("gb")...
}
The document.getElementsByClassName() function is one way to select HTML elements from a page and modify it via JavaScript, I suggest checking out the very good JavaScript tutorials on W3Schools for more and better ways to do this, but this is the general principal. You would then modify the HTML element any way you need to.
Hope this helps!
You need to do that using JavaScript. I have attached a example for that.
$(".one").on('click', function() {
$(".two").css('color', 'red');
})
.one{
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="one"> Change below text to RED </p>
<p class="two"> Black text </p>
There is a way to use a :focus state to change the look of parents, but it wouldn't be possible to differentiate between which click caused the parent to focus.
Here's a simple example using JavaScript and jQuery.
var helloEls = document.querySelectorAll('#jsTest .hh');
var goodbyeEls = document.querySelectorAll('#jsTest .gb');
helloEls.forEach(function(elem) {
elem.addEventListener("click", function() {
goodbyeEls.forEach(function(el) {
if (el.className==='gb active'){
el.className = 'gb';
} else {
el.className = 'gb active';
}
});
});
});
var gbEls = $('#jqueryTest .gb');
$('#jqueryTest .hh').click(function(){
if (gbEls.hasClass('active')){
gbEls.removeClass('active');
} else {
gbEls.addClass('active');
}
});
.gb.active {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="jsTest">
<p class="hh">
Hello!
</p>
<p class="gb">
Goodbye!
</p>
</div>
<div id="jqueryTest">
<p class="hh">
Hello!
</p>
<p class="gb">
Goodbye!
</p>
</div>
I am using this code:
{ box-sizing: border-box; }
.specific:hover { background-image: url("example.jpg"); color:white }
With this code I am using this for html:
div class="specific"
With these I can easily hover over a paragraph and the background image appears instantly. But I want a little bit more like when I will hover, it should play a sound (namely mp3 file) and when I will move away the cursor the sound will stop playing and when I will hover my cursor again the sound should play from the first not from where I left it.
I have seen a lot of similar posts but the code there, I cannot put them into my file.
You can do this very easily with an HTML audio tag with the built in JS methods, properties etc. See this page. Many people don't like W3Schools much but there's good information there and it's very straight forward. It would take a very small amount of JS/jQuery utilizing a hover event listener with the .play() and .pause() methods to make this happen.
Here's a simple tutorial using .play() and .pause().
I laboriously put together a fiddle for you. But seriously, try your own stuff.
HTML
<div class="specific">
Listen to some <i>music</i>...
</div>
JS
let specific = document.querySelector(".specific");
let audio = document.createElement("audio");
audio.src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
document.body.appendChild(audio);
specific.onmouseover = () => {
audio.play();
}
specific.onmouseout = () => {
audio.pause();
}
You're welcome.
UPDATE
Updated fiddle: https://jsfiddle.net/dLxkhogy/21/
HTML
<div class="specific">
<p>
Listen to some <i>music</i>...
</p>
</div>
<div class="specific">
<p>
Listen to some more music...
</p>
</div>
<div class="specific">
<p>
Listen to even <i>more</i> music!
</p>
</div>
JS
let specifics = document.querySelectorAll(".specific");
let audios = ["https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3"];
for(i = 0; i < specifics.length; i++){
let specific = specifics[i];
let audio = document.createElement("audio");
audio.src = audios[i];
document.body.appendChild(audio);
specific.onmouseover = () => {
audio.play();
}
specific.onmouseout = () => {
audio.pause();
audio.currentTime = 0;
}
}
One of my websites went online a few weeks back. The design included an embed tweet within a div which displayed properly at that time.
However while checking today, I noticed the embed tweet has moved to the right in Mozilla. Now what's puzzling me is that I didn't even touch the code, so has twitter or Mozilla updated one of their properties???
heres a link to my site.
OK the problem is solved finally managed overwriting the css created by twitter.
I simply used the following scc selector
"div_containing_twitter_script" iframe {properties}
So if I placed the code in a div with "twitter" as an id the code would be
#twitter iframe {property:value}
Nevertheless thanks a lot for trying to help me out...
If Twitter feeds are not showing you can use this following code.
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> <p></p> <script charset="utf-8" src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
version: 2,
type: 'profile',
rpp: 5,
interval: 1000,
width: 180,
height: 200,
theme: {
shell: {
background: '#88d4ff',
color: '#ffffff'
},
tweets: {
background: '#ffffff',
color: '#333333',
links: '#00aeef'
}
},
features: {
scrollbar: false,
loop: true,
live: true,
behavior: 'default'
}
}).render().setUser('#YourUser').start(); /* Need to change User ID*/
</script>
I've set up soundmanager:
soundManager.setup({
And I've created a sound:
soundManager.createSound({
From the console I also have threeSixtyPlayer:
threeSixtyPlayer.init()
threeSixtyPlayer.init(): Found 0 relevant items.
So how do I make the 360player play the sound? And how do I detect that it is finished playing so that I can create and play the next one after that?
The docs lack information regarding 360ui but from what I've tried:
HTML code needed:
<div id="sm2-container"></div>
<div class="ui360 ui360-vis">
<a id="song_link" class="sm2_link" href="asfgasg.mp3"></a>
</div>
JS:
soundManager.setup({
url: 'inc/soundmanager/',
flashVersion: 9,
useFlashBlock: true,
onready: function() {
soundManager.createSound('someSongId_', selectedSong);
},
ontimeout: function() {
// Error msg
}
});
You also need the files (depending on the theme you want):
flashblock.css
360player.css
360player-visualization.css
berniecode-animator.js
360-button-vis-play-light.png
360-button-vis-pause-light.png
After that there should be a div with a "play" image.
To play you can use soundManager.getSoundById('myId') and chain it with the onfinish event. Example at the doc:
soundManager.play('myId',{
onfinish: function() {
alert('The sound '+this.id+' finished playing.');
}
});
Hope it helps...