www.webdeveloper.com
Results 1 to 4 of 4

Thread: Close video with javascript

  1. #1
    Join Date
    Nov 2012
    Posts
    2

    Close video with javascript

    Hello, I'm Rafael, I live in Brazil and I have a big doubt JS.
    I'm new to JS and my problem is:
    I'm doing a page containing 3 images, clicking on an image a YOUTUBE VIDEO OF LOADED is, if you click on another image in the video running to be closed and the new video uploaded.

    The solution:
    oa declare each ifame div as display: none;,
    when the User clicks on the image a javascript function is triggered by modifying the div display: block;.

    The problem:
    The div appears and hides normally, but the video continues its execution actually the audio video continues running.

    Follow the code test


    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Galeria de video</title>
    <script type="text/javascript">
    function movie_1(id){
    document.getElementById("test_1").style.display='block';
    document.getElementById("test_3").style.display='none';
    document.getElementById("test_2").style.display='none';
    }
    function movie_2(id){
    document.getElementById("test_2").style.display='block';
    document.getElementById("test_1").style.display='none';
    document.getElementById("test_3").style.display='none';
    }
    function movie_3(id){
    document.getElementById("test_3").style.display='block';
    document.getElementById("test_1").style.display='none';
    document.getElementById("test_2").style.display='none';
    }
    </script>

    <style type="text/css">
    #test_1, #test_2, #test_3{
    display: none;}
    #movies{
    width:500px;
    height:300px;
    margin-left:auto;
    margin-right:auto;}
    #conteudo{
    width:900px;
    height:700px;
    margin-left:auto;
    margin-right:auto;}
    </style>
    </head>

    <body>
    <div id="conteudo">

    <div id="movies">

    <a href="javascript: ;" onclick="movie_1(test_1);">Video 1</a>
    <a href="javascript: ;" onclick="movie_2(test_2);">Video 2</a>
    <a href="javascript: ;" onclick="movie_3(test_3);">Video 3</a>


    <div id="test_1"><iframe width="420" height="315" src="http://www.youtube.com/embed/NK0U5lOP_-k" frameborder="0" allowfullscreen></iframe></div>


    <div id="test_2"><iframe width="420" height="315" src="http://www.youtube.com/embed/LQuA7jjeNKw" frameborder="0" allowfullscreen></iframe></div>
    <div id="test_3"><iframe width="420" height="315" src="http://www.youtube.com/embed/T8RzkST12bs" frameborder="0" allowfullscreen></iframe></div>

    </div>
    </div>


    </body>
    </html>

  2. #2
    Join Date
    Jan 2005
    Posts
    366
    Rather than showing/hiding the iframes, why not try having a single iframe, and dynamically setting the
    Code:
    SRC
    of the iframe (just like old-fashioned image replacement)?

  3. #3
    Join Date
    Nov 2012
    Posts
    2
    Quote Originally Posted by omnicity View Post
    Rather than showing/hiding the iframes, why not try having a single iframe, and dynamically setting the
    Code:
    SRC
    of the iframe (just like old-fashioned image replacement)?
    OmniCity,
    I'm not understanding right,
    I am new to programming and web language, have knowledge of university:
    Javascript, Html, CSS, C, Vb.net and Php.
    Encounter many difficulties to find references, and, to tell the truth my english is bad, my native language is Portuguese, and if not for the translater I was dead.
    You could be a little more espessifico? Explaining with examples.
    thank you

  4. #4
    Join Date
    Jan 2005
    Posts
    366
    Instead of having three iframes, have just one, but give it an ID:

    HTML Code:
    <div id="test_1"><iframe id="vidFrame" width="420" height="315" src="http://www.youtube.com/embed/NK0U5lOP_-k" frameborder="0" allowfullscreen></iframe></div>
    Then something along the lines of:

    Code:
    <a href="javascript:void;" onclick="showMovie(0);">Video 1</a>
    <a href="javascript:void;" onclick="showMovie(1);">Video 2</a>
    <a href="javascript:void;" onclick="showMovie(2);">Video 3</a>
    
    var movieList = ["http://www.youtube.com/embed/NK0U5lOP_-k",  
                           "http://www.youtube.com/embed/NK0U6lOP_-k",
                           "http://www.youtube.com/embed/NK0U7lOP_-k"];
    
    function showMovie(movieID)
    {
      var thisFrame = document.getElementById("vidFrame");
      thisFrame.src = movieList[movieID];
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles