• vue3实现video视频+弹幕评论


    vue3实现视频加评论

    之前写了一篇博客使用了弹幕插件http://t.csdnimg.cn/616mlvue3 使用弹幕插件,今天对这个页面进行了升级

    变成了

    vue3使用video

    这个没有使用插件,昨天看了好多,没发现有用的插件,下载了几个都没办法使用就用了原生

    1. <source src="../../assets/image/book.mp4" type="video/mp4" />
    2. 你的浏览器不支持视频标签。

    vue3使用弹幕

    弹幕还是用了之前那个文章里面的插件

    注意

     1.使用弹幕的时候记得层级关系,弹幕的层级一定是高于视频的,否则视频就会压着弹幕,弹幕显示不出来,所以把弹幕的层级提高就好了,

    2.弹幕的范围一定是小于视频的,既然弹幕的层级高了,那么就是压着视频的,如果弹幕的范围和视频的范围一样大或者视频的范围小于弹幕,那么鼠标就触碰不到视频了,所以弹幕的范围一定是小于视频的吗

    隐藏弹幕的实现

     之前是通过button来实现隐藏,现在通过switch开关实现,那么就没办法直接调用官方文档的方法,只能曲线调动通过watch监测滑块的开关,来判断是调用哪个方法,但是有个缺点,使用watch,第一次改变值,无法自动调用函数,没发现哪里我写错了,于是我在onMounted的时候自己调用了一次,后面就监视到了,就能调用函数了

    1. onMounted(() => {
    2. value1.value = true;
    3. });
    4. watch(value1, (newValue, OldValue, onCleanup) => {
    5. console.log(newValue);
    6. onCleanup(() => {
    7. console.log("111111");
    8. if (!newValue) {
    9. // 如果 newValue 为 false
    10. danmaku.value.show();
    11. } else {
    12. danmaku.value.hide();
    13. }
    14. });
    15. });

    控制弹幕速度那里我偷了一个懒,没有改变弹幕的详细速度,滑块的速度改变时假的,弹幕的速度虽然改变了,但是改变的不够精细,我让滑块大于50的时候速度+20,小于50了速度-20,没有实现确切的弹幕速度控制,有人想要写的话,可以帮忙改进一下

    1. watch(speed, (newValue, OldValue, onCleanup) => {
    2. onCleanup(() => {
    3. if (newValue > 50) {
    4. // 如果 newValue 为 false
    5. addspeeds();
    6. } else {
    7. jianspeeds();
    8. }
    9. });
    10. });
    11. //弹幕加速
    12. const addspeeds = () => {
    13. speeds.value += 20;
    14. console.log(speeds.value);
    15. };
    16. //弹幕减速
    17. const jianspeeds = () => {
    18. speeds.value -= 20;
    19. console.log(speeds.value);
    20. };

    页面完整代码

    1. <script setup>
    2. import vueDanmaku from "vue3-danmaku";
    3. import { ref, onMounted, reactive, watch } from "vue";
    4. import { getComments, postComments } from "../../api/api";
    5. const speed = ref(50);
    6. const speeds = ref(150);
    7. //内容
    8. const danmus = ref([]);
    9. const value1 = ref(false);
    10. onMounted(() => {
    11. getdata();
    12. value1.value = true;
    13. });
    14. const input = ref("");
    15. const visible = ref(false);
    16. //弹幕组件
    17. const danmaku = ref(null);
    18. watch(value1, (newValue, OldValue, onCleanup) => {
    19. console.log(newValue);
    20. onCleanup(() => {
    21. console.log("111111");
    22. if (!newValue) {
    23. // 如果 newValue 为 false
    24. danmaku.value.show();
    25. } else {
    26. danmaku.value.hide();
    27. }
    28. });
    29. });
    30. watch(speed, (newValue, OldValue, onCleanup) => {
    31. onCleanup(() => {
    32. if (newValue > 50) {
    33. // 如果 newValue 为 false
    34. addspeeds();
    35. } else {
    36. jianspeeds();
    37. }
    38. });
    39. });
    40. //继续播放弹幕
    41. const dplay = () => {
    42. danmaku.value.play();
    43. };
    44. //暂停播放弹幕
    45. const pause = () => {
    46. danmaku.value.pause();
    47. };
    48. //显示弹幕
    49. const show = () => {
    50. danmaku.value.show();
    51. };
    52. //隐藏弹幕
    53. const hide = () => {
    54. visible.value = !visible.value;
    55. };
    56. //弹幕加速
    57. const addspeeds = () => {
    58. speeds.value += 20;
    59. console.log(speeds.value);
    60. };
    61. //弹幕减速
    62. const jianspeeds = () => {
    63. speeds.value -= 20;
    64. console.log(speeds.value);
    65. };
    66. const getdata = () => {
    67. getComments()
    68. .then(res => {
    69. danmus.value = res.data.map(message => {
    70. return message.commentMessage;
    71. // 返回每个消息的 commentMessage 属性
    72. });
    73. console.log(res.data, "111");
    74. ElMessage({
    75. message: "获取信息成功",
    76. type: "success",
    77. });
    78. })
    79. .catch(err => {
    80. console.log(err, "err");
    81. ElMessage.error("获取信息失败");
    82. });
    83. };
    84. const addComment = () => {
    85. let comment = {
    86. commentMessage: input.value,
    87. };
    88. postComments(comment)
    89. .then(res => {
    90. console.log(res.data, "111");
    91. input.value = " ";
    92. ElMessage({
    93. message: "发布评论成功",
    94. type: "success",
    95. });
    96. })
    97. .catch(err => {
    98. console.log(err, "err");
    99. ElMessage.error("发布失败");
    100. });
    101. getdata();
    102. };
    103. script>
    104. <style scoped>
    105. .big {
    106. position: relative;
    107. }
    108. .backgroundImg {
    109. position: absolute;
    110. height: 30rem;
    111. width: 77rem;
    112. }
    113. .buts {
    114. position: absolute;
    115. right: 50px;
    116. margin-top: 95px;
    117. }
    118. .left {
    119. margin-top: 95px;
    120. }
    121. style>

  • 相关阅读:
    iNFTnews | 元宇宙技术将带来全新的购物体验
    视口 css
    远程控制软件Splashtop的使用
    CentOS7日志文件及journalctl日志查看
    list(链表)
    【RK3399】1.RK3399开发板基础配置
    大数据运维一些常见批量操作命令
    从0-1搭建一个web项目vue3+vite+ts+element-plus(脚手架分析)
    iOS-设置指定边圆角(左上、左下等)
    讲真的!身为一个合格的码农,谁还没碰过索引失效呢
  • 原文地址:https://blog.csdn.net/liu62615/article/details/141091128