Danmaku.vue 8.1 KB

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