• 自己封装js组件


    书接上文,上次弄了个基本版本的alert组件(其实就是十分钟前)但是很多功能都没有实现 没有关闭按钮 没有下面确定按钮 没有模态框 没有这那的 这次终极篇就都给它完善好弄个中级版本也是基本可用版本!

    define(['jquery'],function($){
    function Window(){
    this.cfg = {
    400,
    height:200,
    content:'我是默认文本内容',
    handle:null,
    title:'系统消息',
    skinClassName:null,
    hasCloseBtn:false,
    hasMask:false
    }
    }
    Window.prototype = {
    alert:function(cfg){
    var CFG = $.extend(this.cfg,cfg);
    //var boundingBox = $('<div class="window_boundingBox"></div>');
    var boundingBox = $('<div class="window_boundingBox">'+
    '<div class="window_header">'+CFG.title+'</div>'+
    '<div class="window_body">'+CFG.content+'</div>'+
    '<div class="window_footer"><input type="button" value="确定"></div>'+
    '</div>');

    boundingBox.appendTo('body')

    var btn = $('.window_footer input');

    if(CFG.hasMask){

    mask = $('<div class="window_mask"></div>');
    mask.appendTo('body');

    }
    btn.appendTo(boundingBox);
    btn.click(function(){
    CFG.handle && CFG.handle();
    boundingBox.remove();
    mask && mask.remove();
    })

    boundingBox.css({
    this.cfg.width + 'px',
    height:this.cfg.height + 'px',
    left:(CFG.x || (window.innerWidth - CFG.width)/2)+'px',
    top:(CFG.y || (window.innerHeight - CFG.height)/2)+'px',
    })

    //右上角关闭按钮
    if(CFG.hasCloseBtn){
    var closeBtn = $('<span class="window_closeBtn">X</span>');
    closeBtn.appendTo(boundingBox);
    closeBtn.click(function(){
    boundingBox.remove();
    mask && mask.remove();
    })
    }

    //定制样式
    if(CFG.skinClassName){
    boundingBox.addClass(CFG.skinClassName);
    }
    }
    }
    return {
    Window:Window
    }
    })

    main.js调用

    require(['jquery','window'],function($,w){
    new w.Window().alert({
    500,
    height:300,
    content:'新年快乐',
    title:'我是正确标题',
    hasCloseBtn:true,
    hasMask:true
    })
    })

    靠!靠!靠!直接上代码  对就是这么简单暴力!

  • 相关阅读:
    Android轻量级的开源缓存框架ASimpleCache
    ESP8266学习笔记6:ESP8266规范wifi连接操作
    javascript——正則表達式
    STL经常使用遍历算法for_each和transform的比較
    OpenGL(八)使用 subroutine 切换可编程管线
    (一二〇)CALayer的一些特性
    Android 5.0 怎样正确启用isLoggable(一)__使用具体解释
    Elasticsearch的javaAPI之query dsl-queries
    kettle使用log4j管理输出日志
    YY博客园UML用例图-活动图-状态图之博客模块
  • 原文地址:https://www.cnblogs.com/kongsanpang/p/6229848.html
Copyright © 2020-2023  润新知