javascript - 悬停在 Django 模板中的视频播放
问题描述
我通过迭代查询集对象来显示 Django 模板中的视频列表。我想在鼠标输入时播放特定的视频。
Django模板:
{% for video in videos %}
<div class="card bg-light pl-3" style="width:310px;height:300px;border:none">
<a href="{% url 'core:videoview' video.id %}"><video class="videocard" src="{{video.video_file.url}}" poster="{{video.thumbnail.url}}" alt="not found" style="height:185px" preload></a>
<div class="card-body">
<div class="row" style="">
<div class="fst pt-2 pl-3">
<img src="{{video.video_owner.channel_owner.profile_pic.url}}" class="rounded-circle" style="height:45px" alt="">
</div>
<div class="sec pt-2 pl-3">
<tr>
<p class="font-weight-bold""><a href="{% url 'core:videoview' video.id %}" style="text-decoration:none;color:black">{{video.video_title|truncatechars:"25"}}</a>
<br>
<a href="#" style="text-decoration:none;color:black"><span class="font-weight-normal">{{video.video_owner}}</span></a>
</p>
</tr>
</div>
</div>
</div>
</div>
{% endfor %}
JavaScript:
$(".videocard").on('mousenter', function(){
$('.videocard').get(0).play();
$('.videocard').get(0).currentTime = 0;
});
$(".videocard").on('mouseout', function(){
$('.videocard').get(0).pause();
$('.videocard').get(0).currentTime = 0;
});
但由于 get(0),它仅适用于视频列表中的第一个视频。如何使其适用于每个显示的视频?
解决方案
您可以在此处使用 event 关键字
$(".videocard").on('mousenter', function(event){
event.target.play();
event.target.currentTime = 0;
});
$(".videocard").on('mouseout', function(event){
event.target.pause();
event.target.currentTime = 0;
});
推荐阅读
- assembly - 这个汇编函数有什么问题来制作 idt 条目?
- arrays - C中的字符串和空格
- python - 如何返回带有滚动窗口但没有应用附加功能的熊猫数据框?
- retrofit - 如何将多个数组添加到 kotlin 中的 JSON 对象以进行改造
- android - 如何让应用程序通过 Ultra Battery Saver 模式/Adb shell 卸载 Ultra Battery Saver 运行?(无根)
- python - 在 Heroku 错误上托管 Spotify API 应用程序
- excel - 使用 Excel VBA 操作 Visio 形状
- javascript - 在 fetch 和 mount 完成后做一些事情
- python - 在 pandas 中查找行之间和列之间的匹配项
- javascript - 新的 Android 权限不提示使用麦克风/相机