我有一个视频播放器网站,我有一个“下一个”按钮,该按钮应停止视频,更改指向下一个视频的src并使用javascript播放,但我无法使用javascript循环来更改src。
在这里我的刀片文件
@extends('layouts.app')
@section('content')
<section class="player">
<center>
<video id="myvideo" class="playing-video" src="{{url('songs/'.$songs[0]->filename)}}" controls autoplay onended="nextVideo()">
</video>
<br>
<button id="next" style="background-color: transparent;border: none;color: white;" onclick="nextVideo()" type="button">
<h2><i class="fas fa-chevron-right"></i>Next</h2></button>
</center>
</section>
这是不起作用的javascript
var i = 0;
var myVideo = document.getElementById("myvideo");
function nextVideo() {
i++;
myVideo.setAttribute("src", "http://localhost/Laravel/anime/public/songs/" + {{ $songs[i] -> filename }});
myVideo.load();
myVideo.play();
}
我知道我不能在刀片大括号内直接使用javascript变量,但是我没有主意!
答案 0 :(得分:1)
将Laravel集合存储为按钮上的数据点:
<button id="next" data-songs="{{json_encode($songs)}}" ... etc />
然后在nextVideo()JS方法中拉出JS对象并对其进行循环(所有JS):
function nextVideo() {
var songs = JSON.parse( //get the data from the #next button )
//loop using the JS songs object instead of the blade {{}}
}
答案 1 :(得分:0)
尝试使用切换按钮
<button onclick="Toggle()">Next</button>
<script>
var i =0;
function toggle(){
i=++i; // each time you press button the variable i increments value by one (inside brackets only)
if(i===1){
videoStop(); // Function to stop video
}else if(i===2){
videoNext(); // Create a function which will contain url of next video , autoplay functions
i=0; // return i value to 0 and continue using the toggle button
}
}
</script>