I wrote this code based off of Felix Kling's response to click toggle. I was wondering if there was a better way of writing it, and how to close an element when another is clicked.
I am using this to play certain points of the video on click and close them on a secondary click.
I have wrote a function to close on escape with classes, but it slows down and breaks the event from completing.
$(document).on('keydown', function(e) {
if(e.keyCode == 27) {
var buttons = $("button");
if ( buttons.hasClass("open") ) {
myVideo.currentTime = 2.8;
myVideo.play();
setTimeout(function() {
myVideo.pause();
}, 540);
buttons.removeClass("open");
}
}
})
var myVideo = document.getElementById("video");
$("#button-1").clickToggle(function() {
myVideo.currentTime = 2;
myVideo.play();
$(this).addClass("open");
setTimeout(function() {
myVideo.pause();
}, 900);
}, function() {
myVideo.currentTime = 2.8;
myVideo.play();
setTimeout(function() {
myVideo.pause();
}, 540);
});
$("#button-2").clickToggle(function() {
myVideo.currentTime = 3.05;
myVideo.play();
setTimeout(function() {
myVideo.pause();
}, 850);
}, function() {
myVideo.currentTime = 3.8;
myVideo.play();
setTimeout(function() {
myVideo.pause();
}, 680);
});