`
japankn
  • 浏览: 217804 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

自己写遮盖的心得

阅读更多

    近来项目中为了使用Ajax技术,需要一个友好的遮盖层效果的等待画面,本人比较懒,没有使用一些ajax框架,自己动手写了一个遮盖层。

    其中遇到几个比较难搞定的问题:

    1、select这个优先级比较高的页面控件

    2、兼容Mozilla

    3、消息框的自动居中(起码看起来比较舒服)

 

    从网络上搜索了一堆解决办法,比较适用的有:

    1、使用比select优先级更高的遮住他(还有一种是将它隐藏,不喜欢),我只用Iframe,效果不错!

    2、兼容Mozilla主要是支持Mozilla的css样式(网上搜)

    3、从网上摘抄了一段css代码(呵呵)

 

    这样主要的问题解决了。

    我的所有样式都写在了js中,这样免去了每个页面的导入css文件和创建占位的div。样例如下:

 

页面代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

    //注意下面这句是针对Firefox的,如果不写要报错(也能运行)
    <meta http-equiv="Content-Type" content="application/vnd.mozilla.maybe.feed; charset=utf-8" />
    <title></title>
    <link href="02_.css" type="text/css" rel="stylesheet">
    <script type="text/javascript" src="tool.js"></script>
    <script type="text/javascript" src="02_.js"></script>
</head>
<body>
<input type="button" value="显示" onclick="new MesDiv().dostart('标题内容','提示内容',true)">
<select>
    <option>select也能被盖住</option>
</select><br>
</body>
</html>

 

js代码

var bad,mes;
function MesDiv() {
    this.isIE = document.all ? true : false;
    this.init = function(title, content, bl) {
        if (!bad || !mes) {
            bad = this.baddingDiv();
            mes = this.mesDiv(title, content, bl);
            document.body.appendChild(bad);
            document.body.appendChild(mes);
        }
    }
    //底层,作为全屏的铺垫
    this.baddingDiv = function() {
        var d = createObject("div");
        var ifr = createObject("iframe");
        d.style.cssText = "position:absolute;width:100%;height:100%;" +
                          "background:#999999;left:0px;right:0px;top:0px;" +
                          "bottom:0px;-moz-opacity:0;filter:alpha(opacity = 0);" +
                          "z-index: 99;";
        ifr.style.cssText = "position:absolute;width:100%;height:100%;" +
                            "background:#CCCCCC;left:0px;right:0px;top:0px;" +
                            "bottom:0px;-moz-opacity:0;" +
                            "z-index: 96;";
        ifr.frameBorder = 0;
        d.appendChild(ifr);
        return d;
    }
    /*
    * 显示消息的层
    * title 标题内容
    * content 提示内容
    * bl是否有关闭事件及按钮,true为有
    */
    this.mesDiv = function(title, content, bl) {
        //消息框的外层
        var d1 = createObject("div");
        d1.style.cssText = "position: absolute;margin:-150px 0 0 -200px;z-index:100;" +
                           "top:50%;left:50%; width:400px;background-color: #FFFFFF;" +
                           "-moz-opacity:0.5;filter:alpha(opacity = 50);";
        //标题层
        var d2 = createObject("div");
        d2.style.cssText = "height:20px;padding-top:8px;background-color: #DDDDDD;";
        var d2_div1 = createObject("div");
        d2_div1.style.cssText = "float:left;width:290px;padding-left:20px;overflow:hidden;word-break:keep-all; text-overflow:ellipsis";
        d2_div1.appendChild(createText(title || "aaaaa"));
        d2.appendChild(d2_div1);
        if (bl || false) {
            var d2_div2 = createObject("div");
            d2_div2.style.cssText = "float:right;width:80px;text-align:right;padding-right:10px;";
            var d2_div2_span = createObject("span");
            d2_div2_span.onclick = function() {
                new MesDiv().showBackground(bad, 0,false);
    new MesDiv().showBackground(mes, 50,false);
            }
            d2_div2_span.innerHTML = "&times;"
            d2_div2.appendChild(d2_div2_span);
            d2.appendChild(d2_div2);
        }
        //内容层
        var d3 = createObject("div");
        d3.style.cssText = "border-top: #AAAAAA; border-top-style:solid;" +
                           "border-top-width:1px;height:100px;text-align:center;";
        var d3_div1 = createObject("div");
        d3_div1.style.cssText = "padding-top:20px;";
        var d3_div1_img = createObject("img");
        d3_div1_img.src = "warn.png";
        d3_div1_img.align = "absmiddle";
        d3_div1.appendChild(d3_div1_img);
        d3_div1.appendChild(createText(content || "bbbbbb"));
  d3.appendChild(d3_div1);
        if (!bl || false) {
            var d3_div2 = createObject("div");
            d3_div2.style.cssText = "padding-top:10px;";
            var d3_div2_img = createObject("img");
            d3_div2_img.src = "load.gif";
            d3_div2.appendChild(d3_div2_img);
            d3.appendChild(d3_div2);
        }
        d1.appendChild(d2);
        d1.appendChild(d3);
        return d1;
    }
    this.dostart = function(title, content, bl) {
        this.init(title, content, bl);
        bad.style.display = "block";
        mes.style.display = "block";
        this.showBackground(bad, 60,true);
        this.showBackground(mes, 100,true);
    }
    this.doend = function() {
        this.init();
        this.showBackground(bad, 0,false);
  this.showBackground(mes, 50,false);
    }
    //让背景渐渐变暗
    this.showBackground = function(obj, endInt,bl)
    {
        if (this.isIE)
        {
   if(bl){
    obj.filters.alpha.opacity += 2;
    if (obj.filters.alpha.opacity < endInt){
     setTimeout(function(){new MesDiv().showBackground(obj, endInt,bl)}, 5);
    }
   }else{
    obj.filters.alpha.opacity -= 2;
    if (obj.filters.alpha.opacity > endInt){
     setTimeout(function(){new MesDiv().showBackground(obj, endInt,bl)}, 5);
    }else{
     obj.style.display = "none";
    }
   }
        }
        else {
   if(bl){
    var al = parseFloat(obj.style.opacity);
    al += 0.02;
    obj.style.opacity = al;
    if (al < (endInt / 100)){
     setTimeout(function(){new MesDiv().showBackground(obj, endInt,bl)}, 5);
    }
   }else{
    var al = parseFloat(obj.style.opacity);
    al -= 0.02;
    obj.style.opacity = al;
    if (al > (endInt / 100)){
     setTimeout(function(){new MesDiv().showBackground(obj, endInt,bl)}, 5);
    }else{
     obj.style.display = "none";
    }
   }
        }
    }
}

 

    其中有一些没能按照设想的那样写,主要还是javascript的水平不行,不过效果出来了!!希望以后可以写出更好的!!呵呵!!

分享到:
评论

相关推荐

    关于2021全国防灾减灾日心得总结最新精选【5篇】.docx

    从给定文件的信息来看,本文档主要围绕2021年全国防灾减灾日的心得体会进行总结,强调了防灾减灾的重要性,并通过具体的案例和个人经历来阐述如何提高防灾减灾意识和能力。 ### 重要知识点 #### 1. 防灾减灾日的...

    政治92,英语80,我的考研心得(转贴).pdf

    在复习过程中,他创新性地采取遮盖中文含义,用英文回忆单词含义的方式,以发现自己的知识盲点,并集中精力突破这些盲点。这种方法有效避免了无效的机械记忆,让学习变得更加高效。此外,作者主张专注于记忆大纲词汇...

    消防宣传月系列活动心得体会5篇2021.docx

    - **呼吸防护**:用毛巾、餐巾布等遮盖口鼻,避免吸入有毒气体。 - **利用逃生通道**:优先选择疏散楼梯、消防电梯等安全出口。 - **应急绳索使用**:在较低楼层的情况下,可以使用绳索、床单等制作简易逃生工具。 -...

    [亲子装:新年要有新气象]新年亲子装.docx

    2. 满足全家需求:亲子装不仅满足了孩子们对童趣的追求,也让父母有机会回味童年,展示自己的时尚品味。同时,孩子们穿上与父母相似的服装,会有一种被平等对待的自豪感。 3. 不失个性的搭配:专业的亲子装品牌会在...

    项目三打样跟单·织物称重实训ppt湖州职业技术学院.pptx

    在实训的最后阶段,学员们需要总结在实践中遇到的问题,并分享自己在称重过程中的经验与心得。同时,还要指出操作中需要注意的事项,包括确保布料在测量前的平整性,切样时力度的均匀性,以及称量时避免对天平造成...

    Flash实验报告动画设计实验报告-遮罩.doc

    随后,学生在另一个新图层中绘制了一个矩形,将其设置为遮罩层,并确保红色文字在最后一帧被完全遮盖,从而建立起遮罩层和被遮罩层的关系。 为了进一步探索遮罩层动画的潜力,学生还尝试了更复杂的动画制作。他们...

    「焕彩新生」小红书彩妆趋势 -小红书彩妆趋势品类洞察营销指南.pdf

    - **商品笔记**:撰写详细的商品介绍和使用心得,增加产品的可信度。 - **品牌店播**:通过直播销售的方式与消费者建立直接联系。 - **KOL直播**:利用网络红人的影响力推广产品。 - **搜索承接**:确保品牌信息能够...

Global site tag (gtag.js) - Google Analytics