自己封裝的一個純div+css樣式彈出泡泡訊息框,divcss

來源:互聯網
上載者:User

自己封裝的一個純div+css樣式彈出泡泡訊息框,divcss

    也許很多架構都內建了這樣的彈出泡泡框,但我沒接觸過,現在需要用,於是便自己用jquery封裝了一個,完全div+css構成,相容性好。雖然還不完美,但已經可以用了。這個小控制項可以重定義樣式,通過傳遞配置參數,或修改預設配置參數都可以進行樣式調整。不管怎麼說,就當是練手,或給新人提供一個參考學習的機會也行吧

/*  * 彈出泡泡的預設樣式 */var popMsgDefaultConfig = {    cuspStyle: "topleft",          //相對定位的dom對象方位(topleft,topright,bottomleft,bottomright,leftup,leftdown,rightup,rightdown)    cuspLength: 30,                //尖角長度    cuspWidth: 15,                 //尖角寬度    contentWidth: 160,             //訊息文字框寬度    contentHeight: 60,             //訊息文字框的高度    borderWidth: 2,                //邊框寬度    borderColor: "red",            //邊框顏色    backColor: "#EC90F6",          //背景色    cuspShift: "60%",              //尖角位於訊息內容框上/下時,指左位移,尖角位於訊息內容框左/右時,指的上位移。    contentShift: "20%",           //彈出框位於目標框上/下時,指左位移,彈出框位於目標框左/右時,指的上位移。    textStyle: 'color: blue; font-size:small; font-family:"Microsoft YaHei";font-style:italic;', //預設內容文字樣式    /*      * 設定檔合并      */    Merge: function (newConfig) {        var result = {};        for (var p in this) {            result[p] = this[p];            if (typeof (this[p]) == " function ") {                this[p]();            } else {                for (var q in newConfig) {                    if (p == q) {                        result[p] = newConfig[q];                    }                }            }        }        return result;    }};/*  * 拓展jquery對象的方法; */(function ($) {    /*      * 關閉泡泡的方法     */    $.fn.ClosePopMsg = function () {        var popmsg = $("#" + $(this).attr("id") + "_PopMsg");        if (popmsg) {            popmsg.remove();        }    };    /*      * 彈出泡泡的方法     * text: 需要顯示的訊息內容     * popMsgConfig: 自訂樣式     * Example: $("#divUserName").PopMsg("這是一個提示訊息" { backColor: "gray", borderColor: "white", cuspStyle: "topleft",borderWidth: 1});     */    $.fn.PopMsg = function (text, popMsgConfig) {        var obj = $(this);        var id = obj.attr("id") || Math.random();        obj.attr("id", id);        obj.ClosePopMsg();        var config = popMsgDefaultConfig.Merge(popMsgConfig);        //取消事件冒泡          //event.stopPropagation();        //邊框樣式        var offset = obj.offset();        var popMsg = $("<div id='" + id + "_PopMsg'style='position:fixed;' ></div>");        var popContent = $("<div id='" + id + "_Content' style='vertical-align:middle;text-align:center; line-height: " + config.contentHeight + "px;" + " height: " + config.contentHeight + "px" + "; width: " + config.contentWidth + "px" + "; border: " + config.borderColor + " solid;border-radius: 6px;background-color: " + config.backColor + "; " + config.textStyle + "'>" + text + "</div>");        var popCusp = $("<div id='" + id + "_Cusp' style='width: 0; height: 0; position:relative;'></div>");        var popCuspInner = $("<div id='" + id + "_CuspInner' style='position: absolute;'></div>");        popContent.css({ borderWidth: config.borderWidth + "px", borderColor: config.borderColor, backgroundColor: config.backColor });        popCusp.append(popCuspInner);        popMsg.append(popContent);        if (config.cuspStyle == "topleft") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.left + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.width() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ borderTopWidth: config.cuspLength + "px", borderTopStyle: "solid", borderTopColor: config.borderColor, borderRightWidth: config.cuspWidth + "px", borderRightStyle: "solid", borderRightColor: "transparent", marginTop: "-" + config.borderWidth + "px", marginLeft: config.cuspShift });            popCuspInner.css({ borderTopWidth: innerTop, borderTopStyle: "solid", borderTopColor: config.backColor, borderRightWidth: innerWidth, borderRightStyle: "solid", borderRightColor: "transparent", top: "-" + config.cuspLength + "px", left: config.borderWidth + "px" });            popMsg.css({ top: (offset.top - config.contentHeight - config.cuspLength) + "px", left: msgLeft });            popMsg.append(popCusp);        }        if (config.cuspStyle == "topright") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.left + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.width() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ borderTopWidth: config.cuspLength + "px", borderTopStyle: "solid", borderTopColor: config.borderColor, borderLeftWidth: config.cuspWidth + "px", borderLeftStyle: "solid", borderLeftColor: "transparent", marginTop: "-" + config.borderWidth + "px", marginLeft: config.cuspShift });            popCuspInner.css({ borderTopWidth: innerTop, borderTopStyle: "solid", borderTopColor: config.backColor, borderLeftWidth: innerWidth, borderLeftStyle: "solid", borderLeftColor: "transparent", top: "-" + config.cuspLength + "px", left: "-" + (config.cuspWidth - config.borderWidth) + "px" });            popMsg.css({ top: (offset.top - config.contentHeight - config.cuspLength) + "px", left: msgLeft });            popMsg.append(popCusp);        }        if (config.cuspStyle == "bottomleft") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.left + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.width() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ borderBottomWidth: config.cuspLength + "px", borderBottomStyle: "solid", borderBottomColor: config.borderColor, borderRightWidth: config.cuspWidth + "px", borderRightStyle: "solid", borderRightColor: "transparent", marginTop: "-" + config.borderWidth + "px", marginLeft: config.cuspShift });            popCuspInner.css({ borderBottomWidth: innerTop, borderBottomStyle: "solid", borderBottomColor: config.backColor, borderRightWidth: innerWidth, borderRightStyle: "solid", borderRightColor: "transparent", top: (4 * config.borderWidth) + "px", left: config.borderWidth + "px" });            popMsg.css({ top: (offset.top + obj.height()) + "px", left: msgLeft });            popCusp.insertBefore(popContent);        }        if (config.cuspStyle == "bottomright") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.left + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.width() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ borderBottomWidth: config.cuspLength + "px", borderBottomStyle: "solid", borderBottomColor: config.borderColor, borderLeftWidth: config.cuspWidth + "px", borderLeftStyle: "solid", borderLeftColor: "transparent", marginTop: "-" + config.borderWidth + "px", marginLeft: config.cuspShift });            popCuspInner.css({ borderBottomWidth: innerTop, borderBottomStyle: "solid", borderBottomColor: config.backColor, borderLeftWidth: innerWidth, borderLeftStyle: "solid", borderLeftColor: "transparent", top: (3 * config.borderWidth) + "px", left: "-" + (config.cuspWidth - config.borderWidth) + "px" });            popMsg.css({ top: (offset.top + obj.height()) + "px", left: msgLeft });            popCusp.insertBefore(popContent);        }        if (config.cuspStyle == "leftup") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.top + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.height() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ float: "left", borderTopWidth: config.cuspWidth + "px", borderTopStyle: "solid", borderTopColor: config.borderColor, borderLeftWidth: config.cuspLength + "px", borderLeftStyle: "solid", borderLeftColor: "transparent", marginLeft: "-" + config.borderWidth + "px", marginTop: (config.cuspWidth - 2 * config.borderWidth) + "px" });            popCuspInner.css({ borderTopWidth: innerWidth, borderTopStyle: "solid", borderTopColor: config.backColor, borderLeftWidth: innerTop, borderLeftStyle: "solid", borderLeftColor: "transparent", top: "-" + (config.cuspWidth - config.borderWidth) + "px", left: "-" + (config.cuspLength - 4 * config.borderWidth) + "px" });            popMsg.css({ top: msgLeft, left: (offset.left + obj.width() + config.cuspLength) + "px" });            popContent.css({ float: "right" });            popCusp.insertBefore(popContent);        }        if (config.cuspStyle == "leftdown") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.top + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.height() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ float: "left", borderTopWidth: config.cuspWidth + "px", borderTopStyle: "solid", borderTopColor: config.borderColor, borderLeftWidth: config.cuspLength + "px", borderLeftStyle: "solid", borderLeftColor: "transparent", marginLeft: "-" + config.borderWidth + "px", marginTop: (config.cuspWidth - 2 * config.borderWidth) + "px" });            popCuspInner.css({ borderTopWidth: innerWidth, borderTopStyle: "solid", borderTopColor: config.backColor, borderLeftWidth: innerTop, borderLeftStyle: "solid", borderLeftColor: "transparent", top: "-" + (config.cuspWidth - config.borderWidth) + "px", left: "-" + (config.cuspLength - 4 * config.borderWidth) + "px" });            popMsg.css({ top: msgLeft, left: (offset.left + obj.width() + config.cuspLength) + "px" });            popContent.css({ float: "right" });            popCusp.insertBefore(popContent);        }        if (config.cuspStyle == "rightup") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.top + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.height() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ float: "right", borderTopWidth: config.cuspWidth + "px", borderTopStyle: "solid", borderTopColor: config.borderColor, borderRightWidth: config.cuspLength + "px", borderRightStyle: "solid", borderRightColor: "transparent", marginLeft: "-" + config.borderWidth + "px", marginTop: (config.cuspWidth - 2 * config.borderWidth) + "px" });            popCuspInner.css({ borderTopWidth: innerWidth, borderTopStyle: "solid", borderTopColor: config.backColor, borderRightWidth: innerTop, borderRightStyle: "solid", borderRightColor: "transparent", top: "-" + (config.cuspWidth - config.borderWidth) + "px", left: "-" + config.borderWidth + "px" });            popMsg.css({ top: msgLeft, left: (offset.left - popContent.width() - config.cuspLength) + "px" });            popContent.css({ float: "left" });            popCusp.insertBefore(popContent);        }        if (config.cuspStyle == "rightdown") {            var innerTop = (config.cuspLength - 3 * config.borderWidth) + "px";            var innerWidth = (config.cuspWidth - 2 * config.borderWidth) + "px";            var msgLeft = (offset.top + (config.contentShift.indexOf("px") > 0 ? parseInt(config.contentShift) : obj.height() * parseInt(config.contentShift) / 100)) + "px";            popCusp.css({ float: "right", borderBottomWidth: config.cuspWidth + "px", borderBottomStyle: "solid", borderBottomColor: config.borderColor, borderRightWidth: config.cuspLength + "px", borderRightStyle: "solid", borderRightColor: "transparent", marginLeft: "-" + config.borderWidth + "px", marginTop: (config.cuspWidth - 2 * config.borderWidth) + "px" });            popCuspInner.css({ borderBottomWidth: innerWidth, borderBottomStyle: "solid", borderBottomColor: config.backColor, borderRightWidth: innerTop, borderRightStyle: "solid", borderRightColor: "transparent", top: config.borderWidth + "px", left: "-" + config.borderWidth + "px" });            popMsg.css({ top: msgLeft, left: (offset.left - popContent.width() - config.cuspLength) + "px" });            popContent.css({ float: "left" });            popCusp.insertBefore(popContent);        }        $(document.body).append(popMsg);    }})(jQuery);                            


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.