當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。