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閱讀 230,182評論 6 543
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,489評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,290評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,776評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,510評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,866評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,860評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,036評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,585評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,331評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,536評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,058評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,754評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,154評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,469評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,273評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,505評論 2 379

推薦閱讀更多精彩內容

  • 知識點 知識點匯總鏈接:http://pan.baidu.com/s/1dFFq1mp 密碼:hdvc 程序員英語...
    月老下次記de給我打死結閱讀 4,564評論 3 46
  • 前些日子從@張鑫旭微博處得一份推薦(Front-end-tutorial),號稱最全的資源教程-前端涉及的所有知識...
    谷子多閱讀 4,259評論 0 44
  • tribbie閱讀 267評論 8 7
  • 今早老媽送我走,陪我在路口等車。扭扭捏捏的我終于還是跟老媽擁抱了一個。老媽先是一愣,后來慢慢用手環著我的腰。雖然都...
    ido2閱讀 367評論 2 1
  • 有的時候。 放不下的一段感情。放不下的一個人。 是因為他曾經深刻的出現過在你的生命里。給過你一段難忘的記憶。即使后...
    溫酒煮詞閱讀 295評論 0 0