Toggle visibility property of div

JavascriptJqueryCssToggleShow Hide

Javascript Problem Overview


I have an HTML 5 video in a div. I then have a custom play button - that works fine.
And I have the video's visibility set to hidden on load and visible when the play button is clicked, how do I return it to hidden when the play button is clicked again?

function showVid() {
  document.getElementById('video-over').style.visibility = 'visible';
}

#video-over {
  visibility: hidden;
  background-color: rgba(0, 0, 0, .7)
}

<div id="video-over">
  <video class="home-banner" id="video" controls="">
    <source src="http://video-js.zencoder.com/oceans-clip.mp4" type='video/mp4' />
    <source src="http://video-js.zencoder.com/oceans-clip.webm" type='video/webm' />
    <source src="http://video-js.zencoder.com/oceans-clip.ogv" type='video/ogg' />
    </video>
</div>

<button type="button" id="play-pause" onclick="showVid();">
      <img class="img-home-apply" src="/wp-content/images/apply-pic.png" alt="Apply Now">
      </button>

I'm basically just trying to toggle it between the two states of visible and hidden except I can't use toggle because that show's and hides the div. I need it there, just hidden, so it maintains the correct height.

Javascript Solutions


Solution 1 - Javascript

Using jQuery:

$('#play-pause').click(function(){
  if ( $('#video-over').css('visibility') == 'hidden' )
    $('#video-over').css('visibility','visible');
  else
    $('#video-over').css('visibility','hidden');
});

Solution 2 - Javascript

According to the jQuery docs, calling toggle() without parameters will toggle visibility.

$('#play-pause').click(function(){
   $('#video-over').toggle();
});

http://jsfiddle.net/Q47ya/

Solution 3 - Javascript

There is another way of doing this with just JavaScript. All you have to do is toggle the visibility based on the current state of the DIV's visibility in CSS.

Example:

function toggleVideo() {
     var e = document.getElementById('video-over');

     if(e.style.visibility == 'visible') {
          e.style.visibility = 'hidden';
     } else if(e.style.visibility == 'hidden') {
          e.style.visibility = 'visible';
     }
}

Solution 4 - Javascript

To clean this up a little bit and maintain a single line of code (like you would with a toggle()), you can use a ternary operator so your code winds up looking like this (also using jQuery):

$('#video-over').css('visibility', $('#video-over').css('visibility') == 'hidden' ? 'visible' : 'hidden');

Solution 5 - Javascript

To do it with an effect like with $.fadeIn() and $.fadeOut() you can use transitions

.visible {
  visibility: visible;
  opacity: 1;
  transition: opacity 1s linear;
}
.hidden {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 1s, opacity 1s linear;
}

Solution 6 - Javascript

It's better if you check visibility like this: if($('#video-over').is(':visible'))

Solution 7 - Javascript

$.fn.toggleVisibility = function (state) {
	return this.each(function () {
		$(this).css("visibility", state ? "visible" :
			(state === false ? "hidden" :
				$(this).css("visibility") === "hidden" ? "visible" : "hidden"));
	});
};

Then

$('#video-over').toggleVisibility();

or

$('#video-over').toggleVisibility(true);

or

$('#video-over').toggleVisibility(false);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontfer77View Question on Stackoverflow
Solution 1 - Javascripttb11View Answer on Stackoverflow
Solution 2 - JavascripttimetravelerView Answer on Stackoverflow
Solution 3 - JavascriptFatAussieFatBoyView Answer on Stackoverflow
Solution 4 - JavascriptelPastorView Answer on Stackoverflow
Solution 5 - JavascriptdavefrassoniView Answer on Stackoverflow
Solution 6 - JavascriptPaul IştoanView Answer on Stackoverflow
Solution 7 - Javascriptholden321View Answer on Stackoverflow