如何使用JavaScript函数替换图标

huangapple go评论61阅读模式
英文:

How to replace icons with Javascript functions

问题

我一直在为Spotify Clone应用程序制作一些交互式播放器图标。我没有问题播放歌曲文件,但似乎无法弄清楚如何使用OnClick事件替换图标类(从'fa-solid-play'到'fa-solid-pause'等)。

一开始,我尝试使用innerHTML.text方法更改类,但发现只是嵌套了一个新图标在已经存在的图标内部。下面的代码在调用函数时返回classList = null,并且对于任何removeClass/addClass替代方法也是如此。我感觉可能需要重新构建一些HTML才能弄清楚这个问题。

供参考,这是HTML的一部分:

<div class="Player-Icons">
   <div class="Icon-Div">
     <div class="Shuffle-Icon" onclick="shuffle()"><i class="fa-solid fa-shuffle"></i></div>
     <div class="Previous-Icon" onclick="previous()"><i class="fa-solid fa-backward"></i></div>
     <div class="Play-Icon" onclick="playsong()"><i class="fa-solid fa-circle-play" aria-hidden='true' id="Triangle-Play"></i></div>
     <div class="Next-Icon" onclick="next()"><i class="fa-sharp fa-solid fa-forward-step"></i></div>
     <div class="Repeat-Icon" onclick="shuffle()"><i class="fa-solid fa-repeat"></i></div>
   </div>
</div>

JavaScript代码片段:

let play = document.querySelector('.Play-Icon');
let next = document.querySelector('.Next-Icon');
let current_title = document.querySelector('.Song-Title');
let current_artist = document.querySelector('.Song-Artist');
let current_image = document.querySelector('.Current-Image');
let slider = document.querySelector('.Slider');
let time = document.querySelector('.Song-Start');
let remaining_time = document.querySelector('.Song-Finish');
let volume = document.querySelector('.Volume-Slider');

let timer;
let autoplay = 0;

let index_no = 0;
let playing_song = false;

// Audio Element
let track = document.createElement('audio');

// 歌曲列表
let All_song = [
{
  name: "No Ceilings",
  path: "Playlist /Track/No Ceilings.mp3",
  img: "Image/image1.jpg",
  artist: "Tony Shhnow",
}
]

// 所有功能

// 加载曲目
function load_track(index_no){
track.src = All_song[index_no].path;
current_title.innerHTML =  'No Ceilings'; // All_song[index_no].name
current_artist.innerHTML = 'Tony Shhnow';
current_image = All_song[index_no].img;
track.load();
console.log('Current artist is ' + current_artist.innerHTML);
console.log('Current title is ' + current_title.innerHTML);
}

load_track(index_no);

// 检查当前歌曲/If else
function justplay(){
 if(playing_song == false) {
    playsong();
} /*else{
    pausesong();
}*/
}

// 播放示例歌曲的功能
function playsong(){
 track.play();
 playing_song = true

if(playing_song == true)  {
    document.querySelector('.Play-Icon').classList.remove('fa-circle-play');
    document.querySelector('.Play-Icon').classList.add('fa-circle-pause');
    console.log('btnpressed');
} 
}

我希望这对你有所帮助。

英文:

I've been working on some interactive player icons for a Spotify Clone application.
I have no problem getting the icon/button to play the song file, but I can't seem to figure out how to replace the icon classes (from 'fa-solid-play' to 'fa-solid-pause' for ex.) using an OnClick event.

At first I tried to change the classes using the innerHTML.text method but I found that just nested a new icon inside the already existing one.
The following code returns classList = null when I call the function, and the same is true for any kind of removeClass/ addClass alternative.
I feel I may have to refactor some of the HTML for this to make sense perhaps.

For reference this a snippet of the HTML:

 &lt;div class=&quot;Player-Icons&quot;&gt;
&lt;div class=&quot;Icon-Div&quot;&gt;
&lt;div class=&quot;Shuffle-Icon&quot; onclick=&quot;shuffle()&quot;&gt;&lt;i class=&quot;fa-solid fa-shuffle&quot;&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;Previous-Icon&quot; onclick=&quot;previous()&quot;&gt;&lt;i class=&quot;fa-solid fa-backward&quot;&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;Play-Icon&quot; onclick=&quot;playsong()&quot; &gt;&lt;i class=&quot;fa-solid fa-circle-play&quot;  aria-hidden=&#39;true&#39; id=&quot;Triangle-Play&quot;&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;Next-Icon&quot; onclick=&quot;next()&quot;&gt;&lt;i class=&quot;fa-sharp fa-solid fa-forward-step&quot;&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;Repeat-Icon&quot; onclick=&quot;shuffle()&quot;&gt;&lt;i class=&quot;fa-solid fa-repeat&quot;&gt;&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;

JS Snippet:

    { let play = document.querySelector(&#39;.Play-Icon &#39;);
let next = document.querySelector(&#39;.Next-Icon&#39;);
let current_title = document.querySelector(&#39;.Song-Title&#39;);
let current_artist = document.querySelector(&#39;.Song-Artist&#39;);
let current_image = document.querySelector(&#39;.Current-Image&#39;);
let slider = document.querySelector(&#39;.Slider&#39;);
let time = document.querySelector(&#39;.Song-Start&#39;);
let remaining_time = document.querySelector(&#39;.Song-Finish&#39;);
let volume = document.querySelector(&#39;.Volume-Slider&#39;);
let timer;
let autoplay = 0;
let index_no = 0;
let playing_song = false;
//const playIconClassName = &#39;fa-circle-play&#39;
//const pauseIconClassName = &#39;fa-circle-pause&#39;
//Audio Element
let track = document.createElement(&#39;audio&#39;);
//Song List 
let All_song = [
{
name: &quot;No Ceilings&quot;,
path: &quot;Playlist /Track/No Ceilings.mp3&quot;,
img: &quot;Image/image1.jpg&quot;,
artist: &quot;Tony Shhnow&quot;,
}
]
//All Functions
//Load the track
function load_track(index_no){
track.src = All_song[index_no].path;
current_title.innerHTML =  &#39;No Ceilings&#39;; //All_song[index_no].name
current_artist.innerHTML = &#39;Tony Shhnow&#39;;
current_image = All_song[index_no].img;
track.load();
console.log(&#39;Current artist is &#39; + current_artist.innerHTML);
console.log(&#39;Current title is &#39; + current_title.innerHTML );
}
load_track(index_no);
//Checker for current song / If else 
function justplay(){
if(playing_song==false) {
playsong();
} /*else{
pausesong();
}*/

}

//Function for playing sample song
function playsong(){
track.play();
playing_song = true
if(playing_song==true)  {
document.querySelector(&#39;Play-Icon&#39;).classList.remove(&#39;fa-circle-play&#39;);
document.querySelector(&#39;Play-Icon&#39;).classList.add(&#39;fa-circle-pause&#39;);
console.log(&#39;btnpressed&#39;);
} 

}

At first I tried to change the classes using the innerHTML.text method but I found that just nested a new icon inside the already existing one.
The following code returns classList = null when I call the function, and the same is true for any kind of removeClass/ addClass alternative.
I feel I may have to refactor some of the HTML for this to make sense perhaps.

答案1

得分: 1

缺少一个 "."

querySelector('div') 会找到一个 div 元素。你所指的是一个类名,所以你需要一个 .

尝试将以下代码更改为:

if(playing_song==true)  {
    document.querySelector('.Play-Icon').classList.remove('fa-circle-play');
    document.querySelector('.Play-Icon').classList add('fa-circle-pause');
    console.log('btnpressed');
}
英文:

Missing a "."

querySelector(&#39;div&#39;) will find a div element.
What you are referring to is a class name, so you need a ..

Try changing

if(playing_song==true)  {
document.querySelector(&#39;Play-Icon&#39;).classList.remove(&#39;fa-circle-play&#39;);
document.querySelector(&#39;Play-Icon&#39;).classList.add(&#39;fa-circle-pause&#39;);
console.log(&#39;btnpressed&#39;);
} 

to

if(playing_song==true)  {
document.querySelector(&#39;.Play-Icon&#39;).classList.remove(&#39;fa-circle-play&#39;);
document.querySelector(&#39;.Play-Icon&#39;).classList.add(&#39;fa-circle-pause&#39;);
console.log(&#39;btnpressed&#39;);
} 

huangapple
  • 本文由 发表于 2023年2月27日 19:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定