当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML oncanplaythrough事件用法及代码示例


当缓冲指定的媒体并且浏览器估计它可以播放而不必停止时,将发生HTML DOM oncanplaythrough事件。

事件的顺序在音频/视频的加载过程中发生:

  • onloadstart
  • ondurationchange
  • onloadedmetadata
  • onloadeddata
  • onprogress
  • oncanplay
  • oncanplaythrough

用法:


在HTML中:

<element oncanplaythrough="myScript">

在JavaScript中:

object.oncanplaythrough = function(){myScript};

在JavaScript中,使用addEventListener()方法:

object.addEventListener("canplaythrough", myScript);

例:使用HTML

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM oncanplaythrough event</h2> 
        <video controls oncanplaythrough="myFunction()"> 
            <source src="Geekfun.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            function myFunction() { 
                alert("Can play through video without stopping"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

输出:

例:使用JavaScript

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM oncanplaythrough event</h2> 
        <video controls id="myVideo"> 
            <source src="Geekfun.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            document.getElementById( 
              "myVideo").oncanplaythrough = function() { 
                myFunction() 
            }; 
  
            function myFunction() { 
                alert("Can play through video without stopping"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

输出:

例:使用addEventListener()方法:

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM oncanplaythrough event</h2> 
        <video controls id="myVideo"> 
            <source src="Geekfun.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            document.getElementById( 
            "myVideo").addEventListener("canplaythrough", myFunction); 
  
            function myFunction() { 
                alert("Can start playing video"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

输出:

支持的浏览器:DOM oncanplaythrough事件支持的浏览器如下:

  • 谷歌浏览器
  • IE浏览器
  • Firefox
  • 苹果Safari
  • Opera


相关用法


注:本文由纯净天空筛选整理自Vijay Sirra大神的英文原创作品 HTML | DOM oncanplaythrough Event。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。