gpt4 book ai didi

javascript - 在 Blazor 中显示模态视频

转载 作者:行者123 更新时间:2023-12-04 09:49:01 25 4
gpt4 key购买 nike

我正在尝试使用此模板创建一个用于在 Blazor 中查看视频的模式窗口 - https://codepen.io/JacobLett/pen/xqpEYE

我已将该函数放在 jsfunctions.js 文件中,并且我有对它的引用 - <script src="jsfunctions.js"></script>在 index.html 的 Head 标签内

function ModalShow() {
$(document).ready(function() {
// Gets the video src from the data-src on each button
var $videoSrc;
$('.video-btn').click(function() {
$videoSrc = $(this).data("src");
});
console.log($videoSrc);

// when the modal is opened autoplay it
$('#myModal').on('shown.bs.modal', function(e) {
// set the video src to autoplay and not to show related video. Youtube related video is like a box of chocolates... you never know what you're gonna get
$("#video").attr('src', $videoSrc + "?autoplay=1&amp;modestbranding=1&amp;showinfo=0");
})

// stop playing the youtube video when I close the modal
$('#myModal').on('hide.bs.modal', function(e) {
// a poor man's stop video
$("#video").attr('src', $videoSrc);
})

// document ready
});
}

但是,我不知道如何初始化 javascript 函数。
我尝试在 blazor 组件中使用以下方法
protected override async Task OnInitializedAsync()
{
await JSRuntime.InvokeAsync<object>("ModalShow").AsTask();
}

但是,我收到此错误:

Unhandled exception rendering component: $ is not defined
ReferenceError: $ is not defined
at ModalShow (https://localhost:5001/jsfunctions.js:10:5

我是 Blazor 的新手,我对 JavaScript 一无所知。是否有一种纯粹的 Blazor 方式来显示模态视频?

谢谢你。

后期编辑:
按钮的 HTML 代码:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary video-btn" data-toggle="modal" data-src="https://www.youtube.com/embed/Jfrjeg26Cwk" data-target="#myModal">
Play Video 1 - autoplay
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="" id="video" allowscriptaccess="always" allow="autoplay"></iframe>
</div>
</div>
</div>
</div>
</div>

CSS:

body {
margin: 2rem;
}

.modal-dialog {
max-width: 800px;
margin: 30px auto;
}

.modal-body {
position: relative;
padding: 0px;
}

.close {
position: absolute;
right: -30px;
top: 0;
z-index: 999;
font-size: 2rem;
font-weight: normal;
color: #fff;
opacity: 1;
}

最佳答案

像那样:

window.modalVideo = {
init: () => {
// Gets the video src from the data-src on each button
var $videoSrc;
$('.video-btn').click(function() {
$videoSrc = $(this).data("src");
});
console.log($videoSrc);

// when the modal is opened autoplay it
$('#myModal').on('shown.bs.modal', function(e) {
// set the video src to autoplay and not to show related video. Youtube related video is like a box of chocolates... you never know what you're gonna get
$("#video").attr('src', $videoSrc + "?autoplay=1&amp;modestbranding=1&amp;showinfo=0");
});

// stop playing the youtube video when I close the modal
$('#myModal').on('hide.bs.modal', function(e) {
// a poor man's stop video
$("#video").attr('src', $videoSrc);
});
}
};



protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("modalVideo.init");
}
}

您还需要在 intdex.html 中加载 JQuery: $是 JQuery

<script src="//code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

Bootstrap js 打开一个引导模式

<script src="//stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

还有波普尔,因为 bootstrap js depends on Popper.js

<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>

关于javascript - 在 Blazor 中显示模态视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62043053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com