项目简介 一个轻量化、设置简单、可定制、高度自由化的静态页面MomoTalk实现方案。
在静态页面上实现聊天气泡,并且可以仿照游戏中momotalk的样式制作类似的版本。
Github有类似的MMTK工程,但是都不适用在静态页面内部,而且不方便对页面优化,于是我又自己搓轮子做一个可以即开即用的小功能。
使用Tailwind CSS 和 daisyUi 实现。
按照官网流程安装完之后,就可以实现网页聊天气泡的效果了(也可以用其他的样式)。
开始 添加气泡的CSS样式 首先我们需要写一份能用的CSS样式来供气泡使用。
在./css
中新建chat.css
,然后把这些已经写好的CSS样式复制进去:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 body { font-family : Arial, sans-serif; background-color : #f0f0f0 ; } .chat-container { max-width : 600px ; margin : 0 auto; } .chat { display : flex; align-items : flex-start; margin-bottom : 15px ; } .chat-start { justify-content : flex-start; } .chat-end { justify-content : flex-end; } .chat-image { margin-right : 10px ; } .chat-bubble { position : relative; } .chat-bubble :before { border-color : transparent #4b5a6f transparent transparent; border-style : solid; border-width : 6px ; content : "" ; position : absolute; left : -9px ; top :10px ; } .chat-end .chat-bubble :before { border-color : transparent transparent transparent #4a8ac6 ; border-style : solid; border-width : 6px ; content : "" ; position : absolute; top : 10px ; inset-inline-start : 99% ; } .chat-bubble { color : white; border-radius : 15px ; padding : 10px 15px ; max-width : 100% ; word-wrap : break-word; flex-grow : 0 ; margin-top : 5px ; position : relative; border-bottom-left-radius : 15px ; border-bottom-right-radius : 15px ; } .chat-start .chat-bubble { background-color : #4b5a6f ; border-end-start-radius : 15px ; } .chat-end .chat-bubble { background-color : #4a8ac6 ; border-end-end-radius : 15px ; } .emoji-image { width : 200px ; height : 200px ; display : inline-block; vertical-align : middle; } .chat-image img { width : 60px ; height : 60px ; border-radius : 50% ; object-fit : cover; } .avatar-container { width : 65px ; height : 65px ; border-radius : 50% ; overflow : hidden; } .avatar-container img { width : 100% ; height : 100% ; object-fit : cover; } .chat-header { font-weight : bold; color : black; margin-bottom : 3px ; align-self : flex-start; } .text-container { display : flex; justify-content : center; align-items : center; height : 30px ; } .text-message { color : #474f5d ; font-size : 15px ; text-align : center; background-color : #dce6e8 ; border : 0px solid #dce6e8 ; border-radius : 10px ; padding : 5px 10px ; font-weight : bold; } .text-bubble { display : flex; flex-direction : column; align-items : flex-start; background-color : rgba (255 , 255 , 255 , 0.9 ); background-image : url ('https://bu.dusays.com/2024/10/09/6705649e15f30.jpg' ); background-size : cover; background-position : center; border-radius : 15px ; padding : 15px ; max-width : 520px ; margin : 30px auto; box-sizing : border-box; box-shadow : 0px 3.2px 6.4px 0px rgba (0 , 0 , 0 , 0.3 ); } .separator { margin : 5px 0 ; width : 100% ; border-top : 1px solid #fff ; } .text { color : #474f5d ; font-size : 20px ; text-align : left; font-weight : bold; margin-bottom : 5px ; } .button { background-color : #fc879a ; color : white; font-weight : bold; border : none; border-radius : 5px ; padding : 15px ; cursor : pointer; transition : background-color 0.3s ; width : 100% ; box-sizing : border-box; box-shadow : 0px 0.6px 1.6px 0px rgba (0 , 0 , 0 , 0.4 ); } .button :hover { background-color : #fc748a ; } .button :active { background-color : #f1304f ; }
引用自定义css 然后我们需要在主题的head文件中引用这段自定义的css,找到./themes/anzhiyu/layout/**/head.pug
(我的主题文件用的是pug格式),找到合适的位置引用
1 2 3 4 5 6 《此处省略上部分内容,只需要找到link那一块加上就可以了》 //- 在这里引入 Tailwind CSS link(href='/css/output.css' rel='stylesheet') //- 引入聊天气泡样式 link(href='/css/chat.css' rel='stylesheet')
如何使用? 整体结构 1 2 3 <div class ="chat-container" > ... </div >
<div class="chat-container">
这是整个聊天气泡的容器,用于包裹所有的聊天消息。它通常用于设置最大宽度、居中显示等样式。
左侧聊天消息 1 2 3 4 5 6 7 8 9 10 11 <div class ="chat chat-start" > <div class ="chat-image" > <img src ="example.jpg" alt ="头像" > </div > <div > <div class ="chat-header" > nickname</div > <div class ="chat-bubble" > 在这里填写内容~ </div > </div > </div >
<div class="chat chat-start">
这是左侧聊天消息的容器。
chat-start
表示这是左侧消息,通常用于显示用户发送的消息。
当你需要修改头像的时候只需要更改里面的example.jpg就行了。
如果对头像大小不满意可以调整 ./css/chat.css
文件中的.avatar-container
选项,把固定宽度和高度改一下就好。
<img src="...">
: 头像图片,src
属性设置头像的 URL,alt
属性用于描述头像的内容,便于 SEO 和可访问性。
同理,nickname
也可以替换掉成你想要的昵称
使用过程中一定要注意格式!不然整个页面的排版都会崩掉!
聊天内容
1 2 3 4 5 6 <div > <div class ="chat-header" > shrimp</div > <div class ="chat-bubble" > I Love You. </div > </div >
<div>
: 用于包裹昵称和消息内容的容器。
<div class="chat-header">
: 昵称的容器,这里是“shrimp”。
<div class="chat-bubble">
: 聊天气泡的容器,这里显示的是消息内容“I Love You.”。
发送包含表情的信息
1 2 3 4 5 6 7 8 9 10 11 <div class ="chat chat-start" > <div class ="avatar-container" > <img src ="https://bu.dusays.com/2024/10/04/66ffaa05a654c.jpg" alt ="头像" > </div > <div > <div class ="chat-header" > shrimp</div > <div class ="chat-bubble" > <img src ="https://bu.dusays.com/2024/10/08/670545dbc02a9.png" alt ="表情" class ="emoji-image" > </div > </div > </div >
这部分的结构与第一条消息相似,但它的 chat-bubble 中包含一个表情图片。<img src="...">
: 表情图片,通过 class=”emoji-image” 可以为这个图片应用特定的样式。
右侧聊天消息
1 2 3 4 5 6 7 8 <div class ="chat chat-end" > <div class ="chat-bubble" > I Love you too. </div > <div class ="avatar-container" > <img src ="https://bu.dusays.com/2024/10/04/66ffaa05a654c.jpg" alt ="头像" > </div > </div >
<div class="chat chat-end">
: 这是右侧聊天消息的容器,chat-end 表示这是右侧消息,通常用于显示接收的消息。
使用旁白描述
1 2 3 <div class ="text-container" > <div class ="text-message" > 在这里填旁白和描述文本</div > </div >
使用羁绊剧情按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <div class ="text-bubble" > <div class ="text" > 在这里改气泡的描述</div > <div class ="separator" > </div > <button class ="button" onclick ="yourFunction()" > 在这里更改按钮文本</button > </div > <script > function yourFunction ( ) { alert ('写你想要提示的文本' ); } </script > <script > function yourFunction ( ) { window .location .href = "https://example.com" ; } </script >
<script>
里面是使用JavaScript实现按钮点击的特殊事件,比如:提醒,跳转,弹出文本图片等。
当然你也可以根据需求删改相关的选项,比如说右边气泡可以去掉头像仿照MMTK效果
通过上述方式实现的所有小功能
然后就可以利用这个功能来实现沉浸式故事啦~
目前这个功能可以用在很多地方,比如说作为故事的引子,通过mmtk引导读者前往另一篇故事来实现更强的沉浸感。
当然,你也可以把他扔到其他文本里单独当装饰,也是非常不错的选择。
关于能不能通过动画来播放?目前来说有点复杂,而且排版会错位,后期有精力我会研究下并更新这个文章。
完整代码 你可以通过这段代码实现默认效果,或者复制其中的一部分来省事,但一定要注意结构!
如果发现文本是往上走的记得把头像class换成avatar-container
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <div class ="text-container" > <div class ="text-message" > 似乎过了很久很久...</div > </div > <div class ="chat-container" > <div class ="chat chat-start" > <div class ="avatar-container" > <img src ="https://bu.dusays.com/2024/10/09/67064a3a44cab.webp" alt ="头像" > </div > <div > <div class ="chat-header" > 时雨</div > <div class ="chat-bubble" > 我爱你。 </div > </div > </div > <div class ="chat chat-start" > <div class ="avatar-container" > <img src ="https://bu.dusays.com/2024/10/09/67064a3a44cab.webp" alt ="头像" > </div > <div > <div class ="chat-header" > 时雨</div > <div class ="chat-bubble" > <img src ="https://bu.dusays.com/2024/10/08/670545dbc02a9.png" alt ="表情" class ="emoji-image" > </div > </div > </div > <div class ="chat chat-end" > <div class ="chat-bubble" > 我也爱你口牙! </div > <div class ="avatar-container" > <img src ="https://bu.dusays.com/2024/10/04/66ffaa05a654c.jpg" alt ="头像" > </div > </div > </div > <div class ="text-bubble" > <div class ="text" > | 羁绊事件</div > <div class ="separator" > </div > <button class ="button" onclick ="yourFunction()" > 时雨的羁绊剧情</button > </div > <script > function yourFunction ( ) { alert ('诶诶,剧情去哪里啦?还没写呢~' ); } </script >
By TLSLime 2024.10.9