jquery—components.prototype把经常复用的东西提出来

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>prototype</title>
    <link rel="stylesheet" href="component.css">
    <script src="jquery-2.1.4.min.js"></script>
    <script src="components.js"></script>
</head>
<body>
    <script>
        var myDialog=new components();
        myDialog.myDialog('myDialog');
        myDialog.myToast('myToast',1000);
        myDialog.myPopup('myPopup',1000);

        $(window).on('mousedown',function(e){
            touchRun(e);
            $(window).on('mousemove',function(e) {
                touchRun(e);
            });
        })
        $(window).on('mouseup',function(){
            $(window).off('mousemove');
            $('#myTouch').remove();
        });

        function touchRun(e){
            e = e || window.event;
            x = e.pageX || e.clientX + document.body.scroolLeft;
            y = e.pageY || e.clientY + document.body.scrollTop;
            myDialog.myTouch('pink',x,y,'myTouch');
        }
    </script>
</body>
</html>

js:

function components() {}

components.prototype={
    //dialog
    myDialog:function (message) {
        var myDialogLen=$("#myDialog").length;
        if(!myDialogLen) {   //如果界面上没有这个弹窗,就拼接
            var dialogString='';
            dialogString+='<div id="myDialog" class="top-wrapper">';
            dialogString+='<div class="c-dialog">';
            dialogString+='<div class="dialog-mess">'+message+'</div>';
            dialogString+='<div class="dialog-btn">确定</div>';
            dialogString+='</div>';
            dialogString+='</div>';
            $('body').append(dialogString);
        }

        $(".dialog-btn").on('click',function() {
            $("#myDialog").remove();
        });

    },
    //toast
    myToast:function (message,time) {
        var toast=$('#myToast').length;
        if(!toast) {   //如果界面上没有这个弹窗,就拼接
            var toastString = '';
            toastString += '<div id="myToast">';
            toastString += '<div class="c-toast">';
            toastString += '<div class="toast-mess">' + message + '</div>';
            toastString += '</div>';
            toastString += '</div>';
            $('body').append(toastString);

        }
       //如果界面上有这个弹窗,就显示隐藏
        $('.toast-mess').html(message);
        $('#myToast').fadeIn();
        setTimeout("$('#myToast').fadeOut()",time)
    },

    //白弹窗
    myPopup:function (message,time) {
        var popup=$('#myPopup').length;
        if(!popup) {
            var popupString = '';
            popupString += '<div id="myPopup">';
            popupString += '<div class="c-popup">';
            popupString += '<div class="popup-img">';
            popupString += '<img src="../assets/components/images/success.png"/>';
            popupString += '</div>';
            popupString += '<div class="popup-mess">' + message + '</div>';
            popupString += '</div>';
            popupString += '</div>';
            $('body').append(popupString);

        }
        $('.popup-mess').html(message);
        $('#myPopup').fadeIn();
        setTimeout("$('#myPopup').fadeOut()",time)
    },

    // touch
    myTouch:function(color,pageX,pageY,uName){
        var touch=$('#myTouch').length;
        if(!touch){
            touchStyle = '<style>@keyframes warn{0%{transform:scale(0);opacity:0}25%{transform:scale(0);opacity:.1}50%{transform:scale(.1);opacity:.3}75%{transform:scale(.5);opacity:.6}100%{transform:scale(1);opacity:0}}@-webkit-keyframes warn{0%{-webkit-transform:scale(0);opacity:0}25%{-webkit-transform:scale(0);opacity:.1}50%{-webkit-transform:scale(.1);opacity:.3}75%{-webkit-transform:scale(.5);opacity:.6}100%{-webkit-transform:scale(1);opacity:0}}</style>'
            $($('head')[0]).append(touchStyle);
            var touchString = '';
            touchString += '<div id="myTouch">';
            touchString += '<div class="touch-box" style="position: absolute;">';
            touchString += '<div class="touch-circle" style="position: absolute; top:0; left:0; z-index: 999;">';
            touchString += '<div class="touch-circle-dot" style="position: absolute; width:20px; height:20px; border-radius: 50%; background: #4077bd; box-shadow:0 0 5px 2px #4077bd; left: 0; top: 0;  "></div>';
            touchString += '<div class="touch-circle-pulse" style="position: absolute; width: 60px; height: 60px; left: -20px; top: -20px; opacity: 0;border-radius: 50%; background: #4077bd; -webkit-animation: warn 1s ease-out; -moz-animation: warn 1s ease-out; animation: warn 1s ease-out; -webkit-animation-iteration-count: infinite; -moz-animation-iteration-count: infinite; animation-iteration-count: infinite;  "></div>';
            touchString += '<div class="touch-circle-big" style="position:absolute;width:50px;height:50px;border-radius:50%;border:2px solid #4077bd; left:-18px;top:-18px"></div>';
            touchString += '</div>';
            touchString += '<div class="touch-name" style="position: absolute; bottom:10px; left:25px; width:50px; height:20px; color:#fff; background:#4077bd; border-radius: 3px; line-height: 20px; text-align: center; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'+ uName +'</div>';
            touchString += '</div>';
            touchString += '</div>';
            $('body').append(touchString);
        }
        $('#myTouch .touch-box').show();
        $('.touch-box .touch-name').css('background',color);
        $('.touch-box .touch-circle-dot').css('background',color);
        $('.touch-box .touch-circle-dot').css('box-shadow','0 0 5px 2px '+ color);
        $('.touch-box .touch-circle-big').css('border-color',color);
        $('.touch-box .touch-circle-pulse').css('background',color);
        $('.touch-box').css('left',pageX);
        $('.touch-box').css('top',pageY);
        $('.touch-box .touch-name').html(uName);
    }

}

css:

/********** component **********/
/* topWrapper: the full screen transparent backgrond*/
.top-wrapper{
    top:0;
    position:absolute;
    z-index:11;
    width:100%;
    height:100%;
    background:rgba(0,0,0,0.2);
    color:#333;
}

/* dialog */
.c-dialog{
    position:absolute;
    top:50%;
    left:50%;
    padding:0;
    width:280px;
    height:140px;
    border-radius:3px;
    background:#fff;
    font-size:16px;
    -webkit-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
}

/* toast */
.c-toast{
    position:absolute;
    top:50%;
    left:50%;
    padding:20px 20px;
    width:180px;
    line-height:20px;
    border-radius:3px;
    background:rgba(0,0,0,0.5);
    box-shadow:0 0 2px 1px #333;
    font-size:16px;
    text-align:center;
    color: #fff;
    -webkit-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
    z-index: 20;
}
.c-dialog .dialog-mess{
    padding:35px 20px;
    width:auto;
    height:auto;
    text-align:left;
    line-height:20px;
}
.c-dialog .dialog-btn{
    position: absolute;
    bottom: 10px;
    right: 10px;
    padding:0;
    width: 70px;
    height: 30px;
    line-height: 30px;
    color: #4170b4;
    text-align: center;
    border-radius: 3px;
}
.c-dialog .dialog-btn:active{
    background:#eee;
}

/* myPopup */
.c-popup{
    position:absolute;
    top:50%;
    left:50%;
    width:320px;
    height:150px;
    border-radius:5px;
    background:#fff;
    font-size:16px;
    text-align:center;
    color: #fff;
    -webkit-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
    z-index: 20;
}
.c-popup .popup-mess{
    color:#404040;
    font-size: 20px;
}
.c-popup .popup-img{
    width:70px;
    height:70px;
    border-radius: 50%;
    overflow: hidden;
    margin:20px auto 10px;
}
.c-popup .popup-img>img{
    width:100%;
    height:100%;
}

代码链接:
https://pan.baidu.com/s/1bGKCUU

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,511评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,495评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,595评论 0 225
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,558评论 0 190
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,715评论 3 270
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,672评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,112评论 2 291
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,837评论 0 181
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,417评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,928评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,316评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,773评论 2 234
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,253评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,827评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,440评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,523评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,583评论 2 249

推荐阅读更多精彩内容