3.3 示例:页内iframe形式

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>自定义按钮测试</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="keywords" content="telephone=no">
    <meta name="description" content="自定义按钮测试">
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        body {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
        }

        /* 以下为自定义按钮以及新消息相关样式 */
        .diy-btn {
            width: 90px;
            height: 36px;
            line-height: 36px;
            border: 1px solid #ccc;
            background-color: #a36ceb;
            position: fixed;
            right: .5em;
            bottom: .5em;
            float: right;
            cursor: pointer;
        }
        .diy-btn span {
            display: inline-block;
        }
        .diy-btn .btn-name {
            width: inherit;
            height: inherit;
            line-height: inherit;
            color: #fff;
            font-size: 16px;
            text-align: center;
            display: inline-block;
            float: left;
        }
        .diy-btn .msg-num {
            min-width: 12px;
            height: 12px;
            line-height: 12px;
            border-radius: 12px;
            background-color: #f00;
            display: none;
            position: absolute;
            top: -9px;
            left: -9px;
            font-size: 12px;
            color: #fff;
            text-align: center;
            padding: 3px;
        }
        .msg-unread {
            width: 16em;
            min-height: 3em;
            max-height: 36em;
            position: absolute;
            bottom: 0em;
            right: 10em;
            background-color: #fff;
            -webkit-box-shadow: 0 1px 7px 0 rgba(0,0,0,.25);
            box-shadow: 0 1px 7px 0 rgba(0,0,0,.25);
            border-radius: 3px;
            padding: 1em;
            border: 1px solid #aaa\9;
            text-align: left;
            overflow: auto;
            color: #333;
            font-size: 12px;
            overflow: hidden;
            cursor: auto;
            display: none;
        }
        .msg-head {
            height: 20px;
            line-height: 1;
            color: #a6a6a6
        }
        .msg-head span {
            padding: 0 5px 0 0;
            display: inline-block
        }
        .msg-name,.msg-time {
            max-width: 90px;
            overflow: hidden;
            white-space: nowrap;
            -o-text-overflow: ellipsis;
            text-overflow: ellipsis
        }
        .msg-head .msg-close {
            float: right;
            position: relative;
            top: -10px;
            right: -10px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 700
        }
        .msg-close:hover {
            color: red
        }
        .msg-desc {
            width: 100%;
            max-height: 4.5em;
            color: #323232;
            line-height: 1.5;
            cursor: pointer;
            overflow: hidden;
            word-wrap: break-word;
            word-break: normal;
            -o-text-overflow: ellipsis;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical
        }
    </style>
    <!-- 插入插件代码 -->
    <script type="text/javascript">
        (function (w, d, s, i, v, j, b) {
            w[i] = w[i] || function () {
                (w[i].v = w[i].v || []).push(arguments)
            };
            j = d.createElement(s),
                b = d.getElementsByTagName(s)[0];
            j.async = true;
            j.charset = "UTF-8";
            j.src = "https://www.v5kf.com/[站点id]/v5kf.js";
            b.parentNode.insertBefore(j, b);
        })(window, document, "script", "V5CHAT");
    </script>
</head>
<body>
    <!-- 自定义按钮 -->
    <div class="diy-btn" id="oBtn">
        <span class="btn-name">在线客服</span>
        <span class="msg-num"></span>
        <div class="msg-unread">
            <div class="msg-head">
                <span class="msg-name"></span>
                <span class="msg-time"></span>
                <!-- <span class="msg-close">×</span> -->
            </div>
            <div class="msg-desc"></div>
        </div>
    </div>

    <script type="text/javascript">
        var msg = {
            Num: {
                num: 0,                 // 新消息数 初始为空或0
                time: 500,                 // 闪烁时间间隔
                timer: null,             // 定时器
                isShow: true,             // 是否显示新消息数
                isBlink: true             // 是否闪烁新消息数
            },
            UnRead: {
                time: 8000,             // 显示时间后隐藏
                timer: null,             // 定时器
                isShow: true             // 是否显示最新消息详情
            }
        };

        // 获取对象
        var oBtn = document.querySelector('.diy-btn'),
            oBtnName = document.querySelector('.btn-name'),
            oMsgNum = document.querySelector('.msg-num'),
            oMsgUnRead = document.querySelector('.msg-unread'),
            oMsgName = document.querySelector('.msg-name'),
            oMsgTime = document.querySelector('.msg-time'),
            oMsgDesc = document.querySelector('.msg-desc');

        // 功能实现
        V5CHAT('withoutBtn'); // 自定义按钮时需隐藏原插件中的按钮
        V5CHAT('onPluginLoad', function() {
            // // 自定义iframe样式(移动端全屏显示或PC端居中显示)
            // var style = document.createElement('style');
            // if(/Android|webOS|iPhone|iPad|BlackBerry|mobile|xiaomi/i.test(navigator.userAgent)) {
            //     // 移动端
            //     style.innerText = 'iframe#v5frame{width: 100% !important;height: 100% !important;bottom: 0 !important;left: 0 !important;right: 0 !important;z-index: 2147483640}';
            // } else {
            //     // PC端
            //     style.innerText = 'iframe#v5frame{width: 48em !important;height: 48em !important;bottom: 50% !important;right: 50% !important;margin-bottom: -24em !important;margin-right: -24em !important}';
            // }
            // document.head.appendChild(style);

            V5CHAT('onChatShow', function () { // 对话框显示的回调
                msg.Num.timer && clearTimeout(msg.Num.timer);
                msg.Num.num = 0; // 新消息数初始化
                oMsgNum.innerHTML = '';
                oMsgNum.style.display = 'none'; // 隐藏新消息(数)的提示
                oMsgUnRead.style.display = 'none';
            });

            V5CHAT('onChatHide', function () { // 对话框隐藏的回调
                V5CHAT('onMessage', function (res) { //收到消息的回调
                    if (v5frame.style.display === 'none') {
                        // 更新最新消息详情
                        oMsgName.innerHTML = res.name;
                        oMsgTime.innerHTML = res.time;
                        oMsgDesc.innerHTML = oMsgDesc.title = res.msg;

                        // 最新消息显示 msg.UnRead.time 后会自动隐藏
                        if (msg.UnRead.isShow) {
                            msg.UnRead.timer && clearTimeout(msg.UnRead.timer);
                            oMsgUnRead.style.display = 'block'; // 显示最新消息详情
                            msg.UnRead.timer = setTimeout(function() {
                                oMsgUnRead.style.display = 'none';
                            }, msg.UnRead.time);
                        }

                        // 更新新消息数
                        msg.Num.num++; // 新消息计数
                        oMsgNum.innerHTML = msg.Num.isShow ? (msg.Num.num > 99 ? '99+' : msg.Num.num) : '';
                        oMsgNum.style.display = 'block'; // 显示新消息(数)的提示

                        // 新消息提醒(数)间隔 msg.Num.time 闪烁
                        if (msg.Num.isBlink) {
                            msg.Num.timer && clearTimeout(msg.Num.timer);
                            msg.Num.timer = setInterval(function() {
                                oMsgNum.style.display = oMsgNum.style.display === 'block' ? 'none' : 'block';
                            }, msg.Num.time);
                        }
                    }
                });
            });
        });

        // 点击自定义按钮事件
        oBtn.onclick = function() {
            var oV5Frame = document.querySelector('#v5frame'); // 获取iframe会话框对象
            if (oV5Frame && oV5Frame.style.display === 'block') {
                V5CHAT('hideChat');
            } else {
                V5CHAT('showChat');
            }
        };
    </script>
</body>
</html>

results matching ""

    No results matching ""