简体   繁体   English

单击更改图标

[英]Changing Icon on click

I'm trying to change the icon when the audio is playing. 我正在尝试在播放音频时更改图标。

So, when the user hovers over the image, a play button will appear. 因此,当用户将鼠标悬停在图像上时,将出现一个play按钮。 The user will then click the play button and the audio will then begin. 然后,用户将单击play按钮,然后将开始音频。 I'd like to icon to change to the pause button when the audio is playing, then when the user wants to stop the audio, they will click the pause button and it will return to the play button. 我想在pause音频时将图标更改为pause按钮,然后当用户想要停止音频时,他们将单击pause按钮,然后它将返回到play按钮。 I thought my code would work by removing and adding classes, but it doesn't. 我以为我的代码可以通过删除和添加类来工作,但事实并非如此。

 var audio = document.getElementById("audio"); function play() { icon = $(this).find("i"); if (audio.paused) { audio.play(); icon.addClass("fa-pause-circle").removeClass("fa-play-circle"); } else { audio.pause(); icon.addClass("fa-play-circle").removeClass("fa-pause-circle"); } } 
 #credits-content { display: grid; grid-template-rows: repeat(1, 1fr); grid-template-columns: repeat(1, 1fr); grid-gap: 15px; height: 750px; width: 80%; margin: 0 10% 50px 10%; } #images-services { grid-row: span 1; grid-column: span 1; } .image-item { display: flex; justify-content: center; align-items: center; color: #f1f1f1; } #coward-image:hover { box-shadow:inset 0 0 0 2000px rgba(0, 0, 0, 0.7); cursor: pointer; } #coward-image { background-image: url("https://i.ytimg.com/vi/xQ0_zWouE8c/maxresdefault.jpg"); background-position: center; background-repeat: no-repeat; background-size: cover; box-shadow:inset 0 0 0 2000px rgba(0, 0, 0, 0.0); -moz-transition: all .2s ease-in; -o-transition: all .2s ease-in; -webkit-transition: all .2s ease-in; transition: all .2s ease-in; } #coward-play { opacity: 0; -moz-transition: all .2s ease-in; -o-transition: all .2s ease-in; -webkit-transition: all .2s ease-in; transition: all .2s ease-in; } #coward-image:hover #coward-play{ opacity: 1; } 
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="credits"> <div id="credits-content"> <div id="coward-image" class="image-item images-services"> <audio src="songs/memo.m4a" id="audio"></audio> <i class="far fa-play-circle fa-7x" id="coward-play" onclick="play()"></i> </div> </div> </div> 

The this in your function doesn't refer to the element, as you only have access to this for an HTML onclick event inside the actual attribute. 函数中的this不引用元素,因为您只能在实际属性内访问HTML onclick事件的this Also, as you have the onclick attached to the icon itself, there is no need to use find() , as that method returns an element's descendant nodes. 另外,由于onclick附加到图标本身,因此无需使用find() ,因为该方法返回元素的后代节点。 If you move the onclick to the div and pass in this as a parameter to the function, you can use your code basically as written. 如果将onclick移至div并将this作为参数传递给函数,则基本上可以按照编写的方式使用代码。 I made a Codepen here 我在这里做了一个Codepen

You're using $(this) within the play function to try and reference the i icon , which is incorrect scope. 您在play函数中使用$(this)尝试引用i icon ,这是错误的作用域。 If you wanted, you could do onclick="play(this)" and pass the object reference (the i icon element) into your play function and use it correctly there, using $(thisElement) as the object reference within the play function. 如果需要,可以执行onclick="play(this)"并将对象引用( i图标元素)传递到play函数中,并在其中正确使用它,使用$(thisElement)作为play函数中的对象引用。 Also, if you're using jQuery , you can reference the i icon by its id attribute of $('#coward-play') instead of using .find() ; 另外,如果您使用的是jQuery ,则可以通过其id属性$('#coward-play')来引用i icon ,而不是使用.find() find() looks for nested elements... find()寻找嵌套元素...

I created an example below that shows the alternate way of using .on('click') functionality of jQuery to bind to your coward-play icon , but commented that code out. 我创建了一个例子以下,显示使用的另一种方法.on('click')的功能jQuery绑定到你的coward-play icon ,但评论的代码了。 I've made the example use the onclick="play(this)" route to stay close to your original code. 我使该示例使用onclick="play(this)"路线来保持与原始代码的距离。

I also added onended="reset()" to your audio element, so that when the audio finishes playing, it will reset the icon back to the play button as expected. 我还向您的audio元素中添加了onended="reset()" ,以便在音频播放完毕后,它将按预期将图标重置回播放按钮。

Warning, Horse Noises when playing audio.. 警告,播放音频时会发出马声。

 var audio = document.getElementById("audio"); // THIS IS AN ALTERNATE WAY OF DOING THE CLICK VIA JQUERY /* $('#coward-play').on('click', function() { play(this); }); */ function play(thisElement) { if (audio.paused) { audio.play(); $(thisElement).addClass("fa-pause-circle").removeClass("fa-play-circle"); } else { audio.pause(); $(thisElement).addClass("fa-play-circle").removeClass("fa-pause-circle"); } } function reset() { $('#coward-play').addClass("fa-play-circle").removeClass("fa-pause-circle"); } 
 #credits-content { display: grid; grid-template-rows: repeat(1, 1fr); grid-template-columns: repeat(1, 1fr); grid-gap: 15px; height: 750px; width: 80%; margin: 0 10% 50px 10%; } #images-services { grid-row: span 1; grid-column: span 1; } .image-item { display: flex; justify-content: center; align-items: center; color: #f1f1f1; } #coward-image:hover { box-shadow: inset 0 0 0 2000px rgba(0, 0, 0, 0.7); cursor: pointer; } #coward-image { background-image: url("https://i.ytimg.com/vi/xQ0_zWouE8c/maxresdefault.jpg"); background-position: center; background-repeat: no-repeat; background-size: cover; box-shadow: inset 0 0 0 2000px rgba(0, 0, 0, 0.0); -moz-transition: all .2s ease-in; -o-transition: all .2s ease-in; -webkit-transition: all .2s ease-in; transition: all .2s ease-in; } #coward-play { opacity: 0; -moz-transition: all .2s ease-in; -o-transition: all .2s ease-in; -webkit-transition: all .2s ease-in; transition: all .2s ease-in; } #coward-image:hover #coward-play { opacity: 1; } 
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="credits"> <div id="credits-content"> <div id="coward-image" class="image-item images-services"> <audio src="https://www.w3schools.com/html/horse.ogg" id="audio" onended="reset()"></audio> <i class="far fa-play-circle fa-7x" id="coward-play" onclick="play(this)"></i> </div> </div> </div> 

You can accomplish this with some JS and JQuery. 您可以使用一些JS和JQuery完成此操作。

 var click = 1; function myFunction() { if (click < 18) { $('#coward-play').click(function() { $('#coward-play').addClass('fa-pause-circle'); $('#coward-play').removeClass('fa-play-circle'); click = 20; console.log(click); }); } if (click > 18) { $('#coward-play').click(function() { $('#coward-play').addClass('fa-play-circle'); $('#coward-play').removeClass('fa-pause-circle'); click = 1; console.log(click); }); } } 
 #credits-content { display: grid; grid-template-rows: repeat(1, 1fr); grid-template-columns: repeat(1, 1fr); grid-gap: 15px; height: 750px; width: 80%; margin: 0 10% 50px 10%; } #images-services { grid-row: span 1; grid-column: span 1; } .image-item { display: flex; justify-content: center; align-items: center; color: #f1f1f1; } #coward-image:hover { box-shadow: inset 0 0 0 2000px rgba(0, 0, 0, 0.7); cursor: pointer; } #coward-image { background-image: url("https://i.ytimg.com/vi/xQ0_zWouE8c/maxresdefault.jpg"); background-position: center; background-repeat: no-repeat; background-size: cover; box-shadow: inset 0 0 0 2000px rgba(0, 0, 0, 0.0); -moz-transition: all .2s ease-in; -o-transition: all .2s ease-in; -webkit-transition: all .2s ease-in; transition: all .2s ease-in; } #coward-play { opacity: 0; -moz-transition: all .2s ease-in; -o-transition: all .2s ease-in; -webkit-transition: all .2s ease-in; transition: all .2s ease-in; } #coward-image:hover #coward-play { opacity: 1; } 
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="credits"> <div id="credits-content"> <div id="coward-image" class="image-item images-services"> <audio src="songs/memo.m4a" id="audio"></audio> <i class="far fa-play-circle fa-7x" id="coward-play" onclick="myFunction()"></i> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM