前端工作中碰到的一些东西

开发 前端
弹出窗口:我们在工作中,经常会碰到弹出窗口类应用,有时候还需要一点遮盖层,这类圆角弹出框其实用得还是很广泛的,用CSS3可以很容易的出现,但是考虑到浏览器兼容问题,这类还是需要用图片实现了。

久不出技术类文章,我都忘了自己是一程序员啦......今天写一点工作中遇到的东西,大家共同学习,反正也比较浅显了。

弹出窗口

我们在工作中,经常会碰到弹出窗口类应用,有时候还需要一点遮盖层:

这类圆角弹出框其实用得还是很广泛的,用CSS3可以很容易的出现,但是考虑到浏览器兼容问题,这类还是需要用图片实现了

主要代码如下:

  1. //弹出层剧中   
  2.        function popup(popupName) {   
  3.            var _scrollHeight = $(document).scrollTop(); //获取当前窗口距离页面顶部高度   
  4.            _windowHeight = $(window).height(); //获取当前窗口高度   
  5.            _windowWidth = $(window).width(); //获取当前窗口宽度   
  6.            _popupHeight = popupName.height(); //获取弹出层高度   
  7.            _popupWeight = popupName.width(); //获取弹出层宽度   
  8.            //            _posiTop = (_windowHeight - _popupHeight) / 2 + _scrollHeight - 50;   
  9.            _posiTop = _scrollHeight + 120;   
  10.            _posiLeft = (_windowWidth - _popupWeight) / 2;   
  11.            popupName.css({ "left": _posiLeft + "px", "top": _posiTop + "px", "display": "block" }); //设置position   
  12.        }   
  13.    
  14.        function dragFunc(dragDiv, dragBody) {   
  15.            if (dragDiv[0] && dragBody[0]) {   
  16.                var dragAble = false;   
  17.                var x1 = 0;   
  18.                var y1 = 0;   
  19.                var l = 0;   
  20.                var t = 0;   
  21.                var divOffset = dragBody.offset();   
  22.                dragDiv.mousedown(function (e) {   
  23.                    var ss = this;   
  24.                    //                    var rootId =    
  25.    
  26.                    dragDiv.css("cursor", "move");   
  27.                    dragAble = true;   
  28.                    // 当前鼠标距离div边框的距离   
  29.                    // 当前鼠标坐标,减去div相对左边的像素   
  30.                    l = parseInt(dragBody.css("left"));   
  31.                    t = parseInt(dragBody.css("top"));   
  32.                    x1 = e.clientX - l;   
  33.                    y1 = e.clientY - t;   
  34.                    x1x1 = x1 > 0 ? x1 : 0;   
  35.                    y1y1 = y1 > 0 ? y1 : 0;   
  36.                    this.setCapture && this.setCapture();   
  37.                });   
  38.                dragDiv.mousemove(function (e) {   
  39.                    if (!dragAble)   
  40.                        return;   
  41.                    // 当前div左边的坐标   
  42.                    // 当前鼠标坐标,减去鼠标拖动量   
  43.                    var x2 = 0;   
  44.                    var y2 = 0;   
  45.                    //需要考虑滚动条问题!!!   
  46.                    var top = $(document).scrollTop() ? $(document).scrollTop() - 15 : 0;   
  47.                    var left = $(document).scrollLeft() ? $(document).scrollLeft() - 15 : 0;   
  48.                    x2 = e.clientX - x1 + left;   
  49.                    y2 = e.clientY - y1 + top;   
  50.                    x2x2 = x2 > 0 ? x2 : 0;   
  51.                    y2y2 = y2 > 0 ? y2 : 0;   
  52.                    //要移动一定数量才移动   
  53.                    if (Math.abs(l - x2) > 10 || Math.abs(t - y2) > 10) {   
  54.                        dragBody.css("left", x2 + "px");   
  55.                        dragBody.css("top", y2 + "px");   
  56.                    }   
  57.                });   
  58.                dragDiv.mouseup(function (event) {   
  59.                    if (!dragAble)   
  60.                        return;   
  61.                    dragAble = false;   
  62.                    // dragDiv.css("position", "relative");   
  63.                    this.releaseCapture && this.releaseCapture();   
  64.                });   
  65.            }   
  66.        }   
  67.    
  68.        var MyDialog = function (cfg) {   
  69.            this.config = {   
  70.                id: (new Date()).getTime().toString(),   
  71.                el: null,   
  72.                bodyId: null,   
  73.                cover: true,   
  74.                boxHtm: '<div class="dialog"  >  ' +   
  75.        '<table>   ' +   
  76.        '    <tr class="top">  ' +   
  77.        '       <td class="tl"> ' +   
  78.        '        </td> ' +   
  79.        '        <td class="c"> ' +   
  80.        '        </td> ' +   
  81.        '        <td class="tr"> ' +   
  82.        '        </td> ' +   
  83.        '    </tr> ' +   
  84.        '    <tr> ' +   
  85.        '        <td class="c"> ' +   
  86.        '          <div style="width:10px;"></div>' +   
  87.        '        </td> ' +   
  88.        '        <td class="main"> ' +   
  89.        '            <div class="title"> ' +   
  90.        '                <h3> ' +   
  91.        '                    <span class="title_text">请输入标题</span> <a class="cls" href="javascript:;"></a> ' +   
  92.        '                </h3> ' +   
  93.        '            </div> ' +   
  94.        '            <div class="content"> ' +   
  95.        '             请输入内容   ' +   
  96.        '            </div> ' +   
  97.        '        </td> ' +   
  98.        '        <td class="c"> ' +   
  99.        '        </td> ' +   
  100.        '    </tr> ' +   
  101.        '    <tr class="bottom"> ' +   
  102.        '        <td class="bl"> ' +   
  103.        '        </td> ' +   
  104.        '        <td class="c"> ' +   
  105.        '          <div style="width:10px;"></div>' +   
  106.        '        </td> ' +   
  107.        '        <td class="br"> ' +   
  108.        '        </td> ' +   
  109.        '    </tr> ' +   
  110.        '</table> ' +   
  111.    '</div>'  
  112.            };   
  113.            var scope = this;   
  114.            if (cfg) {   
  115.                $.each(cfg, function (key, value) {   
  116.                    scope.config[key] = value;   
  117.                });   
  118.            }   
  119.            this.box = null;   
  120.            this.cover = null;   
  121.            this.tmpBody = null;   
  122.        }   
  123.    
  124.    
  125.        MyDialog.prototype.show = function () {   
  126.            var scope = this;   
  127.            var cover = null;   
  128.            var box = null;   
  129.            if (this.config.cover) {   
  130.                if (this.config.id && $('#' + this.config.id + '_cover')[0]) {   
  131.                    cover = $('#' + this.config.id + '_cover');   
  132.                    cover.show();   
  133.                } else {   
  134.                    cover = $('<div style=" display:block; " id="' + this.config.id + '_cover" class="coverDiv" ></div>');   
  135.                    $('body').append(cover);   
  136.                }   
  137.                scope.cover = cover;   
  138.            }   
  139.            if (!$('#' + this.config.id)[0]) {   
  140.                box = $(this.config.boxHtm);   
  141.                $('body').append(box);   
  142.                box.attr('id', this.config.id);   
  143.                if (this.config.title) {   
  144.                    box.find('.title_text').html(this.config.title);   
  145.                }   
  146.                if (this.config.bodyId) {   
  147.                    var body = $('#' + this.config.bodyId);   
  148.                    var tmp = $('<div></div>').append(body);   
  149.                    var initBody = tmp.html();   
  150.                    scope.tmpBody = $(initBody);   
  151.                    tmp = null;   
  152.                    if (body[0]) {   
  153.                        var con = box.find('.main .content');   
  154.                        body.show();   
  155.                        con.html('');   
  156.                        con.append(body);   
  157.                    }   
  158.                }   
  159.                if (this.config.el && this.config.el[0]) {   
  160.                    var con = box.find('.main .content');   
  161.                    con.html(this.config.el);   
  162.                }   
  163.                //居中   
  164.                popup(box);   
  165.                //关闭dialog   
  166.                box.find('.title .cls').click(function (e) {   
  167.                    scope.close();   
  168.                    e.preventDefault();   
  169.                    return false;   
  170.                });   
  171.    
  172.                dragFunc($('#' + this.config.id + ' .main .title'), $('#' + this.config.id));   
  173.                box.show();   
  174.                this.box = box;   
  175.            }   
  176.        }   
  177.        MyDialog.prototype.close = function () {   
  178.            //这里有问题   
  179.            var box = this.box;   
  180.            var tmpBody = this.tmpBody;   
  181.            var cover = this.cover;   
  182.            if (tmpBody && tmpBody[0]) {   
  183.                $('body').append(tmpBody);   
  184.            }   
  185.            if (box && box[0]) {   
  186.                box.remove();   
  187.            }   
  188.            if (cover && cover[0]) {   
  189.                cover.hide();   
  190.            }   
  191.        }; 

调用方法:

  1. var dia = new MyDialog({   
  2.                 title : title,   
  3.                 bodyId : id,   
  4.                 id : id + '_box'  
  5.             });   
  6.             dia.show(); 

具体可能还需要一定函数回调,各位可以自己封装一番。

拖放

工作中也经常会出现拖放效果的一些需求:

代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  2. <html xmlns="http://www.w3.org/1999/xhtml">   
  3. <head>   
  4.     <title></title>   
  5.     <script src="http://www.cnblogs.com/scripts/jquery-1.7.1.js" type="text/javascript"></script>   
  6.     <script type="text/javascript">   
  7.     
  8.         function dragFunc(dragDiv, dragBody, dropBody) {   
  9.             if (!dropBody[0]) {   
  10.                 dropBody = $(document);   
  11.             }   
  12.             if (dragDiv[0] && dragBody[0]) {   
  13.                 var dragAble = false;   
  14.                 var x1 = 0;   
  15.                 var y1 = 0;   
  16.                 var l = 10;   
  17.                 var t = 10;   
  18.                 var init_position = '';   
  19.                 var init_cursor = '';   
  20.                 var tmp_body = null;   
  21.     
  22.                 dragDiv.mousedown(function (e) {   
  23.                     var ss = this;   
  24.     
  25.                     init_position = dragBody.css("position");   
  26.                     init_cursor = dragBody.css("init_cursor");   
  27.                     dragBody.css("position", "absolute");   
  28.                     dragDiv.css("cursor", "move");   
  29.     
  30.                     tmp_body = $('<div class="tmp_div"></div>');   
  31.                     tmp_body.css('width', dragBody.css('width'));   
  32.                     tmp_body.css('height', dragBody.css('height'));   
  33.                     tmp_body.insertAfter(dragBody);   
  34.                     $(document).bind("selectstart", function () { return false; });     
  35.     
  36.     
  37.                     dragAble = true;   
  38.                     // 当前鼠标距离div边框的距离   
  39.                     // 当前鼠标坐标,减去div相对左边的像素   
  40.                     l = parseInt(dragBody.css("left")) ? parseInt(dragBody.css("left")) : 10;   
  41.                     t = parseInt(dragBody.css("top")) ? parseInt(dragBody.css("top")) : 10;   
  42.     
  43.                     var offset = dragBody.offset();   
  44.     
  45.                     l = parseInt(offset.left);   
  46.                     t = parseInt(offset.top);   
  47.     
  48.                     x1 = e.clientX - l;   
  49.                     y1 = e.clientY - t;   
  50.                     x1x1 = x1 > 0 ? x1 : 0;   
  51.                     y1y1 = y1 > 0 ? y1 : 0;   
  52.                     this.setCapture && this.setCapture();   
  53.                 });   
  54.                 dragDiv.mousemove(function (e) {   
  55.                     if (!dragAble)   
  56.                         return;   
  57.                     // 当前div左边的坐标   
  58.                     // 当前鼠标坐标,减去鼠标拖动量   
  59.                     var x2 = 0;   
  60.                     var y2 = 0;   
  61.                     //需要考虑滚动条问题!!!   
  62.                     var top = $(document).scrollTop() ? $(document).scrollTop() - 15 : 0;   
  63.                     var left = $(document).scrollLeft() ? $(document).scrollLeft() - 15 : 0;   
  64.                     x2 = e.clientX - x1 + left;   
  65.                     y2 = e.clientY - y1 + top;   
  66.                     x2x2 = x2 > 0 ? x2 : 0;   
  67.                     y2y2 = y2 > 0 ? y2 : 0;   
  68.                     //要移动一定数量才移动   
  69.                     if (Math.abs(l - x2) > 10 || Math.abs(t - y2) > 10) {   
  70.                         dragBody.css("left", x2 + "px");   
  71.                         dragBody.css("top", y2 + "px");   
  72.                     }     
  73.                     //红 #993300   
  74.                     //灰 #DBEAF9   
  75.                     //移动结束后判断拖放   
  76.                     var w = parseInt(dragBody.css('width'));   
  77.                     var h = parseInt(dragBody.css('height'));   
  78.                     $.each(dropBody, function () {   
  79.                         var el = $(this);   
  80.                         el.css('background-color', 'Gray');   
  81.                         var offset = el.offset();   
  82.     
  83.                         var _l = offset.left || 0;   
  84.                         var _t = offset.top || 0;   
  85.                         var _w = parseInt(el.css('width'));   
  86.                         var _h = parseInt(el.css('height'));   
  87.     
  88.                         if (x2 > _l && x2 + w < _l + _w && y2 > _t && y2 + h < _t + _h) {   
  89.                             el.css('background-color', '#DBEAF9');   
  90.                                 
  91.                             el.append(tmp_body);   
  92.     
  93.                         }   
  94.     
  95.                         var s = '';   
  96.     
  97.                     });   
  98.     
  99.                 });   
  100.                 dragDiv.mouseup(function (event) {   
  101.                     if (!dragAble)   
  102.                         return;   
  103.     
  104.                     $(document).unbind("selectstart");     
  105.     
  106.                     //还原position 与 cursor   
  107.                     dragBody.css("position", init_position);   
  108.                     dragBody.css("cursor", init_cursor);   
  109.     
  110.                     //dragBody.css("left", '0');   
  111.                     //dragBody.css("top", '0');   
  112.                     if (tmp_body) {   
  113.                         dragBody.insertAfter(tmp_body);   
  114.                         var offset = tmp_body.offset();   
  115.                         l = parseInt(offset.left);   
  116.                         t = parseInt(offset.top);   
  117.                         dragBody.css("left", l);   
  118.                         dragBody.css("top", t);   
  119.                         tmp_body.remove();   
  120.     
  121.                     }   
  122.                     dragAble = false;   
  123.                     // dragDiv.css("position", "relative");   
  124.                     this.releaseCapture && this.releaseCapture();   
  125.                 });   
  126.             }   
  127.         }   
  128.         $(document).ready(function () {   
  129.             var d1 = $('#d1');   
  130.             var c = $('.c');   
  131.     
  132.             dragFunc(d1, d1, c);   
  133.     
  134.     
  135.         });     
  136.     </script>   
  137.     <style type="text/css">   
  138.         div   
  139.         {   
  140.             width: 100px;   
  141.             height: 100px;   
  142.             border: 1px solid black;   
  143.               
  144.         }   
  145.         .tmp_div    
  146.         {   
  147.             border-style: dashed;   
  148.                 
  149.         }   
  150.             
  151.         #c1   
  152.         {   
  153.             background-color:  Gray;   
  154.             width: 300px;   
  155.             height:300px;   
  156.             float:left;   
  157.             margin:20px;   
  158.         }   
  159.             
  160.          #c2   
  161.         {   
  162.             background-color:  Gray;   
  163.             width: 300px;   
  164.             height:300px;   
  165.             float:left;   
  166.             margin:20px;   
  167.         }        
  168.     </style>   
  169. </head>   
  170. <body>   
  171.     <div id="c1" class="c">1   
  172.         <div id="d1">me   
  173.         </div>   
  174.     </div>   
  175.     <div id="c2" class="c">2   
  176.     </div>    
  177. </body>   
  178. </html> 

异步文件上传

 

我们所谓的AJAX异步文件上传事实上用js技术好像暂时还不能实现,就我所谓的异步上传事实上还是表单提交,而将form的target指向一

隐藏的iframe,然后成功后回调即可,真是十分坑爹的做法。。。。。

若是要更好的体验,便需要借助flash或者XX框架了,但是我也没有研究过.

  1. <form id="formImg" name="formImg" enctype="multipart/form-data" method="post" action="">   
  2. <input type="hidden" name="MAX_FILE_SIZE" value="800000" id="max_size"/>   
  3. <input type="hidden" name="callback" value="parent.add_img_input" id="callback"/>   
  4. <a class="upbtn"><input type="file" name="userfile" id="userfile" title="支持JPG、GIF、PNG格式,文件小于1M"   
  5. name="pic" value="" onchange="javascript:up_img(17);">上传</a>   
  6. </form>   
  7. document.charset='utf-8';   
  8.                 var form = $('#formImg');   
  9.                 var frame = $('#frame_img');   
  10.                 if (!frame[0]) {   
  11.                     frame = $('<iframe id="frame_img" name="frame_img" style="display:none;" ></iframe>');   
  12.                 }   
  13.                 form.append(frame);   
  14.                 form.attr('target', 'frame_img');   
  15.                 form.attr('action', url);   
  16.                 form.submit();      
  17.                 document.charset='gbk'

但是回调会涉及一点跨域的问题,需要在同一大域名下才行。

原文链接:http://www.cnblogs.com/yexiaochai/archive/2013/01/05/2846082.html

责任编辑:张伟 来源: 博客园
相关推荐

2021-08-28 11:47:52

json解析

2020-10-12 08:03:51

Go语言编程

2021-07-05 05:34:10

Typescript语言开发

2013-04-07 10:40:55

前端框架前端

2021-03-27 00:00:01

软件开发语言

2013-07-24 09:32:13

Android项目

2021-10-31 07:36:17

前端JavaScript编程

2021-07-15 08:12:31

体系感面试逻辑思维

2022-01-11 14:25:46

前端监控SDK

2021-06-30 07:19:35

微服务业务MySQL

2022-09-01 11:02:42

前端工具

2015-12-30 11:14:57

前端工具

2017-09-01 12:48:34

DevSecOps安全运维

2017-10-16 14:40:50

数据库MySQL工具

2021-04-18 21:07:32

门面模式设计

2012-10-29 13:20:17

应届生IT女生招聘

2011-03-08 14:28:03

proftpdGentoo

2016-09-12 17:19:51

JavaScriptArray操作技巧

2010-05-25 17:00:04

Java WebWeb容器Web应用

2012-04-16 09:54:05

移动web错误理念
点赞
收藏

51CTO技术栈公众号