Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

javascript - How to pause or stop iframe and html5 video on tab change

I have a piece of code, where tabs contains different videos. The problem is, on changing the tab the video keeps on playing. I want it to stop playing when the tab is changed. What I want to achieve is this https://dramasq.de/kr201012/63.html What I achieved so far is placed here, http://mento.xyz/demo.html

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial;}

/* Style the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
</style>
</head>
<body>

<h2>Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent">
<script src="https://cdn.fluidplayer.com/v3/current/fluidplayer.min.js"></script>
<video id="video" style="width: 640px; height: 360px;"> <source src="https://bilibili.xiang-kuyun.com/20210106/5614_a1dfad3e/index.m3u8" type="application/x-mpegURL" />
</video>
<script>
fluidPlayer(
   'video',
    {
        layoutControls: {
            primaryColor:          "#28B8ED",
           fillToContainer:        true, 
   controlBar: {
                autoHide:           true,
                autoHideTimeout:    3,
                animated:           true
            }         
        }
    }
);
</script>
</div>

<div id="Paris" class="tabcontent">
<iframe width="852" height="480" src="https://www.youtube.com/embed/Qjv1biOIoZs" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

<div id="Tokyo" class="tabcontent">
<script src="https://cdn.fluidplayer.com/v3/current/fluidplayer.min.js"></script>
<video id="video1" style="width: 640px; height: 360px;"> <source src="https://bilibili.xiang-kuyun.com/20210106/5614_a1dfad3e/index.m3u8" type="application/x-mpegURL" />
</video>
<script>
fluidPlayer(
   'video1',
    {
        layoutControls: {
            primaryColor:          "#28B8ED",
           fillToContainer:        true, 
   controlBar: {
                autoHide:           true,
                autoHideTimeout:    3,
                animated:           true
            }         
        }
    }
);
</script>
</div>

<script>
function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
  /**
 * Stop an iframe or HTML5 <video> from playing
 * @param  {Element} element The element that contains the video
 */
var stopVideo = function ( tabcontent ) {
    var iframe = tabcontent.querySelector( 'iframe');
    var video = tabcontent.querySelector( 'video' );
    if ( iframe ) {
        var iframeSrc = iframe.src;
        iframe.src = iframeSrc;
    }
    if ( video ) {
        video.pause();
    }
}; 
}
</script>
   
</body>
</html> 
question from:https://stackoverflow.com/questions/65598563/how-to-pause-or-stop-iframe-and-html5-video-on-tab-change

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...