electron製作聊天界面(仿製qq)

效果圖:圖片描述

樣式使用scss和flex佈局

這也是製作IM系統的最後一個界面了!
在製作之前參考了qq和千牛

需要注意的點

qq將滾動條美化了 而且在無操作的情況下是不會顯示的

滾動條美化

::-webkit-scrollbar { /*滾動條整體樣式*/
    width: 5px; /*高寬分別對應橫豎滾動條的尺寸*/
    height: 1px;
}

::-webkit-scrollbar-thumb { /*滾動條裏面小方塊*/
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 5px rgba(228, 57, 60, 0.2);
    background: rgba(20, 20, 50, 0.6);
    position: absolute;
}

::-webkit-scrollbar-track { /*滾動條裏面軌道*/
    -webkit-box-shadow: inset 0 0 5px rgba(228, 57, 60, 0.2);
    border-radius: 10px;
    background: #EDEDED;
    position: absolute;
}

滾動條根據時機顯示

其實這個也很簡單 用的mouseentermouseleave事件

<div
    :style="{overflowY:messageScroll? 'auto' : 'hidden',paddingRight: messageScroll ? '0': '5px' }"
    @mouseenter="showMessageScrolls" 
    @mouseleave="hideMessageScrolls">
</div>

# script
 showMessageScrolls(){
     this.messageScroll = true;
},
hideMessageScrolls(){
    this.messageScroll = false;
},

這裏解釋一下爲什麼有一個paddingRight
因爲我們的滾動條是5px 如果不加 在滾動條顯示的時候頁面會抖動

簡單寫法

    @mouseenter="messageScroll = true" 
    @mouseleave="messageScroll = false"

頁面滾動

頁面打開的時候消息列表滾動到底部

this.$nextTick(function () {
                this.$refs.msgBox.scrollTop = this.$refs.msgBox.scrollHeight
})

消息發送滾動到底部

 this.$refs.msgBox.scrollTop = this.$refs.msgBox.scrollHeight;

內容編輯

沒有使用表單元素 直接使用的 contenteditable
因爲contenteditable 沒法用雙向數據綁定 不過 可以用數據偵聽器 有很多辦法 但是有很簡單的 使用input事件就行了

代碼

頁面代碼

<template>
    <div class="friend_window">
        <header>
            <div class="nickname">Lee</div>
            <div class="buttons">
                <i class="iconfont">&#xe669;</i>
                <i class="iconfont">&#xe601;</i>
            </div>
        </header>
        <aside>
            <nav>
                <ul>
                    <li >
                        <div class="avatar"><img src="@/assets/img/1.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天-</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                    <li >
                        <div class="avatar"><img src="@/assets/img/2.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天-</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                    <li >
                        <div class="avatar"><img src="@/assets/img/3.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天-</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                    <li >
                        <div class="avatar"><img src="@/assets/img/4.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天-</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                    <li class="active">
                        <div class="avatar"><img src="@/assets/img/5.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天1-</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                    <li >
                        <div class="avatar"><img src="@/assets/img/6.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天-</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                    <li >
                        <div class="avatar"><img src="@/assets/img/7.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                    <li >
                        <div class="avatar"><img src="@/assets/img/8.jpg" alt=""></div>
                        <div class="msg_box">
                            <div class="nickname">李昊天-</div>
                            <div class="messages">最近還好嗎</div>
                        </div>
                        <div class="push_right">
                            <div class="time">12:50</div>
                            <div class="number">1</div>
                        </div>
                    </li>
                </ul>
            </nav>
            <main>
                <div
                        class="message_main"
                        ref="ele"
                        :style="{overflowY:messageScroll? 'auto' : 'hidden',paddingRight: messageScroll ? '0': '5px' }"
                        @mouseenter="showMessageScrolls" @mouseleave="hideMessageScrolls"
                >
                    <div class="mes_box" v-for="(item,index) in list" :class="{'me' : index % 2 === 0}">
                        <div class="avatar">
                            <img src="@/assets/img/5.jpg" alt="">
                        </div>
                        <div class="message_box">
                            {{item.msg}}
                        </div>
                    </div>
                </div>
                <div class="input_box">
                    <div class="menubar">
                        <svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-biaoqing-weixiao"></use>
                        </svg>
                        <svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-folder"></use>
                        </svg>
                        <svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-tupian1"></use>
                        </svg>
                        <svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-shuangsechangyongtubiao-"></use>
                        </svg>
                    </div>
                    <div class="input" ref="input" contenteditable="true" @keydown.enter="sendMsg" @change="inputMsg"
                         @input="inputMsg"></div>
                    <div class="footerbar">
                        <Button>關閉</Button>
                        <Button type="primary">發送</Button>
                    </div>
                </div>
            </main>
        </aside>
    </div>
</template>

script代碼

<script>
    import '@/assets/css/scrool.css'
    import '@/assets/fonts/iconfont.js';

    export default {
        name: "friend",
        data() {
            return {
                list: [
                    {msg: '趙客縵胡纓,吳鉤霜雪明'},
                    {msg: '銀鞍照白馬,颯沓如流星'},
                    {msg: '十步殺一人,千里不留行'},
                    {msg: '事了拂衣去,深藏身與名'},
                    {msg: '閒過信陵飲,脫劍膝前橫。'},
                    {msg: '將炙啖朱亥,持觴勸侯嬴。'},
                    {msg: '三杯吐然諾,五嶽倒爲輕'},
                    {msg: '眼花耳熱後,意氣素霓生。'},
                    {msg: '救趙揮金槌,邯鄲先震驚。'},
                    {msg: '千秋二壯士,烜赫大梁城。'},
                    {msg: '縱死俠骨香,不慚世上英。'},
                    {msg: '誰能書閣下,白首太玄經。'},
                    {msg: '是唐代大詩人李白借樂府古題創作的一首詩。此詩開頭四句從俠客的裝束、兵刃、坐騎刻畫俠客的形象;第二個四句描寫俠客高超的武術和淡泊名利的行藏;第三個四句引入信'},
                ],
                msg: '',
                number:8,
                messageScroll:false
            }
        },
        mounted() {
            this.$nextTick(function () {
                this.$refs.ele.scrollTop = this.$refs.ele.scrollHeight
            })
        },

        methods: {
            showMessageScrolls(){
                this.messageScroll = true;
            },
            hideMessageScrolls(){
                this.messageScroll = false;
            },
            inputMsg(e) {
                this.msg = e.target.innerHTML;
            },
            sendMsg(e) {
                this.list.push({msg: this.msg});
                this.msg = '';
                this.$refs.input.innerHTML = '';
                setTimeout(() => {
                    this.$refs.ele.scrollTop = this.$refs.ele.scrollHeight;
                }, 200);
                e.preventDefault();
            }
        }
    }
</script>

樣式代碼

.friend_window {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("../img/main_1.jpg");
  border-radius: 4px;
  -webkit-user-select: none;
  background-size: 100% 100%;

  header {
    height: 40px;
    background-color: rgba(0, 0, 0, 0.3);
    -webkit-app-region: drag;
    border-radius: 4px 4px 0 0;
    display: flex;
    justify-content: space-between;

    .nickname {
      color: #FFF;
      line-height: 40px;
      font-size: 20px;
      margin: auto;
      padding-left: 40px
    }

    .buttons {
      i {
        display: inline-block;
        color: #FFF;
        width: 40px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        cursor: pointer;
        -webkit-app-region: no-drag;

        &:hover {
          background-color: rgba(255, 255, 255, 0.3);
        }
      }
    }
  }

  aside {
    height: calc(100% - 40px);
    border-radius: 0 0 4px 4px;
    display: flex;
  }

  nav {
    width: 240px;
    position: relative;

    background-size: 100% 100%;
    overflow-y: auto;

    &:after {
      display: inline-block;
      content: '';
      width: 5px;
      cursor: e-resize;
      position: absolute;
      right: -2px;
      top: 0;
      height: 100%;
    }

    ul {
      li.active {
        background-color: rgba(255, 255, 255, 0.2);
      }
      li {
        list-style: none;
        height: 60px;
        padding-left: 10px;
        cursor: pointer;
        display: flex;
        overflow: hidden;
        align-items: flex-start;

        &:hover {
          background-color: rgba(255, 255, 255, 0.2);
        }

        .push_right {
          padding-right: 10px;
          text-align: center;
          align-self: center;

          .time {
            font-size: 13px;
            color: #CFD3DA;
          }

          .number {
            display: inline-block;
            background-color: #e4393c;
            color: #fff;
            min-width: 15px;
            min-height: 15px;
            padding: 0 2px;
            line-height: 15px;
            border-radius: 50%;
            text-align: center;
            font-size: 12px;
          }
        }

        .msg_box {
          align-self: center;
          flex: 1;
          color: #EFF1F3;

          .messages {
            color: #CFD3DA;
          }
        }

        .avatar {
          width: 45px;
          height: 45px;
          align-self: center;
          margin-right: 10px;

          img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
        }
      }
    }
  }

  main {
    background-color: #fff;
    width: calc(100% - 240px);
    border-radius: 0 0 4px 0;

    .message_main {
      height: calc(100% - 35%);
      overflow-y: auto;

      &::-webkit-scrollbar {
        display: block !important;
      }

      .mes_box {
        display: flex;
        margin-bottom: 10px;
        margin-top: 10px;
        padding: 10px;

        .avatar {
          width: 40px;
          height: 40px;
          margin-right: 10px;

          img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
        }

        .message_box {
          background-color: #FFFFFF;
          color: #333;
          padding: 10px;
          border-radius: 5px;
          max-width: 72%;
          position: relative;
          border: 1px solid #D4D4D4;

          &::before {
            content: '';
            display: block;
            position: absolute;
            width: 10px;
            height: 10px;
            border: 1px solid #D4D4D4;
            border-right: none;
            border-top: none;
            background-color: #FFFFFF;
            border-radius: 3px;
            transform: rotate(44deg);
            left: -6px;
            top: 14px;
          }
        }
      }

      .me {
        display: flex;
        justify-content: flex-end;

        .message_box {
          background-color: #A0E759;
          color: #333;
          border: 1px solid #77BF41;

          &::before {
            display: none;
          }

          &::after {
            content: '';
            display: block;
            position: absolute;
            width: 10px;
            height: 10px;
            border: 1px solid #77BF41;
            border-bottom: none;
            border-left: none;
            border-radius: 3px;
            background-color: #A0E759;
            transform: rotate(45deg);
            right: -6px;
            top: 14px;
          }
        }

        .avatar {
          order: 2;
          margin-left: 10px;
        }
      }
    }

    .input_box {
      border-top: 1px solid #ccc;
      height: calc(100% - 65%);

      .menubar {
        height: 30px;
        width: 100%;
        display: flex;
        align-items: center;

        .icon {
          display: inline-block;
          padding: 2px;
          width: 25px;
          height: 25px;
          cursor: pointer;
          margin-right: 5px;
          margin-left: 5px;

          &:hover {
            background-color: rgba(0, 0, 0, 0.1);
          }
        }
      }

      .footerbar {
        display: flex;
        height: 70px;
        align-items: center;
        justify-content: flex-end;
        padding-right: 20px;

        button {
          margin: 0 10px;
          padding-left: 30px;
          padding-right: 30px;
        }
      }

      .input {
        font-size: 16px;
        padding: 4px 8px;
        overflow-y: auto;
        height: calc(100% - 70px - 30px);

        background-color: #fff;

        &::-webkit-scrollbar {
          display: block !important;
        }
      }
    }
  }
}

.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}

聲明

代碼只爲學習使用,如果有個人或者機構使用該代碼帶來的侵權行爲,與本人無關
如果代碼有不合理之處請大家提出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章