h5聊天IM實例|仿微信語音|地圖定位|搖一搖效果

前言

相信你們都有使用過微信,微信這款產品確實設計的不錯,簡約不簡單。尤爲裏面的搖一搖、語音功能更是吸引了大批用戶,顛覆式的功能及設計,讓微信穩穩坐上了社交寶座。css

介紹

以前因爲項目需求有開發過一款簡單的聊天項目,不過功能及UI比較簡單,最近又從新在原先的基礎上進行了一次大的重構,開發了這款仿微信界面聊天IM系統——趣聊weChatIM。html

技術架構/功能

基於html5+css3+Zepto+swiper+wcPop+flex等技術混合開發,實現了發送消息、表情(動圖),圖片、視頻預覽,添加好友/羣聊,右鍵長按菜單。另外新增了語音模塊、地圖定位模塊。總體功能界面效果比較接近微信聊天。html5

介紹了這麼多,一睹效果吧:
圖片描述node

怎麼樣,看了效果圖,是否是感受還不錯,棒棒噠。css3

引入css及js插件以下:chrome

clipboard.png

另外項目中使用的彈窗wcPop.js,是本身開發的移動端彈窗組件,兼容全部移動設備(親測),多種api調用方式及動畫效果;
clipboard.png
clipboard.png
clipboard.png
嗯,效果統一,的確不錯。api

圖片描述

圖片描述

圖片描述

圖片描述

搖一搖功能,基於shake.js插件微信

$("#J__popScreen_shake").on("click", function () {
    var shakePopIdx = wcPop({
        id: 'wcim_shake_fullscreen',
        skin: 'fullscreen',
        title: '搖一搖',
        content: $("#J__popupTmpl-shakeFriends").html(),
        position: 'right',
        xclose: true,
        style: 'background: #303030;',
        show: function(){
            // 搖一搖功能
            var _shake = new Shake({threshold: 15});
            _shake.start();
            window.addEventListener("shake", function(){
                window.navigator.vibrate && navigator.vibrate(500);
                // console.log("觸發搖一搖!");

                //...邏輯代碼
                
                setTimeout(function(){
                    $(".J__shakeLoading").fadeOut(300);
                    $(".J__shakeInfoBox").html(shakeTpl);
                }, 1500);
            }, false);
        }
    });
});

圖片描述

圖片描述

聊天模塊代碼片斷架構

// ...滾動聊天區底部
function wchat_ToBottom() {
    $(".mescroll").animate({ scrollTop: $("#J__chatMsgList").height() }, 0);
}
// ...處理編輯器信息
var $editor = $(".J__wdtEditor"), _editor = $editor[0];
function surrounds() {
    setTimeout(function () { //chrome
        var sel = window.getSelection();
        var anchorNode = sel.anchorNode;
        if (!anchorNode) return;
        if (sel.anchorNode === _editor ||
            (sel.anchorNode.nodeType === 3 && sel.anchorNode.parentNode === _editor)) {

            var range = sel.getRangeAt(0);
            var p = document.createElement("p");
            range.surroundContents(p);
            range.selectNodeContents(p);
            range.insertNode(document.createElement("br")); //chrome
            sel.collapse(p, 0);

            (function clearBr() {
                var elems = [].slice.call(_editor.children);
                for (var i = 0, len = elems.length; i < len; i++) {
                    var el = elems[i];
                    if (el.tagName.toLowerCase() == "br") {
                        _editor.removeChild(el);
                    }
                }
                elems.length = 0;
            })();
        }
    }, 10);
}

編輯器框插入表情處理app

if (that.hasClass("face")) { //小表情
    var img = that[0].cloneNode(true);
    if (!_editor.childNodes.length) {
        _editor.focus();
    }
    _editor.blur(); //輸入表情時禁止輸入法

    setTimeout(function () {
        if (document.selection && document.selection.createRange) {
            document.selection.createRange().pasteHTML(img);
        } else if (window.getSelection && window.getSelection().getRangeAt) {
            range = _rng.getRange();
            range.insertNode(img);
            range.collapse(false);

            _lastRange = range; //記錄當前光標位置 (不然光標會跑到表情前面)
            _rng.addRange();
        }
    }, 10);
}

判斷編輯器內容是否爲空

function isEmpty() {
    var html = $editor.html();
    html = html.replace(/<br[\s\/]{0,2}>/ig, "\r\n");
    html = html.replace(/<[^img].*?>/ig, "");
    html = html.replace(/&nbsp;/ig, "");
    return html.replace(/\r\n|\n|\r/, "").replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, "") == "";
}

圖片描述

圖片描述

圖片描述

圖片描述

圖片描述

地圖模塊,採用的是百度地圖api

// ...選擇位置
var poiPopIdx;
$(".J__chooseWz").on("click", function () {
    poiPopIdx = wcPop({
        id: 'wcim_local_fullscreen',
        title: '位置',
        skin: 'fullscreen',
        content: $("#J__popupTmpl-location").html(),
        position: 'right',
        xclose: true,
        style: 'background: #f3f3f3;',
        // 加載地圖
        show: function () {
            $(".J__sendLatlng").show();
            $(".localMap").html('<iframe id="mapPage" width="100%" height="100%" frameborder=0 src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp"></iframe>');
        }
    });
});
// ...獲取位置座標點
var pointArr;
window.addEventListener('message', function (event) {
    // 接收位置信息,用戶選擇確認位置點後選點組件會觸發該事件,回傳用戶的位置信息
    var loc = event.data;
    // 防止其餘應用也會向該頁面post信息,需判斷module是否爲'locationPicker'
    if (loc && loc.module == 'locationPicker') {
        console.log('location', loc);
        pointArr = loc;
    }
}, false);
相關文章
相關標籤/搜索