【问题标题】:Why is my Audio not playing when I hover the Div?为什么当我悬停 Div 时我的音频没有播放?
【发布时间】:2018-09-04 16:55:32
【问题描述】:

我的重复径向渐变没有播放音频。 有人可以帮忙吗?我想我的 id 有问题。 第一个链接是代码,第二个是网站。 https://github.com/Robocop79/Websiteland/blob/master/index.html https://robocop79.github.io/Websiteland/

【问题讨论】:

    标签: javascript html css audio


    【解决方案1】:

    您的 Javascript 代码中的主要问题是:

    • HTML 中没有 ID 为 nav-two 的元素,您的意思是 grad1
    • 您正在克隆音频<source> 元素,但.play() 只能在<audio> 上执行
    $("#grad1 a").each(function(i) {
      if (i != 0) {
        $("#bay").parent()
        .clone()
        .attr("id", "bay" + i)
        .appendTo($(this).parent());
      }
      $(this).data("bay", i);
    })
    .mouseenter(function() {
      $("#bay" + $(this).data("bay"))[0].play();
    });
    

    您还可以通过在 .each 函数内移动 mouseenter 调用来简化音频触发方式(因此不再需要 data 属性)。

    $("#grad1 a").each(function(i) {
      if (i != 0) {
        var clonedaudio = $("#bay").parent()
          .clone()
          .appendTo($(this).parent());
    
        $(this).mouseenter(function() {
          clonedaudio[0].play();
        });
      }
    })
    

    【讨论】:

    猜你喜欢
    • 2018-03-12
    • 2022-01-06
    • 1970-01-01
    • 2023-04-05
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    相关资源
    最近更新 更多