JQuery Slider Help - Mouse-Over event solution - javascript

So, what I am seeking is simple in context, but challenging to implement.
I want the hover function to be disabled until the user clicks the play button and then the pause button.
So, basically I would like the mouse-over function of the slider to be void until the user clicks the Un-Mute Button (Play Youtube Button) and then clicks the Mute Button (Pause YT Button). If you go to the JSFiddle you will see that, upon load, if your mouse hovers over the slider and then hovers out ... the slider moves (After 6 sec). I do not want the slider to start until the user starts the Youtube video.
var W = $('#image_rotator').width(),
N = $('#slider .slide').length,
C = 0,
S = 0,
intv;
if (N <= 1) $('#left, #right').hide();
$('#slider').width(W * N);
$('#left, #right').click(function () {
C = (this.id == 'right' ? ++C : --C) < 0 ? N - 1 : C % N;
$('#slider').stop().animate({
left: -C * W
}, 300, function () {
if (C == 0) {
$('#playiunmute').show();
}
if (C == 1) {
$('#left').show();
}
if (C == 1) {
$('#right').show();
}
if (C == 1) {
$('#playiunmute').hide();
}
if (C == 10) {
$('#slider').css("left", "0");
$('#right').click();
}
});
});
function setResetInterval(e) {
var intv = $("#slider");
if (e) {
timer = setInterval(function () {
$('#right').click();
}, 6000);
} else {
clearInterval(timer);
}
}
$("#imute").click(function (e) {
e.preventDefault();
setResetInterval(true);
S = 0;
});
$("#iunmute").click(function (e) {
e.preventDefault();
setResetInterval(false);
S = 1;
});
$("#playiunmute").click(function (e) {
e.preventDefault();
setResetInterval(false);
S = 1;
});
$('#slider, #left, #right, #playiunmute').hover(function (e) {
return (e.type == 'mouseleave' && S != 1) ? setResetInterval(true) : setResetInterval(false);
});
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '460',
width: '1190',
controls: 1,
videoId: 'kDUERmuuogo',
playerVars: {
controls: 0,
showinfo: 0,
modestbranding: 1,
wmode: "opaque",
rel: 0
},
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
player.mute();
event.target.playVideo();
event.target.setPlaybackQuality("hd720");
}
document.getElementById('iunmute').onclick = function (e) {
e.preventDefault();
(player.isMuted()) ? player.unMute() : player.mute();
player.playVideo();
player.seekTo(0);
};
document.getElementById('imute').onclick = function (e) {
player.mute();
player.pauseVideo();
};
document.getElementById('playiunmute').onclick = function (e) {
e.preventDefault();
(player.isMuted()) ? player.unMute() : player.mute();
player.playVideo();
};
$('#iunmute').click(function () {
$('#imute').show();
});
$('#iunmute').click(function () {
$('#iunmute').hide();
});
$('#imute').click(function () {
$('#playiunmute').show();
});
$('#imute').click(function () {
$('#imute').hide();
});
$('#playiunmute').click(function () {
$('#imute').show();
});
$('#playiunmute').click(function () {
$('#playiunmute').hide();
});
$('#iunmute').click(function () {
$('#left').hide();
});
$('#iunmute').click(function () {
$('#right').hide();
});
$('#playiunmute').click(function () {
$('#left').hide();
});
$('#playiunmute').click(function () {
$('#right').hide();
});

I had a feeling your initial question (Trigger Alert When Slider Moves Left A Certain Amount) was going to bug me until I figured out how to make this work, and I was right! I sat down and figured out a sort of compromise-ish solution. In order to get it to work, I had eliminate the pause button that you added in favor of the normal YouTube player controls instead, because I could attach listeners to them, which resulted in shorter and more-comprehensible code. I tested this pretty thoroughly, and it should accomplish precisely what you were looking for: When the video is paused (or muted; I wasn't sure if you wanted this functionality as well since, although you compared your mute button to YouTube's play/pause buttons in the question, it was still a mute button.), the video will stop and the slider will advance to the next slide. When the interval (or the user manually advancing the slides) eventually causes the slider to return to the video, the video will start again from the beginning, and the interval will be cancelled, so that the only way that the player will advance past the video will be for the user to mute the video. If you don't want the video to start again from the beginning, you can remove the line player.seekTo(0); in the $("#left, #right").click() handler. If you don't want the mute button to cause the slide to change, remove the following interval function:
window.setInterval(function(){
if(player.isMuted()) {
player.pauseVideo();
}}, 100);
As well as both player.unMute(); lines in the $("#left, #right").click() handler.
Below is a portion of the code that I modified/added. I added only this portion in order to prevent my answer from becoming any longer than it had to be (it's already on the longer side as far as answers go). The full code is visible in the Fiddle.
function onPlayerReady() {
player.playVideo();
player.unMute();
player.setPlaybackQuality("hd720");
window.setInterval(function(){
if(player.isMuted()) {
player.pauseVideo();
}}, 100);
}
function onPlayerStateChange() {
if (player.getPlayerState() == 2) {
$("#right").click();
}
}
I hope this is what you're looking for!
http://jsfiddle.net/IronFlare/6o6kL8pf/2/
If you have any questions, feel free to add a comment to this answer.

Related

Unable to pause audio after eventListener end

When first loading the code, the pause button works fine, it pauses the audio and changes the HTML class of the button inside, the button continues to work after using the previous/next buttons to go to another song, however after the eventListener is triggered the pause button stops working, it doesn't pause the songs but the HTML inside still changes.
Edit: After a little bit of further debugging I found that the pause button is only able to pause the first time a track is loaded, once the audio.addEventListener('ended', nextTrack); is triggered it is unable to pause/play any audio.
let audio = new Audio;
let playing = false;
let playpause = document.getElementById('play-pause');
let root = document.documentElement;
let songname = document.getElementById('name');
let next = document.getElementById('next');
let prev = document.getElementById('previous');
let index = 0;
songlist = [
{"name":"Love Again"
,"artist":"The Kid LAROI",
"path":"resources/music/love-again.mp3",
},
{
"name":"Always Do",
"artist":"The Kid LAROI",
"path":"resources/music/always-do.mp3",
},
{
"name":"Bye Bye",
"artist":"Juice WRLD",
"path":"resources/music/bye-bye.mp3",
},
{
"name":"Understand",
"artist":"BoyWithUke",
"path":"resources/music/understand.mp3",
}
]
function progress_animation(){
var currentTime = audio.currentTime;
var duration = audio.duration;
$('#progbar').stop(true, true).animate({ 'width': (currentTime + .25) / duration * 100 + '%' }, 250, 'linear');
window.requestAnimationFrame(progress_animation);
};
function load(index){
songname.textContent = `${songlist[index].artist} - ${songlist[index].name}`;
audio.src = songlist[index].path;
audio.load()
};
audio.addEventListener('ended', nextTrack);
$('#play-pause').click(function (){
if (!playing) {
Play()
playing = true
} else {
Pause()
playing = false
}
});
function nextTrack(){
if (index < songlist.length - 1) {
index++;
} else {
index = 0;
}
load(index);
};
function prevTrack(){
if (index > 0) {
index--;
} else {
index = songlist.length - 1;
}
load(index);
};
function Play() {
audio.play();
playing = true;
playpause.innerHTML = '<i class="fa-solid fa-pause"></i>';
};
function Pause() {
audio.pause()
playing = false;
playpause.innerHTML = '<i class="fa-solid fa-play"></i>';
};
The pause button still changes its HTML class and sets the "playing" variable to false/true depending on the state of "playing" but doesn't actually pause the song

Vanilla JS image slideshow play/pause button

My play/pause button works the first time I press pause and the first time I press play. But after that if I want to pause the slideshow again, it just plays the slideshow at a faster speed and I am no longer able to pause it.
I am thinking maybe it is to do with the fact I have two separate functions: one for the play/pause button icon toggle, and another for the actual play pause behaviour? (update: this has now been fixed, but still doesn't work)
Sorry, I am struggling with javascript, I have a lot to learn.
My script:
const playPause = document.querySelector('.pause');
let slideId;
// FUNCTION TO MOVE TO NEXT SLIDE
const moveToNextSlide = () => {
slides = getSlides();
if (index >= slides.length - 1) return;
index++;
slideGroup.style.transform = `translateX(${-slideWidth * index}px)`;
slideGroup.style.transition = '.8s';
}
// FUNCTION TO START SLIDESHOW
const startSlide = () => {
slideId = setInterval(() => {
moveToNextSlide();
}, interval);
playing = true;
};
// START AUTOMATIC SLIDESHOW UPON ENTERING THE PAGE
startSlide();
//PLAY PAUSE BUTTON - slideshow start/stop
playPause.addEventListener('click', () => {
if(!slideId) {
slideId = startSlide();
console.log('started');
} else {
clearInterval(slideId);
slideId = null;
console.log('stopped');
}
});
//PLAY PAUSE BUTTON - image change
function toggle(button) {
if (button.className != 'pause') {
button.src = 'img/pause.png';
button.className = 'pause';
}
else if (button.className == 'pause') {
button.src = 'img/play.png';
button.className = 'play';
}
return false;
}
HTML:
<input type='image' src='img/pause.png' class='pause' onclick='toggle(this);' />
This is what the console looks like when I try to pause the slideshow for a second time:
There are some details in your code that are missing and would be helpful to have, but I guess that you can get rid of the onclick handler and attach two event listeners:
let slideId;
// FUNCTION TO START SLIDESHOW
const startSlide = () => {
let interval = 2000; // using just as sample
playing = true;
return setInterval(() => {
console.log('moveToNextSlide();') // replaced just to test without missing code
}, interval);
};
//PLAY PAUSE BUTTON - slideshow start/stop
playPause.addEventListener('click', () => {
if(!slideId) {
slideId = startSlide();
console.log('started');
} else {
clearInterval(slideId);
slideId = null;
console.log('stopped');
}
});
//PLAY PAUSE BUTTON - image change
playPause.addEventListener('click', function toggle() { // the arrow function would not work in this case
var button = this;
if (button.className != 'pause') {
button.src = 'img/pause.png';
button.className = 'pause';
}
else if (button.className == 'pause') {
button.src = 'img/play.png';
button.className = 'play';
}
return false;
});

Resetting variables is not resetting game?

I am creating a Simon game and it works on round one, but once your each round two it immediately says incorrect.
jsfiddle
I reset all variables between the rounds, but it still seems to save them as it instantly says "Game Over!"
JS:
function makeGuess() {
...
var checkForComplete = setInterval(function () {
if (yourAnswer.length >= round) {
clearInterval(checkForComplete);
yourString = yourAnswer.toString();
correctString = correctAnswer.toString();
if (yourString === correctString) {
alert('Correct');
round++;
yourAnswer = [];
correctAnswer = [];
yourString = "";
correctString = "";
playSimon();
}
else {
alert("Game Over!");
}
}
}, 500)
Here is the playSimon() function:
function playSimon() {
setTimeout(function () {
color = Math.floor(Math.random() * 4);
if (color == 0) {
correctAnswer.push(0);
$('#redButton').addClass('lightOn');
setTimeout(function () {
$('#redButton').removeClass('lightOn');
}, 500);
}
else if (color == 1) {
correctAnswer.push(1);
$('#blueButton').addClass('lightOn');
setTimeout(function () {
$('#blueButton').removeClass('lightOn');
}, 500);
}
else if (color == 2) {
correctAnswer.push(2);
$('#greenButton').addClass('lightOn');
setTimeout(function () {
$('#greenButton').removeClass('lightOn');
}, 500);
}
else if (color == 3) {
correctAnswer.push(3);
$('#yellowButton').addClass('lightOn');
setTimeout(function () {
$('#yellowButton').removeClass('lightOn');
}, 500);
}
i++;
if (i <= round) {
playSimon();
}
else {
makeGuess();
}
}, 700);
}
Why is it alerting Game Over instantly on round 2?
Because you are binding the click events multiple times:
function makeGuess() {
$('#redButton').click(function() {
yourAnswer.push(0);
});
$('#blueButton').click(function() {
yourAnswer.push(1);
});
$('#greenButton').click(function() {
yourAnswer.push(2);
});
$('#yellowButton').click(function() {
yourAnswer.push(3);
});
...
}
Each time makeGuess() is called, the buttons are bound yet another click event listener. First time it's done (first level), there's only 1 event listener so it's working correctly, but from the second level onward each buttons are bound with 2 identical click listener, and when the player click a button, the function is called twice => yourAnswer is pushed twice immediately, which result in an incorrect answer => game over.
A quick fix is to unbind all click events on the 4 buttons before binding them all again, optimally immediately before the next playSimon() is called.
if (yourString === correctString) {
alert('Correct');
round++;
yourAnswer = [];
correctAnswer = [];
yourString = "";
correctString = "";
$('#redButton').unbind( "click" );
$('#blueButton').unbind( "click" );
$('#greenButton').unbind( "click" );
$('#yellowButton').unbind( "click" );
playSimon();
} else {
alert("Game Over!");
}
JsFiddle: https://jsfiddle.net/dfk2am7e/1/
AVAVT is right, however instead of unbinding, rather just bind the event once off.
I have updated your JSFiddle and optimized your code a little bit.
https://jsfiddle.net/dfk2am7e/3/
// Run this when the page has loaded to ensure HTML is there.
$(function(){
$('#redButton').click(function() {
yourAnswer.push(0);
});
$('#blueButton').click(function() {
yourAnswer.push(1);
});
$('#greenButton').click(function() {
yourAnswer.push(2);
});
$('#yellowButton').click(function() {
yourAnswer.push(3);
});
});

Autostart jQuery slider

I'm using a script that animates on click left or right to the next div. It currently works fine but I'm looking to add two features to it. I need it to repeat back to the first slide if it is clicked passed the last slide and go to the last slide if click back from the first slide. Also, I'm interested in getting this to autostart on page load.
I've tried wrapping the clicks in a function and setting a setTimeout but it didn't seem to work. The animation is currently using CSS.
Here's the current JS:
<script>
jQuery(document).ready(function() {
var boxes = jQuery(".box").get(),
current = 0;
jQuery('.right').click(function () {
if (current == (-boxes.length + 1)){
} else {
current--;
updateBoxes();
}
console.log(-boxes.length + 1);
console.log(current);
});
jQuery('.left').click(function () {
if (current === 0){
} else{
current++;
updateBoxes();
}
});
function updateBoxes() {
for (var i = current; i < (boxes.length + current); i++) {
boxes[i - current].style.left = (i * 100 + 50) + "%";
}
}
});
</script>
Let me know if I need a jsfiddle for a better representation. So far, I think the code is pretty straightforward to animate on click.
Thanks.
Try
jQuery(document).ready(function () {
var boxes = jQuery(".box").get(),
current = 0,
timer;
jQuery('.right').click(function () {
if (current == (-boxes.length + 1)) {
current = 0;
} else {
current--;
}
updateBoxes();
}).click(); //initialize the view
jQuery('.left').click(function () {
if (current === 0) {
current = -boxes.length + 1;
} else {
current++;
}
updateBoxes();
});
function updateBoxes() {
//custom implementation for testing
console.log('show', current)
$(boxes).hide().eq(-current).show();
autoPlay();
}
function autoPlay() {
clearTimeout(timer);
//auto play
timer = setTimeout(function () {
jQuery('.right').click();
}, 2500)
}
});
Demo: Fiddle
Here's an example based on my comment (mostly pseudocode):
$(function(){
var boxes = $('.box'),
current = 0,
timer;
// Handler responsible for animation, either from clicking or Interval
function animation(direction){
if (direction === 1) {
// Set animation properties to animate forward
} else {
// Set animation properties to animate backwards
}
if (current === 0 || current === boxes.length) {
// Adjust for first/last
}
// Handle animation here
}
// Sets/Clears interval
// Useful if you want to reset the timer when a user clicks forward/back (or "pause")
function setAutoSlider(set, duration) {
var dur = duration || 2000;
if (set === 1) {
timer = setInterval(function(){
animation(1);
}, dur);
} else {
clearInterval(timer)
}
}
// Bind click events on arrows
// We use jQuery's event binding to pass the data 0 or 1 to our handler
$('.right').on('click', 1, function(e){animation(e.data)});
$('.left').on('click', 0, function(e){animation(e.data)});
// Kick off animated slider
setAutoSlider(1, 2000);
Have fun! If you have any questions, feel free to ask!

Anything Slider not playing video on second loop in IE9

I am using Anything slider in infinite slides with an HTML 5 video in one slide. The goal is to have the video run each time the slide loops. the problem is in IE9 it seems to break the slider on the second loop. The video does not play nor does it advance to the next slide, it just freezes.
onSlideInit: function (e, slider) {
var vid = slider.$lastPage.find('video');
if (vid.length && typeof (vid[0].pause) !== 'undefined') {
vid[0].pause();
}
},
onSlideBegin: function (e, slider) { },
onSlideComplete: function (slider) {
// make current slide conform to user define data-duration value
var video = slider.$lastPage.find('video');
console.log(video)
if (video.length > 0) {
video[0].currentTime = 0;
}
if (hasVideoSupport == "false") {
jwplayer("container").setup({
file: video,
flashplayer: "http://player.longtailvideo.com/player.swf",
height: 400,
width: 500
});
jwplayer().play()
slider.startStop(false);
onComplete(function () { slider.startStop(true) })
} else {
var vid = slider.$currentPage.find('video');
if (vid.length > 0) {
console.log(vid);
vid[0].play();
}
}
},
Thanks for the help

Categories