Danmaku.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <div class="danmaku-area" @keydown.stop>
  3. <div
  4. class="danmaku-container"
  5. v-chat-scroll
  6. v-if="isShowList"
  7. >
  8. <!-- <ul class=""> -->
  9. <transition-group appear tag="ul" class="danmaku-list" name="dm">
  10. <li
  11. class="danmaku-list-item"
  12. v-for="(item) in showDanmakuData"
  13. :key="item"
  14. >
  15. <span v-html="item"></span>
  16. </li>
  17. </transition-group>
  18. </div>
  19. <div class="input-container" v-if="!isMobile">
  20. <input @click.stop="toggleSelectMenu" v-model="danmu" type="text" placeholder="请选择弹幕内发送吧~" class="send-choices">
  21. <div class="send-btn-container">
  22. <img
  23. @click="hideList"
  24. class="show-icon"
  25. :src="showIcon || ''"
  26. v-if="isShowList"
  27. />
  28. <img
  29. @click="showList"
  30. class="close-icon"
  31. :src="closeIcon || ''"
  32. v-if="!isShowList"
  33. />
  34. <button class="send-btn primary" @click="leaveMsg">发送</button>
  35. </div>
  36. <ul class="show-list" v-show="isShowSelectList">
  37. <li
  38. class="list-item"
  39. v-for="(item, key) in quotes"
  40. :key="key"
  41. @click="sendDanmaku(key)"
  42. v-html="item"
  43. >
  44. </li>
  45. </ul>
  46. </div>
  47. <div class="input-mobile" v-else>
  48. <span class="send-choices" @click.stop="toggleSelectMenu">请选择弹幕内发送吧~</span>
  49. <div class="send-btn-container">
  50. <img
  51. @click="hideList"
  52. class="show-icon"
  53. :src="showIcon || ''"
  54. v-if="isShowList"
  55. />
  56. <img
  57. @click="showList"
  58. class="close-icon"
  59. :src="closeIcon || ''"
  60. v-if="!isShowList"
  61. />
  62. <!-- <button class="send-btn">发送</button> -->
  63. </div>
  64. <ul class="show-list" v-show="isShowSelectList">
  65. <li
  66. class="list-item"
  67. v-for="(item, key) in quotes"
  68. :key="key"
  69. @click="sendDanmaku(key)"
  70. >
  71. {{ item }}
  72. </li>
  73. </ul>
  74. </div>
  75. </div>
  76. </template>
  77. <script>
  78. import { saveBarrage } from "@/config/api";
  79. export default {
  80. name: "danmaku",
  81. props: {
  82. quotes: {
  83. type: Array,
  84. default: function() {
  85. return [
  86. ];
  87. },
  88. },
  89. danmuArr: {
  90. type: Array,
  91. default: function() {
  92. return [
  93. ];
  94. },
  95. },
  96. showIcon: {
  97. type: String,
  98. required: true,
  99. },
  100. closeIcon: {
  101. type: String,
  102. required: true,
  103. },
  104. arrowIcon: {
  105. type: String,
  106. required: true,
  107. },
  108. limit: {
  109. type: Number,
  110. required: false,
  111. default: 5000,
  112. },
  113. },
  114. watch: {
  115. isNotInputAction: function() {},
  116. },
  117. data() {
  118. return {
  119. danmu:'',
  120. showDanmakuData: [],
  121. isShowList: true,
  122. isShowSelectList: false,
  123. isNotInputAction: false,
  124. timer: null,
  125. autoIndex: 0,
  126. isMobile: /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)
  127. };
  128. },
  129. methods: {
  130. toggleSelectMenu() {
  131. this.isShowSelectList = !this.isShowSelectList;
  132. },
  133. showList() {
  134. this.isShowList = true;
  135. },
  136. hideList() {
  137. this.isShowList = false;
  138. },
  139. sendDanmaku(index) {
  140. const text = this.quotes[index];
  141. if (this.showDanmakuData.length <= this.limit) {
  142. this.showDanmakuData.push(text);
  143. }
  144. this.isShowSelectList = false;
  145. this.isNotInputAction = true;
  146. },
  147. sendDanmakuSelf(text){
  148. if (this.showDanmakuData.length <= this.limit) {
  149. this.showDanmakuData.push(text);
  150. }
  151. this.isShowSelectList = false;
  152. this.isNotInputAction = true;
  153. this.danmu=''
  154. },
  155. async leaveMsg(){
  156. let tmp = this.danmu.trim()
  157. if (!tmp) {
  158. return alert('弹幕不能为空')
  159. }
  160. saveBarrage({
  161. content: tmp,
  162. },res=>{
  163. if (res.code==0) {
  164. this.sendDanmakuSelf(tmp)
  165. }
  166. })
  167. },
  168. autoPopAnimation() {
  169. if (!this.isNotInputAction) {
  170. this.timer = setInterval(() => {
  171. if (this.autoIndex <= this.danmuArr.length) {
  172. if (this.danmuArr[this.autoIndex]) {
  173. this.showDanmakuData.push(this.danmuArr[this.autoIndex]);
  174. this.autoIndex++;
  175. } else {
  176. clearInterval(this.timer);
  177. }
  178. }
  179. }, 2000);
  180. }
  181. },
  182. },
  183. mounted() {
  184. this.autoPopAnimation();
  185. },
  186. };
  187. </script>
  188. <style lang="less" scoped>
  189. .danmaku-area {
  190. margin: 0;
  191. }
  192. .danmaku-container {
  193. max-width: 340px;
  194. max-height: 288px;
  195. overflow-x: hidden;
  196. overflow-y: scroll;
  197. }
  198. /* .hidedanmu{
  199. visibility: hidden;
  200. pointer-events: none;
  201. } */
  202. .danmaku-list {
  203. text-decoration: none;
  204. max-width: 342px;
  205. overflow: hidden;
  206. display: flex;
  207. flex-direction: column;
  208. margin: 0;
  209. padding: 0;
  210. }
  211. .danmaku-list li.danmaku-list-item {
  212. margin-bottom: 10px;
  213. padding: 0;
  214. display: block;
  215. overflow: hidden;
  216. white-space: nowrap;
  217. text-overflow: ellipsis;
  218. text-align: left;
  219. }
  220. .danmaku-list li.danmaku-list-item > span {
  221. padding: 0 20px;
  222. line-height: 36px;
  223. font-size: 14px;
  224. display: inline-block;
  225. margin: 0;
  226. color: #fff;
  227. border-radius: 20px;
  228. background: rgba(0, 0, 0, 0.6);
  229. border: 1px solid hsla(0, 0%, 100%, 0.53);
  230. overflow: hidden;
  231. white-space: nowrap;
  232. text-overflow: ellipsis;
  233. text-align: left;
  234. max-width: 290px;
  235. }
  236. .danmaku-list li.danmaku-list-item:nth-last-child(6) {
  237. /* visibility: hidden; */
  238. animation: fadeout 0.3s linear;
  239. }
  240. .input-container {
  241. background: rgba(0, 0, 0, 0.6);
  242. border-radius: 25px;
  243. max-width: 350px;
  244. height: 48px;
  245. display: flex;
  246. justify-content: space-between;
  247. align-items: center;
  248. position: relative;
  249. padding: 10px;
  250. }
  251. .input-container {
  252. >span,>input{
  253. cursor: pointer;
  254. font-size: 14px;
  255. height: 48px;
  256. text-align: left;
  257. line-height: 48px;
  258. padding-left: 10px;
  259. background:none;
  260. outline:none;
  261. border:none;
  262. color: #fff;
  263. &::placeholder{
  264. color: rgb(140, 140, 140);
  265. }
  266. }
  267. }
  268. .input-container > * {
  269. cursor: pointer;
  270. }
  271. .arrow-icon {
  272. margin-right: 8px;
  273. width: 18px;
  274. }
  275. .send-btn {
  276. background: rgb(184, 23, 23);
  277. color: #fff;
  278. padding: 5px 15px;
  279. border: 10px;
  280. outline: 0;
  281. font-size: 16px;
  282. cursor: pointer;
  283. border-radius: 15px;
  284. margin-left: 5px;
  285. }
  286. .send-btn:hover,
  287. .send-btn:focus {
  288. background: rgb(153, 17, 17);
  289. }
  290. .send-btn-container {
  291. display: flex;
  292. align-items: center;
  293. }
  294. .send-btn-container .show-icon,
  295. .send-btn-container .close-icon {
  296. width: 24px;
  297. margin-right: 4px;
  298. }
  299. .input-container .show-list {
  300. text-decoration: none;
  301. position: absolute;
  302. left: 0;
  303. bottom: 64px;
  304. background: rgba(0, 0, 0, 0.7);
  305. width: 340px;
  306. color: #fff;
  307. border-radius: 10px;
  308. max-height: 200px;
  309. overflow-x: hidden;
  310. overflow-y: scroll;
  311. margin: 0;
  312. padding: 0;
  313. }
  314. .input-container .show-list .list-item {
  315. text-align: left;
  316. font-size: 14px;
  317. line-height: 36px;
  318. text-decoration: none;
  319. white-space: nowrap;
  320. text-overflow: ellipsis;
  321. color: #fff;
  322. max-width: 100%;
  323. padding: 0 10px;
  324. overflow: hidden;
  325. }
  326. .input-container .show-list .list-item:hover {
  327. background: rgb(184, 23, 23);
  328. }
  329. .dm-enter {
  330. opacity: 0;
  331. transform: translateY(20px);
  332. }
  333. .dm-leave-to {
  334. opacity: 0;
  335. transform: translateY(-50px);
  336. }
  337. .dm-enter-active,
  338. .dm-leave-active {
  339. transition: all 0.6s ease;
  340. }
  341. .dm-move {
  342. transition: all 0.6s ease;
  343. }
  344. .dm-leave-active {
  345. position: absolute;
  346. }
  347. .input-mobile{
  348. min-width: 213px;
  349. height: 40px;
  350. background: rgba(0, 0, 0, 0.5);
  351. border-radius: 3px;
  352. display: flex;
  353. padding: 10px 10px 10px 20px;
  354. box-sizing: border-box;
  355. position: relative;
  356. justify-content: space-between;
  357. }
  358. .input-mobile .send-choices{
  359. color: #fff;
  360. font-size: 14px;
  361. display: inline-block;
  362. vertical-align: middle;
  363. line-height: 20px;
  364. }
  365. .input-mobile .show-list {
  366. text-decoration: none;
  367. position: absolute;
  368. left: 0;
  369. bottom: 50px;
  370. background: rgba(0, 0, 0, 0.9);
  371. width: 90%;
  372. color: #fff;
  373. border-radius: 3px;
  374. max-height: 200px;
  375. overflow-x: hidden;
  376. overflow-y: scroll;
  377. margin: 0;
  378. padding: 0;
  379. }
  380. .input-mobile .show-list .list-item {
  381. text-align: left;
  382. font-size: 14px;
  383. line-height: 36px;
  384. text-decoration: none;
  385. white-space: nowrap;
  386. text-overflow: ellipsis;
  387. color: #fff;
  388. max-width: 100%;
  389. padding: 0 10px;
  390. overflow: hidden;
  391. }
  392. .input-mobile .show-list .list-item:hover {
  393. background: rgb(184, 23, 23);
  394. }
  395. .input-mobile .send-btn-container{
  396. margin-left: 10px;
  397. }
  398. @keyframes fadeout {
  399. 0% {
  400. opacity: 1;
  401. transform: translateY(10px);
  402. }
  403. 50% {
  404. opacity: 0.7;
  405. transform: translateY(3px);
  406. }
  407. 100% {
  408. opacity: 0.2;
  409. transform: translateY(0px);
  410. }
  411. }
  412. @media only screen and (max-width: 500px), (max-height: 487px) {
  413. .danmaku-list li.danmaku-list-item{
  414. margin-bottom: 5px;
  415. }
  416. .danmaku-list li.danmaku-list-item > span{
  417. border-radius: 3px;
  418. border: 0;
  419. }
  420. }
  421. </style>