Results 1 to 8 of 8

Thread: How to Add a Rollover Image to "play" & "mute" for an Audio Tag

  1. #1
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default How to Add a Rollover Image to "play" & "mute" for an Audio Tag

    The following code works well to shut music on and off using two images for HTML5 audio.

    Would you please show me how to add a rollover image to "music-on.png" and another

    rollover image to "music-off.png" in the following script?

    Thanks

    head:
    Code:
    <script>
    $(function() {
        $("#mute").click(function(e) {
            e.preventDefault();
    
            var song = $('audio')[0]
            if (song.paused){
                song.play();
                document.getElementById("mute").src = "images/music-off.png";
            }else{
                song.pause();
                document.getElementById("mute").src = "images/music-on.png";
            }
            });
    });
    </script>
    body:
    Code:
    <audio id="audio" preload="auto" tabindex="0" type="audio/mpeg">
    </audio>
    <div class="style1">
     <a href="#" >
            <img id="mute" src="images/music-off.png" class="style2"/>
        </a>
    </div>
    Last edited by KennyP; 03-24-2016 at 08:18 AM.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    You can do JS mouseover image changes like this;
    Code:
    <img src="img.png" onmouseover="this.src='img-hover.png';" onmouseout="this.src='img.png';"></img>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    KennyP (03-21-2016)

  4. #3
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    Hi Beverley:

    Thank you for your reply. That code does work partially, but when the music is off it displays the

    same image as when the music is on. I'd like for play and stop have their own hover image.

    Test Page.

    Thanks
    Last edited by KennyP; 03-24-2016 at 08:24 AM.

  5. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I would do it differently. Instead of switching the img src, use a span with a background image and then switch class. You would define a different image background for the new class in the CSS and apply a CSS3 transition to achieve the fade effect (IE9+)

    This is crude because I'm stuck on mobile but it should give you some ideas http://jsbin.com/bevufagagi/edit?html,css,js,output
    Last edited by Beverleyh; 03-21-2016 at 06:47 PM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  6. The Following User Says Thank You to Beverleyh For This Useful Post:

    KennyP (03-22-2016)

  7. #5
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    Thanks Beverley, but everything I tried failed.
    Last edited by KennyP; 03-24-2016 at 10:00 PM.

  8. #6
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    I thought I figured it out but it didn't work.
    Last edited by KennyP; 03-23-2016 at 09:57 AM.

  9. #7
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    Would you please show me how to add a rollover image to "images/music-off.png"
    and another rollover image to "images/music-on.png" in the following script:

    Code:
    <script>
    $(function() {
        $("#mute").click(function(e) {
            e.preventDefault();
    
            var song = $('audio')[0]
            if (song.paused){
                song.play();
                document.getElementById("mute").src = "images/music-off.png";
    		      
    	    }else{
                song.pause();
                document.getElementById("mute").src = "images/music-on.png";
    			
            }
            });
    });
    </script>
    
    <audio id="audio" preload="auto" tabindex="0" type="audio/mpeg">
    </audio>
    <div class="style1">
     <a href="#" >
            <img id="mute" src="images/music-off.png" "class="style2"/></img>
    </a>
    </div>

  10. #8
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    If anyone needs it, I finally worked it out.

    Now there there's mouseover, or hover, to both, Play Audio and Mute Audio.

    Code:
    <script>
    $(function() {
        $("#mute").click(function(e) {
            e.preventDefault();
    
            var song = $('audio')[0]
            if (song.paused){
                song.play();
                document.getElementById("mute").src = "images/music-off.png";
            }else{
                song.pause();
                document.getElementById("mute").src = "images/music-on.png";
            }
            });
    });
    
      $("#mute").hover(function(e) {
            var song = $('audio')[0];
            if (song.paused){
                $(this).attr("src", "images/music-on-over.png");		      
    	    } else {
                $(this).attr("src", "images/music-off-over.png");		      			
            }
        }, function(e) {
            var song = $('audio')[0]
            if (song.paused){
                $(this).attr("src", "images/music-on.png");		      
    	    } else {
                $(this).attr("src", "images/music-off.png");		      			
            }
       });
    </script>

Similar Threads

  1. Ultimate Fade-in slideshow (v2.4) Audio?
    By Diannah in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 01-26-2011, 05:35 AM
  2. Can the Ultimate Fade-in slideshow work with DOM Image Rollover v3.0?
    By javajones in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 12-20-2010, 05:10 AM
  3. Image Fade In on link rollover
    By jickpictures in forum Looking for such a script or service
    Replies: 5
    Last Post: 04-16-2010, 01:53 AM
  4. Ultimate Fade-in slideshow (v1.51) rollover links
    By wbowman in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 04-22-2009, 05:23 PM
  5. Rollover Image Fade Swap
    By djswilson in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 02-11-2007, 01:07 AM

Bookmarks

Posting Permissions

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