在iframe嵌入时点击显示div或计数器开始

时间:2017-03-16 17:26:14

标签: javascript jquery html iframe youtube

当我点击youtube嵌入iframe时,我正在尝试显示或开始计数。 这是我的代码

    <iframe id="startcount" width="900" height="400" src="https://www.youtube.com/embed/qAM8wEHPccU" frameborder="0" allowfullscreen></iframe>

    <span align="center" style="color:#0091bf; font-weight:bold; font-size:24px;">You will be redirected in 
    <span id="counter">30</span> second(s) for credit verification. </span>

    <script type="text/javascript">


    $(document).ready(
    function() {
        $("#startcount").click(function() {

                function countdown() {
                var i = document.getElementById('counter');
                if (parseInt(i.innerHTML)<=0) {
                //location.href = 'earn_credit_ac.php?ads_id=<?php echo $del_id; ?>';
                }
                i.innerHTML = parseInt(i.innerHTML)-1;
                }
                setInterval(function(){ countdown(); },1000);

                });
    });


    </script>
  1. 点击youtube embeded video iframe
  2. 开始计算
  3. 帮助我,我是jquery的新手。

    感谢All

2 个答案:

答案 0 :(得分:0)

如果您倒计时,则应检查该值是否大于0且不小于0。 以下代码应该有效:

$(document).ready(function() {
      $("#startcount").click(function() {
        function countdown() {
          var i = $("#counter");
          var value = parseInt(i.html());
          var interval;

          if (value >= 0) { // I think you want "greater equal" (>=) instead of "less equal" (<=)
            //location.href = 'earn_credit_ac.php?ads_id=<?php echo $del_id; ?>';
          }
          else {
              clearInterval(interval); // stop interval if counted to -1
          }

          i.html(value - 1);
        }

        setInterval(countdown, 1000);
      });
    });

答案 1 :(得分:0)

我正在使用其他方法来完成此任务。

Html:

  <button id="play-button" class="btn mobile-full" style="padding:10px;"> Play </button>

  <iframe id="video" src="//www.youtube.com/embed/NV16WtEXP6E?autohide=2&border=0&wmode=opaque&enablejsapi=1&modestbranding=1&controls=0&showinfo=1&rel=0" frameborder="0" allowfullscreen></iframe>

  <div align="center">
  <span align="center" style="color:#0091bf; font-weight:bold; font-size:24px;">You will be redirected in 
 <span id="counter"><?php echo $vid_duration; ?></span> second(s) for credit verification. </span>
 </div>

Css:

.button {
  width: 48px;
  height: 48px;
  cursor: pointer;
  &:hover {
    fill: white; 
  }
}

.defs {
  position: absolute;
  top: -9999px;
  left: -9999px;
}

iframe {
  float: left;
  width: 300px;
  height: 200px;
}

.buttons {
  padding: 1rem;
  background: #f06d06;
  float: left;
}

JavaScript:

<script>

// https://developers.google.com/youtube/iframe_api_reference

// global variable for the player
var player;

// this function gets called when API is ready to use
function onYouTubePlayerAPIReady() {
  // create the global player from the specific iframe (#video)
  player = new YT.Player('video', {
    events: {
      // call this function when player is ready to use
      'onReady': onPlayerReady
    }
  });
}

function onPlayerReady(event) {

  // bind events
  var playButton = document.getElementById("play-button");
  playButton.addEventListener("click", function() {
    player.playVideo();

    function countdown() {
        var i = document.getElementById('counter');
        if (parseInt(i.innerHTML)<=0) {
            location.href = 'earn_credit_ac.php?ads_id=<?php echo $del_id; ?>';
        }
        i.innerHTML = parseInt(i.innerHTML)-1;
    }
    setInterval(function(){ countdown(); },1000);

  });



}

// Inject YouTube API script
var tag = document.createElement('script');
tag.src = "//www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);


</script>

这是在手机上播放YouTube视频的唯一方法,同时启动时间计数器。

感谢所有人。