简体   繁体   English

无法切换音频/字体真棒图标

[英]Can't toggle audio/font awesome icon

I'm pretty new to JS/jQuery, please forgive me if I've made an incredibly obtuse mistake!我对 JS/jQuery 还很陌生,如果我犯了一个非常愚蠢的错误,请原谅我! :) :)

var backgroundTrackObj        = document.createElement("audio");
    backgroundTrackObj.src    = "//tjone270.org/quakelive/2-24%20The%20Fiery%20Stronghold.mp3";
    backgroundTrackObj.volume = 0.1;
var playpauseButton           = $("#playpause_background");

function BackgroundTrack(nochange) {
  if (typeof(Storage) !== "undefined") {
    var bgMusic = localStorage.getItem("bgMusic");
    if (bgMusic === "false") {
      backgroundTrackObj.pause();

      playpauseButton.removeClass("fa-pause");
      playpauseButton.addClass("fa-play");
      if (!nochange) {
        localStorage.setItem("bgMusic", "false");
      }
    } else if (bgMusic === "true" || bgMusic === null) {
      backgroundTrackObj.play();
      playpauseButton.removeClass("fa-play");
      playpauseButton.addClass("fa-pause");
      if (!nochange || bgMusic === null) {
        localStorage.setItem("bgMusic", "true");
      }
    }
  } else {
    backgroundTrackObj.play();
  }
}

BackgroundTrack(true);

This is my code.这是我的代码。 I'm loading it in as a file to the current page (via <script src> ).我将它作为文件加载到当前页面(通过<script src> )。

There's a clickable icon in my navbar:我的导航栏中有一个可点击的图标:

<li><a class="navbar-brand" style="color: white; text-decoration: none;" onclick="BackgroundTrack(false)"><i id="playpause_background" class="fa"></i></a></li>

When the page loads, it runs BackgroundTrack(true) .当页面加载时,它运行BackgroundTrack(true) I expect it to look at local storage for the bgMusic key, and if found with a "true" or "false" value, play or not play accordingly and change the clickable icon's icon to match the current state.我希望它查看bgMusic键的本地存储,如果发现值为“true”或“false”,则相应地播放或不播放并更改可点击图标的图标以匹配当前状态。

When I click the button, nothing happens, however if I manually set the local storage key, the music plays/not plays properly, but the button itself won't set the bgMusic key.当我单击按钮时,没有任何反应,但是如果我手动设置本地存储键,音乐播放/无法正常播放,但按钮本身不会设置bgMusic键。 The BackgroundTrack(true) will set the key to "true" if the key doesn't previously exist - but that's as much luck as I'm having with it at the moment.如果该键以前不存在, BackgroundTrack(true)会将键设置为“true” - 但这与我目前拥有的运气一样多。

I've manually tested the .play() and .pause() methods on the backgroundTrackObj var, and that works fine in the Chrome console.我手动测试.play().pause()的方法backgroundTrackObj VAR,而且适用于Chrome控制台罚款。 Using .addClass and .removeClass doesn't seem to work at all however. .addClass ,使用.addClass.removeClass似乎根本不起作用。

Any ideas anyone?任何人的想法?

UPDATE: https://jsfiddle.net/7qxrqk8q更新: https : //jsfiddle.net/7qxrqk8q

There was a few things to change.有几件事情需要改变。

  1. 1st of all, the click was assigned to icon, but not the button.首先,点击被分配给图标,而不是按钮。
  2. The other thing is you checked the state of the music parameter, based on local storage, starting from the 1st click.另一件事是您检查了音乐参数的状态,基于本地存储,从第一次点击开始。 So that is why you wasn't able to change it.所以这就是你无法改变它的原因。
  3. And final, is that you checking Storage, but why not localStorage?最后,是您检查存储,但为什么不检查 localStorage?

I've created button click with jQuery:我已经用 jQuery 创建了按钮点击:

$("#btn").click(function(){
  musicState = !musicState;
    backgroundTrack(musicState);
});

Button id:按钮标识:

<button id="btn"><i id="playpause_background" class="fa"></i></button>

As well as variable for music play state:以及音乐播放状态的变量:

musicState = false;

Function:功能:

function backgroundTrack(el) {
 var bgMusic = localStorage.getItem("bgMusic");
  if(localStorage){
    if (el === false) {
      playpauseButton.removeClass("fa-pause");
      playpauseButton.addClass("fa-play");
      backgroundTrackObj.pause();
      if (!el) {
        localStorage.setItem("bgMusic", "false");
      }
    } else if ( (el === true) || (bgMusic === null) ){
      playpauseButton.removeClass("fa-play");
      playpauseButton.addClass("fa-pause");
      backgroundTrackObj.play();
      if (!el || bgMusic === null) {
        localStorage.setItem("bgMusic", "true");
      }
    }
   } else {
      backgroundTrackObj.play();
   }
}

Notify me if that was helpful.如果这有帮助,请通知我。

Check my solution here.在此处查看我的解决方案。

I updated your BackgroundTrack function as below.我更新了您的 BackgroundTrack 功能,如下所示。 The thing is that you need to distinguish between using the local storage and whether the button is being played or not.问题是您需要区分使用本地存储和按钮是否正在播放。 Both of them can have the same value but not all the time.它们都可以具有相同的值,但并非始终如此。 This is what was making writing the function a bit tricky.这就是编写函数有点棘手的原因。 The best you can get is to click twice at the pause button at the very beginning when you start the page, even it will not track the storage.最好的办法是在启动页面时在最开始的暂停按钮处单击两次,即使它不会跟踪存储。

function BackgroundTrack(nochange) {
    if (typeof(Storage) === "undefined") {
        return backgroundTrackObj.play();
    }

    var bgMusic = localStorage.getItem("bgMusic"), update;
    var isPlay = nochange ? bgMusic === "true" : playpauseButton.hasClass("fa-play");

    if(isPlay){ //toggle
        playpauseButton.removeClass("fa-play");
        playpauseButton.addClass("fa-pause");
        backgroundTrackObj.play();
        update = "true";
    }else{
        playpauseButton.removeClass("fa-pause");
        playpauseButton.addClass("fa-play");
        backgroundTrackObj.pause();
        update = "false";
    }

    if (!nochange) {
        localStorage.setItem("bgMusic", update);
    }
}

 var backgroundTrackObj = document.createElement("audio"); backgroundTrackObj.src = "//tjone270.org/quakelive/2-24%20The%20Fiery%20Stronghold.mp3"; backgroundTrackObj.volume = 0.1; var playpauseButton = $("#playpause_background"); function BackgroundTrack(nochange) { if (typeof(Storage) === "undefined") { return backgroundTrackObj.play(); } var bgMusic = localStorage.getItem("bgMusic"), update; var isPlay = nochange ? bgMusic === "true" : playpauseButton.hasClass("fa-play"); if(isPlay){ //toggle playpauseButton.removeClass("fa-play"); playpauseButton.addClass("fa-pause"); backgroundTrackObj.play(); update = "true"; }else{ playpauseButton.removeClass("fa-pause"); playpauseButton.addClass("fa-play"); backgroundTrackObj.pause(); update = "false"; } if (!nochange) { localStorage.setItem("bgMusic", update); } } BackgroundTrack(true);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html lang="en"> <head> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://use.fontawesome.com/a0b5affde9.js"></script> </head> <body> <button onclick="BackgroundTrack(false)"><i id="playpause_background" class="fa"></i></button> </body> </html>

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

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