美文网首页
jQuery插件 hank编写 (输入框默认提示 detext,

jQuery插件 hank编写 (输入框默认提示 detext,

作者: huang007guo | 来源:发表于2018-07-18 11:05 被阅读0次

    jQuery插件 hank编写 (输入框默认提示 detext, form检验插件 form_check, 自定义显示提示弹窗、浮框 bgmask, 节点移动 ElMove, 异步form提交、文件异步提交 form_iframe, 标签切换 switchTag, 轮播 carousel)

    /**
     * jQuery插件 hank编写
     */
    (function ($){
        /*
         * v3.4 输入框默认提示  $('#mood_message').detext('请输入文字') ||  $('#mood_message').detext('请输入文字',{left:2,top:5}); zindex是z-index
         * update 3.2 - 3.3 修复父层是relative时的错位 使用offset定位
         * ..3.4 - 3.3 增加 label .attr('detext_lab',"1"); 所有相关事件增加 命名空间detext
         */
        $.fn.detext = function (text,options){
            var my = $(this);
            if(!my.is(':visible'))
                return false;
            //宽带高度的内部默认值,
            var defau = {    
                    width:my.width(),
                    height:my.height(),
                    "zindex":get_zindex(my)+1
                  };
            //如果外部设置以外部默认值为准
            defau = $.extend(defau,$.fn.detext.defaults);
            //如果传参数以参数为准
            var opts = $.extend(defau, options);   
            var lab = $('<label>').attr('detext_lab',"1");
            var myoffset = my.offset();
            var laboffset = {left:(myoffset.left+opts.left),top:(myoffset.top+opts.top)};
            var labcssobj = {
                    cursor:'text',
                    "z-index":opts['zindex'],
                    position: "absolute",
                    width:(opts.width-2*opts.left),
                    height:(opts.height-2*opts.top)
    //              "max-width":(opts.width-2*opts.left)+'px',
    //              "max-height":(opts.height-2*opts.top)+'px'
            };
            lab.addClass(opts.myclass);
            lab.css(labcssobj);
            lab.text(text);
            my.bind({"focus.detext":function (){
                    lab.hide();},
                "focusout.detext":function (){
                    if($(this).val()=='')
                    {
                        lab.show();
                    }else
                    {
                        lab.hide();
                    }
                }});
            lab.bind("click.detext",function (){
                var lmy = $(this);
                lmy.hide(0,function (){my.focus();});
            });
            my.after(lab);
            lab.offset(laboffset);
            //窗口缩放的位置变化
            lab.get(0).resite = function (){
                var $this = $(this);
                if(!$this.is(':visible'))
                    return;
                var poffset = $($this.data('pel')).offset();
                var laboffset = {left:(poffset.left+opts.left),top:(poffset.top+opts.top)};
                $this.offset(laboffset);
            };
            
            //收集这些label
            if(!($.fn.detext.detext_el instanceof Array))
                $.fn.detext.detext_el = new Array();
            $.fn.detext.detext_el.push(lab.get(0));
            lab.data('pel',my.get(0));
            
            //窗口缩放的位置变化
            if(!$.fn.detext.detext_resize){
            $(window).bind("resize.detext",function (){
                if(!($.fn.detext.detext_el instanceof Array))
                    return;
                var allel = $.fn.detext.detext_el;
                for(var i=0;i<allel.length;i++)
                    allel[i].resite();  
            });
            $.fn.detext.detext_resize=1;
            }
            //jq获得元素真实z-index
            function get_zindex(jqobj){
                if(jqobj.css('z-index')=='auto')
                {
                    return get_zindex(jqobj.parent());
                }else
                {
                    return isNaN(parseInt(jqobj.css('z-index')))?0:parseInt(jqobj.css('z-index'));
                }
                    
                    
            }
            return my;
        };
        $.fn.detext.defaults = {    
                left: 2,    
                top: 2,
                myclass:'de_text_label'
        };
    
        /*
         *form 检验插件 v1.5 ie6兼容 $('#formid').form_check();  || $('#formid').form_check({chtype:'focusout',chwarning:function (ele,msg){alert(msg)}}); 
         *节点上可以有 属性,cherrTex错误文本,chaddTex错误文本前缀,其他属性请参考代码
         */
        $.fn.form_check = function (options){
            var defaults = $.extend({}, $.fn.form_check.defaults);   
            var opts = $.extend(defaults, options);   
            var my = $(this);
            if(typeof (opts.chright) == 'function')
                chright = opts.chright;
            if(typeof (opts.chwarning) == 'function')
                chwarning = opts.chwarning;
    
            if(opts.chtype == 'focusout'){
                $(my).on('focusout.form_check','[chtype]',function (){
                    var $this = $(this);
                    var chtype = $(this).attr('chtype');
                    var chret, addTex = $this.attr('chaddTex') ? $this.attr('chaddTex') : '';
                    if($this.attr('last')==$this.val())
                        return;
                    
                    if(chtype == 'mobile')
                        chret = chmobile($this.val());
                    else if(chtype == 'email')
                        chret = chemail($this.val());
                    else if(chtype == 'len')
                        chret = chlen($this.val(),parseInt($this.attr('minlen')),parseInt($this.attr('maxlen')));
                    else if(chtype == 'password'){
                        chret = chpass($this.val());
                        $this.parents('form').eq(0).find('input[chtype=password1]').removeAttr('last');
                    }else if(chtype == 'password1'){
                        if($this.val() == $this.parents('form').eq(0).find('input[chtype=password]').val()){
                            chret = true;
                        }else
                            chret = '两次密码输入不一致';
                    }else if(chtype == 'checked'){
                        chret = chchecked($this);
                    }
                    else
                        return;
                    
                    $this.attr('last',$this.val());
                    if(chret===true)
                        chright($this);
                    else{
                        $this.select();
                        chret = $this.attr('cherrTex')?$this.attr('cherrTex'):(addTex+chret);
                        chwarning(chret, $this);
                    }
                });
            }else if(opts.chtype == 'submit'){
                my.bind("submit.form_check",function (eve){
                        var isok = true;
                        var ele, addTex;
                        my.find('[chtype]').each(function (){
                        var $this = $(this);
                        var chtype = $(this).attr('chtype');
                        var chret;
                        if(chtype == 'mobile')
                            chret = chmobile($this.val());
                        else if(chtype == 'email')
                            chret = chemail($this.val());
                        else if(chtype == 'len')
                            chret = chlen($this.val(),parseInt($this.attr('minlen')),parseInt($this.attr('maxlen')));
                        else if(chtype == 'password')
                            chret = chpass($this.val());
                        else if(chtype == 'password1'){
                            if($this.val() == $this.parents('form').eq(0).find('input[chtype=password]').val()){
                                chret = true;
                            }else
                                chret = '两次密码输入不一致';
                        }else if(chtype == 'checked'){
                            chret = chchecked($this);
                        }
                        else
                            return true;
                        if(chret !== true){
                            isok = chret;
                            ele = $this;
                            return false;
                        }
                        });
                        if(isok !== true){
                            addTex = ele.attr('chaddTex') ? ele.attr('chaddTex') : '';
                            isok = ele.attr('cherrTex')?ele.attr('cherrTex'):(addTex+isok);
                            if(typeof (opts.chwarning) == 'function')
                                opts.chwarning(isok, ele, eve);
                            else{
                                alert(isok);
                                ele.select();
                            }
                            my.attr('form_check_submit',"0");
                            return false;
                        }else{
                            my.attr('form_check_submit',"1");
                            my.trigger('form_check_submit_ok');
                            if(typeof (opts.chright) == 'function')
                                return opts.chright();
                            else
                                return true;
                        }
                });
            }
            function chright(El){
                var rihtml = '<span class="form_check-result-chright"></span>';
                var ele = _get_toele(El);
                $(ele).show();
                $(ele).html(rihtml);
            }
            function chwarning(msg, El){
                var warhtml = '<span class="form_check-result-warning">'+msg+'</span>';
                var ele = _get_toele(El);
                $(ele).show();
                $(ele).html(warhtml);
            }
            function _get_toele($this){
                var toele;
                //获取显示信息的节点
                //信息显示在 warid属性的id上
                if($this.is('[form_check-warid]'))
                {
                    toele = $('#'+$this.attr('form_check-warid')).get(0);
                }
                //信息显示在 (下一个节点||下一个节点内)的属性checkspan=1的节点上
                else if($this.next().find('*').andSelf().filter('[form_check-checkspan=1]').size()==1){
                    toele = $this.next().find('*').andSelf().filter('[form_check-checkspan=1]').get(0);
                }
                //信息显示在 id = form_check- + name属性
                else if($this.is('[name]')&&$this.parents('form').eq(0).find('#form_check-'+$this.attr('name')).size()==1){
                    toele = $this.parents('form').eq(0).find('#form_check-'+$this.attr('name')).get(0);
                }
                //信息显示在添加一个 (下一个节点属性checkspan=1的节点)上
                else{
                    var obj = $('<span form_check-checkspan="1" class="form_check-result">&nbsp;</span>');
                    $this.after(obj);
                    toele = obj.get(0);
                }
                return toele;
            }
            return my;
        };
        //email 电子邮箱 校验函数
        function chemail(msg, name){
            if(typeof (name) == 'undefined')
                name = '电子邮箱';
            var ems = msg.search(/^[\w\-]+@[\w\-]+(\.[a-zA-Z]+)+$/g);
            if(ems==-1)
                return name+'格式有误';
            else
                return true;
        }
        //手机校验函数
        function chmobile(msg, name){
            if(typeof (name) == 'undefined')
                name = '手机';
            var ems = msg.search(/^1\d{10}$/g);
            if(ems==-1)
                return name+'格式有误';
            else
                return true;
        }
        //长度校验函数
        function chlen(msg, minlen, maxlen, istrim){
            if(typeof istrim == 'undefined')
                istrim = true;
            if(typeof minlen == 'undefined' || minlen == null || isNaN(minlen))
                minlen = 1;
            if(istrim)
                msg = $.trim(msg);
                        if(typeof maxlen == 'undefined' ||  maxlen == null || isNaN(maxlen) || maxlen < minlen ){
                                if(msg.length < minlen)
                                        return '内容长度不正确';
                        }else if(msg.length < minlen||msg.length > maxlen){
                            //return '内容长度必须大于' + (minlen - 1) + '小于' + (maxlen + 1); alert();
                            return '内容长度不正确';
                        }
            return true;
        }
    
        //密码检验函数
        function chpass(msg, name) {
            if(typeof (name) == 'undefined')
                name = '密码';
            if(msg == '' || (/[\'\"\\]/).test(msg)) {
                return name+'空或包含非法字符';
            }
            /*else if(msg.length < 6 || msg.length > 15){
                return name+'长度必须是6-15位';
            }*/
            else if(!(/^\w{6,18}$/).test(msg)){
                return name+'必须是英文字母数字下划线6-18位';
            }
            else {
                return true;
            }
        }
        //checked验证
        function chchecked(ele){
            if( ele.is(':checked'))
                return true;
            else
                return '未选中';
        }
        
        //提供一个外部调用验证函数的接口
        $.fn.form_check.callChFun = function(){
            var nowFun = eval("ch"+arguments[0]);
            var args = [];
            for(var i = 1; i < arguments.length; i++){
                args.push(arguments[i]);
            }
            return nowFun.apply(nowFun, args);
        };
        //外部可访问默认值
        $.fn.form_check.defaults = {    
                chtype: 'focusout',
                chright: '',
                chwarning: ''
        };
    })(jQuery);
    
    (function ($){
        /*
         * @name 自定义显示提示  v0.92  由于位置使用margin,fixed 所以最多兼容ie7 v0.92 - v0.93 修复一个遍历数组bug
         * @desc 其中$(bgmask).data('myshow') 保存了所有这个bgmask的show节点,但是要判读现在是否属于它还需判断show节点的tobgmask属性是否是bgmask的bgmask属性!<br />
         * @param $.bgmask.defaults <br />
         *        scroll是否开启滚动 默认关闭
         *        clickhide 是否点击bgmask关闭它,默认是
         *        clickremove 是否点击bgmask删除它,默认否
         *        show_site 显示的节点的位置 如果是字符串 top,center,left,right,bottom。 如果是对象参见$.bgmask._site函数  v1.0 默认center
         *        opacity bgmask的透明度,默认0.4
         *        backgroundColor bgmask的背景色,默认black
         *        myclass bgmask的class 默认bgmask
         *        shut_attr 显示节点内的关闭按钮的需要包含的属性 默认shut_bgmask 所有shut关闭按钮上的data('pshow')保存了它们所属show节点
         *        remove_attr 显示节点内的彻底关闭按钮(无法用open)的需要包含的属性 默认remove_bgmask 所有remove关闭按钮上的data('pshow')保存了它们所属show节点
         *        zIndex bgmask的z-index
         *        bgmaskid bgmask的bgmaskid,用来给使用者来识别它<br />
         *     这些变量可以通过   $.bgmask.defaults 重新覆盖,而调用插件open函数时可以传options参数二次覆盖
         * @param $.bgmask.allbg 保存了所有的bgmask包括关闭的   $.bgmask.allbg[$bgmask.attr('bgmask')]=$bgmask; 
         * @fun open函数      1.打开一个新的个bgmask,  show Element|jQuery|string    return $bgmask.attr('bgmask');<br />
         *      2.重新打开已有bgmask open(attr) 如果 attr是#id即为bgmask属性 如果是字符##id即为bgmaskid属性      默认为最后一个新建的,以下等同
         * @fun shut函数 关闭一个bgmask   attr参数与open第二种一样,#..,##...
         * @fun remove函数 彻底关闭一个bgmask(无法用open), attr参数同上, 其中is_retain_show参数为是否保留show节点,默认不保留
         * @fun clear函数 清理bgmask上的show节点   attr参数与open第二种一样,#..,##...., is_retain_show 是否保存data里的show节点默认为不保存
         * @author hank 
         */
        $.bgmask = {
            defaults:{
                scroll:false,
                clickhide:true,
                clickremove:false,
                show_site:"center",
                opacity : 0.4,
                backgroundColor : "black",
                myclass : "bgmask",
                shut_attr:"shut_bgmask",
                remove_attr:"remove_bgmask"
            },
            index:1,
            max_zIndex:1000,
            allbg:{},
            open:function (show,options){
                if(typeof show === 'undefined' && typeof options  === 'undefined'){
                    return _reopen('#'+$('[bgmask]').last().attr('bgmask'));
                }
                if(typeof show == 'string' && show.charAt(0) == '#')
                    return _reopen(show);
                var defaults = {
                        zIndex : $.bgmask.max_zIndex,
                        bgmaskid:$.bgmask.index
                };          
                defaults = $.extend(defaults, $.bgmask.defaults);
                var opts = $.extend(defaults, options);
                var maskcss = {
                        zIndex : 1000,
                        backgroundColor : "black",
                        opacity : 0,
                    position : "fixed",
                    width : "100%",
                    height: "100%",
                    top : 0,
                    left : 0
                };
                maskcss = $.extend(maskcss, opts);
                maskcss['filter'] = "alpha(opacity="+maskcss.opacity*100+")"; 
                var $bgmask = $('<div bgmask="'+$.bgmask.index+'" bgmaskid="'+opts.bgmaskid+'"></div>');
                    $bgmask.attr('class',opts.myclass);
                    $bgmask.css(maskcss);
                    $('body').append($bgmask);
                    
                $bgmask.show();
                
                
                if(opts.scroll === false){
                    var scroll_el_str = is_ie?'html':'body';
                    if($(scroll_el_str).css("overflow")!='hidden'){
                        $bgmask.attr('body_overflow',$(scroll_el_str).css("overflow"));
                        var oldwidth = $(scroll_el_str).width();
                        $(scroll_el_str).css("overflow", "hidden");
                        var scrollwidth = $(scroll_el_str).width() - oldwidth;
                        $bgmask.attr('body_scrollwidth',scrollwidth);
                        $(scroll_el_str).css('paddingRight',parseInt($(scroll_el_str).css('paddingRight'))+scrollwidth);
                    }
                }
                if(opts.clickremove === true){
                    $bgmask.bind("click.bgmask",function (){
                        $.bgmask.remove("#"+$(this).attr('bgmask'));
                    });
                }
                if(opts.clickhide === true){
                    $bgmask.bind("click.bgmask",function (){
                        $.bgmask.shut("#"+$(this).attr('bgmask'));
                    });
                }
                if(typeof show === 'undefined'){
                    show = $('<div>');
                }
                if (!(show instanceof jQuery)) {
                    try {
                        if (typeof show == 'string' || show instanceof Element) {
                            show = $(show);
                        }else{
                            show = $('<div>');
                        }
                    } catch (err) {
                        show = $(show);
                    }
                }
                var $show = show;
                $show.css({
                    zIndex : parseInt($bgmask.css("zIndex"))+1
                    });
                if($show.parent().size()==0){
                    $('body').append($show);
                }
                $show.show();
                //show放到bgmask data里
                if($bgmask.data('myshow') == undefined){
                    $bgmask.data('myshow',[$show]);
                }else{
                    if ($show.attr('tobgmask') != $bgmask.attr('bgmask')) {
                        var oldatt = $bgmask.data('myshow');
                        oldatt = oldatt.push($show);
                        $bgmask.data('myshow', oldatt);
                    }
                }
                $show.attr('tobgmask',$bgmask.attr('bgmask'));
                
                $allshut_el = $show.find('['+opts.shut_attr+']');
                $allshut_el.data('pshow',$show);
                $allshut_el.each(function (){
                    $(this).bind("click.bgmask",function (myeve){
                        var pshow = $(this).data('pshow');
                        $.bgmask.shut("#"+pshow.attr('tobgmask'));
                        myeve.preventDefault();
                    });
                    
                });
                $allremove_el = $show.find('['+opts.remove_attr+']');
                $allremove_el.data('pshow',$show);
                $allremove_el.each(function (){
                    $(this).bind("click.bgmask",function (myeve){
                        var pshow = $(this).data('pshow');
                        $.bgmask.remove("#"+pshow.attr('tobgmask'));
                        myeve.preventDefault();
                    });
                    
                });
                
                
                $.bgmask._site($show,opts.show_site);
                //得到新的最高层
                if(parseInt($bgmask.css("zIndex"))>=$.bgmask.max_zIndex)
                    $.bgmask.max_zIndex = parseInt($bgmask.css("zIndex"))+1;
                $bgmask.data('opts',opts);
                $.bgmask.allbg[$bgmask.attr('bgmask')]=$bgmask; 
                $.bgmask.index++;
                return $bgmask.attr('bgmask');
            },
            /*
             * show_site如果是对象  默认参数deopts = {fromjq:'',fromsite:{top:1,left:0},css:{}};
             * fromjq 为jq节点或者节点,//$show相对于formjq的位置top,left 可以选值1,0,-1 ;  css作为最后附加选项覆盖
             */
            _site:function ($show,show_site){
                //定位
                if($show.size()<1)
                    return false;
                $show.show();
                //$show.offset({'left':$(window).width()/2,'top':$(window).height()/2});
                
                if (typeof show_site == 'string') {
                    $show.css({"top":"50%",
                        "left":"50%",
                        position : "fixed",
                        "margin":0
                    });
                    switch (show_site) {
                    case "top":
                        $show.css("top", "0");
                        $show.css("margin-left", -($show.outerWidth()/2));
                        break;
                    case "left":
                        $show.css("left", "0");
                        $show.css("margin-top", -($show.outerHeight()/2));
                        break;
                    case "right":
                        $show.css("left", "100%");
                        $show.css("margin-left", -$show.outerWidth());
                        $show.css("margin-top", -($show.outerHeight()/2));
                        break;
                    case "bottom":
                        $show.css("top", "100%");
                        $show.css("margin-top", -$show.outerHeight());
                        $show.css("margin-left", -($show.outerWidth()/2));
                        break;
                    case "top,right":
                        $show.css("top", "0");
                        $show.css("left", "100%");
                        $show.css("margin-left", -$show.outerWidth());
                        break;
                    //默认居中
                    default:
                        $show.css("margin-left", -($show.outerWidth()/2));
                        $show.css("margin-top", -($show.outerHeight()/2));
                    }
                } else if (typeof show_site == 'object') {
                    var deopts = {fromjq:'',
                                fromsite:{top:1,left:0},//$show相对于formjq的位置top,left 可以选值1,0,-1
                            css:{}
                            };
                    var opts = $.extend(deopts, show_site);
                    if(opts.fromjq != ''){
                        if(!(opts.fromjq instanceof jQuery)){ 
                            try {
                                if ((opts.fromjq instanceof Element)) {
                                    opts.fromjq = $(opts.fromjq);
                                }else{
                                    return false;
                                }
                            }catch(err){
                                return false;
                            }
                        }
                        if(opts.fromjq.size()<1)
                            return false;
                        $show.css({position:"absolute"
                        });
                        var offset = opts.fromjq.offset();
                        if(opts.fromsite.left == -1)
                            offset.left = offset.left-$show.outerWidth();
                        else
                            offset.left = offset.left+(opts.fromjq.outerWidth())*opts.fromsite.left;
                        if(opts.fromsite.top == -1)
                            offset.top = offset.top-$show.outerHeight();
                        else
                            offset.top = offset.top+(opts.fromjq.outerHeight())*opts.fromsite.top;
                        $show.offset({left:offset.left,top:offset.top});
                        
                    }
                    $show.css(opts.css);
    
                }
            },
            clear:function (attr, is_retain_show){
                if(typeof is_retain_show === 'undefined')
                    is_retain_show = false;
                var bgmasks = _getall_bgmask(attr);
                for (var j=0;j<bgmasks.length;j++){
                    bgmask = bgmasks[j];
                var bg  = $('div[bgmask='+bgmask+']');
                if(bg.size()<1)
                    continue;
                var allshow = bg.data('myshow');
                bgmask = bg.attr('bgmask');
                if(typeof allshow == 'object'){
                    for(var i=0; i< allshow.length; i++){
                        if(allshow[i].attr('tobgmask') == bgmask){
                            if(is_retain_show === false)
                                allshow[i].removeAttr('tobgmask');
                            allshow[i].hide();
                            allshow[i].trigger('bgmask_hide');
                        }
                    }
                }
                if(is_retain_show === false)
                    bg.data('myshow',[]);
                }
            },
            shut:function (attr){
                var bgmasks = _getall_bgmask(attr);
                for (var j=0;j<bgmasks.length;j++){
                    bgmask = bgmasks[j];
                var bg  = $('div[bgmask='+bgmask+']');
                if(bg.size()<1)
                    continue;
                if(bg.is(':hidden'))
                    continue;
                if(typeof bg == 'object'){
                    $.bgmask.clear("#"+bg.attr('bgmask'), true);
                    bg.hide();
                    if(bg.attr('body_overflow') != undefined){
                        var scroll_el_str = is_ie?'html':'body';
                        $(scroll_el_str).css("overflow",bg.attr('body_overflow'));
                        var scrollwidth = bg.attr('body_scrollwidth');
                        $(scroll_el_str).css('paddingRight',parseInt($(scroll_el_str).css('paddingRight'))-scrollwidth);
                    }
                    bg.trigger('bgmask-shut');
                }
            }
            },
            remove:function (attr, is_retain_show){
                if(typeof is_retain_show === 'undefined')
                    is_retain_show = false;
                var bgmasks = _getall_bgmask(attr);
                for (var j=0;j<bgmasks.length;j++){
                    bgmask = bgmasks[j];
                var bg  = $('div[bgmask='+bgmask+']');
                if(bg.size()<1)
                    continue;
                var allshow = bg.data('myshow');
                bgmask = bg.attr('bgmask');
                if(typeof allshow == 'object'){
                    for(var i=0; i< allshow.length; i++){
                        if(allshow[i].attr('tobgmask') == bgmask){
                            allshow[i].removeAttr('tobgmask');
                            if(is_retain_show === false){
                                allshow[i].trigger('bgmask_remove');
                                allshow[i].remove();
                            }
                                
                        }
                    }
                }
                if(bg.attr('body_overflow') != undefined){
                    var scroll_el_str = is_ie?'html':'body';
                    $(scroll_el_str).css("overflow",bg.attr('body_overflow'));
                    var scrollwidth = bg.attr('body_scrollwidth');
                    $(scroll_el_str).css('paddingRight',parseInt($(scroll_el_str).css('paddingRight'))-scrollwidth);
                }
                bg.remove();
                delete $.bgmask.allbg[bgmask];
                bg.trigger('bgmask-remove');
                }
            }
        };
        var is_ie = !-[1,];
        function _getall_bgmask(attr){
            if(typeof attr == 'undefined')
                attr = '#'+$('[bgmask]').last().attr('bgmask');
            var bgmasks = [];
            if(attr.indexOf('##')===0){
                var bgmaskid = attr.slice(2);
                var bg  = $('div[bgmaskid='+bgmaskid+']');
                bg.each(function (){
                    bgmasks.push($(this).attr('bgmask'));
                });
            }else if(attr.indexOf('#')===0){
                bgmasks = [attr.slice(1)];
            }else{
                return [];
            }
            return bgmasks;
        }
        function _reopen(attr){
            var bgmasks = _getall_bgmask(attr);
            for(var i=0;i<bgmasks.length;i++){
                var bgmask = bgmasks[i];
                var bg  = $('div[bgmask='+bgmask+']');
                if(bg.size()<1)
                    continue;
                var opts = bg.data('opts');
                if(bg.is(':visible'))
                    continue;
                bg.show();
                //重新判断滚动
                if(opts.scroll === false){
                    var scroll_el_str = is_ie?'html':'body';
                    if($(scroll_el_str).css("overflow")!='hidden'){
                        bg.attr('body_overflow',$(scroll_el_str).css("overflow"));
                        var oldwidth = $(scroll_el_str).width();
                        $(scroll_el_str).css("overflow", "hidden");
                        var scrollwidth = $(scroll_el_str).width() - oldwidth;
                        bg.attr('body_scrollwidth',scrollwidth);
                        $(scroll_el_str).css('paddingRight',parseInt($(scroll_el_str).css('paddingRight'))+scrollwidth);
                    }
                }
                var allshow = bg.data('myshow');
                bgmask = bg.attr('bgmask');
                if(typeof allshow == 'object'){
                    for(var j=0; j< allshow.length; j++){
                        if(allshow[j].attr('tobgmask') == bgmask){
                            allshow[j].show();
                            //重新定位
                            $.bgmask._site(allshow[j],opts.show_site);
                        }
                    }
                }
            }
        }
    })(jQuery);
    
    (function ($){
        /**
         * @name 移动El v0.2
         * @example $('移动控件').ElMove(需要移动的节点)
         * @author hank 
         */
        $.fn.ElMove = function (PEl){
            var my = $(this);
            my.each(function (){
                var now = $(this);
                now.css({"cursor": "move"});
                now.bind({"mousedown.ElMove":function (eve){
                    eve.preventDefault();
                    $(this).attr('is_elmove',"1");
                },"mouseup.ElMove":function (){
                    $(this).attr('is_elmove',"0");
                    $(this).removeData('ElMove_pageXY');
                    },
                "mousemove.ElMove":function (eve,myeve){
                    var moveMy = $(this);
                    if(!moveMy.is('[is_elmove="1"]'))
                        return;
                    if(typeof myeve !== 'undefined')
                        eve = myeve;
                    else
                        eve.stopPropagation();
                    var oldpageXY = moveMy.data('ElMove_pageXY');
                    var nowpageXY = {"X":eve.pageX,"Y":eve.pageY};
                    if(!oldpageXY){
                        oldpageXY = {"X":eve.pageX,"Y":eve.pageY};
                    }
                    var Mleft = nowpageXY.X - oldpageXY.X;
                    var Mtop = nowpageXY.Y - oldpageXY.Y;
                    moveMy.data('ElMove_pageXY',nowpageXY);
                    if(!PEl.is(':visible'))
                        return;
                    var nowOffset = PEl.offset();
                    if(PEl.css('position') == "fixed"){
                        PEl.css('position',"absolute");
                        PEl.offset(nowOffset);
                    }
                    PEl.offset({left:nowOffset.left+Mleft,top:nowOffset.top+Mtop});
                    if(Mleft != 0 || Mtop != 0)
                        PEl.trigger("ElMove");
                },
                "dragstart.ElMove":function (eve){
                    eve.preventDefault();
                }
                }).attr('data-Elmove-child','1');
            });
            PEl.attr('data-Elmove-parent','1');
            return my;
        };
        $(function (){
            $('body').bind({"mousemove.ElMove":function (eve){
                var Elmove = $('[is_elmove="1"]');
                if(Elmove.size() > 0)
                    Elmove.each(function (){
                        $(this).triggerHandler('mousemove.ElMove',eve);
                    });
            },"mouseup.ElMove":function (){
                    $('[is_elmove="1"]').attr('is_elmove',"0").removeData('ElMove_pageXY');
                },
            "mouseleave.ElMove":function (){
                //移出body是否取消
                //$('[is_elmove=1]').attr('is_elmove',"0").removeData('ElMove_pageXY');
            }
            });
        });
    })(jQuery);
    (function ($){
        /**
         * @name from的iframe提交 v0.1
         * @desc 主要是考虑带file的from异步提交
         * @param options
         *          success 成功调用函数 success(retex,toiframe);
         *          error 失败调用函数 error(toiframe);
         *          complete 完成调用函数 complete(toiframe,'success');
         *          isSubmit 是否自动提交,默认 false
         *          isOne 是否一次性, 默认 false
         * @pending       isAddDiscern 是否增加请求标识(默认iframe_async=1)如果是字符串添加此参数请求值为1 
         * @example $('form#file').form_iframe({success:function (retex){alert('提交成功,'+retex)}});
         * @author hank
         */
        $.fn.form_iframe = function (options){
            var my = $(this);
            var defaults = $.extend({}, $.fn.form_iframe.defaults);
            var opts = $.extend(defaults, options);
            my.each(function (){
                var target, iframe_name, toiframe,
                    myform = $(this);
                if(!myform.is('form'))
                    return true;
                do{
                    iframe_name = 'iframe_'+((Math.random()*10000000000000000).toString()).slice(0,14);
                }while($('iframe[name="'+iframe_name+'"]').size() > 0);
                toiframe = $('<iframe form_iframe="1" name="'+iframe_name+'"></iframe>').hide();
                myform.attr('target',iframe_name);
                myform.after(toiframe);
                myform.bind('submit',function (){
                    var subCou = (isNaN(parseInt($(this).attr('form_iframe-cou'))) ? 1 : (parseInt($(this).attr('form_iframe-cou'))+1));
                    $(this).attr('form_iframe-cou', subCou);
                    toiframe.attr('form_iframe-cou', subCou);
                });
                toiframe.data('myform',myform);
                toiframe.bind({"error.form_iframe":function (){
                    if(!toiframe.is('[form_iframe-cou]')){
                        return;
                    }
                    opts.error(toiframe);
                    opts.complete(toiframe,'failure');
                    _my_complete(toiframe, 'failure');
                },
                "load.form_iframe":function (){
                    if(!toiframe.is('[form_iframe-cou]')){
                        return;
                    }
                    var retdom = $(this).contents(),
                        retex = $(retdom).text();
                    opts.success(retex,toiframe);
                    opts.complete(toiframe,'success', retex);
                    _my_complete(toiframe, 'success');
                }});
                if(opts.isSubmit)
                    myform.submit();
            });
            function _my_complete(iframe){
                if(opts.isOne)
                    iframe.remove();
            }
            return my;
        };
        $.fn.form_iframe.defaults = {
                success: function (){},    
                error: function (){},
                complete:function (){},
                isSubmit:false,
                isOne:false
        };
    })(jQuery);
    (function ($){
        /**
         * @name 标签切换 v0.1
         * @desc 标签切换
         * @param options
         *          $() TagEl 操作切换的Els 默认切换到 eq = my.index()的showEl 可选参数 switchtag_eg 如果是数组表示切换到 第eq个showEl 否则是showEl的选择器
         *          autoHide 是否自动隐藏
         *          deEq 默认显示第几个节点,配合自动隐藏使用
         *          showEl 被切换的节点,可以是 选择器,或者jqEls
         *          PEl 可选的父节点 如果 showEl为 选择器时生效
         *          CClass 选择后的节点class
         *          eveType 激活切换的事件
         *          SBefore 切换之前调用函数 如果返回false取消切换 传递参数 nowTagEl,nowShowEl,allTagEl,allShowEl
         *          SAfter 切换之后调用函数 传递参数 nowTagEl,nowShowEl,allTagEl,allShowEl
         * @example 
         * @author hank
         */
        $.fn.switchTag = function (options){
            var my = $(this);
            var defaults = $.extend({}, $.fn.switchTag.defaults);
            var opts = $.extend(defaults, options);
            var eveType = opts.eveType+'.switchTag';
            my.bind(eveType, function (eve){
                var nowEl = $(this), nowEq = nowEl.attr('switchtag_eg'),
                    allShowEl = __getAllShowEl(),
                    nowEqS, nowShowEl;
                eve.preventDefault();
                if(nowEq){
                    //为数字
                    if((parseInt(nowEq)).toString() == nowEq)
                        nowEqS = ':eq('+nowEq+')';
                    else
                        nowEqS = nowEq;
                }else{
                    nowEqS =  ':eq('+nowEl.index()+')';
                }
                nowShowEl = allShowEl.filter(nowEqS);
                if(opts.SBefore(nowEl,nowShowEl,my,allShowEl) === false)
                    return;
                nowShowEl.show();
                allShowEl.filter(":not("+nowEqS+")").hide();
                my.removeClass(opts.CClass).attr('switchTag','0');
                nowEl.addClass(opts.CClass).attr('switchTag','1');
                opts.SAfter(nowEl,nowShowEl,my,allShowEl);
            }).attr('switchTag','0');
            if(opts.autoHide){
                my.filter(":eq("+opts.deEq+")").trigger(eveType);
            }
            function __getAllShowEl(){
                var allShowEl;
                if(typeof opts.showEl === 'string'){
                    allShowEl = opts.PEl.find(opts.showEl);
                }else{
                    allShowEl = opts.showEl;
                }
                return allShowEl;
            }
            return my;
        };
        $.fn.switchTag.defaults = {
                autoHide: false,    
                deEq: 0,
                showEl:'',
                PEl:$('body'),
                CClass:'checked_tag',
                eveType:'click',
                SBefore:function (){},
                SAfter:function (){}
                
        };
    })(jQuery);
    (function ($){
        /**
         * @name 轮播v0.1
         * @param options
                    viewEl: '.viwepager', 视窗选择器 !css overflow: hidden;font-size: 0;!
                    onceNum: 1, 每一个屏几个item
                    marginR: 2, 每个item的右边间隔
                    deNum:1, 默认初始到第几个
                    allItemEl: '.allitem', 包住所有itme的节点选择器
                    itemEl: '.item', itme 选选择器
                    toNumEve: 'click', 切到指定位置的El的触发事件类型
                    Mswipe:false, 是否支持jqmbile 的swipe事件
                    autoSwit:false, 自动切换的毫秒数,只有大于0会启动
                    isLoop:false 是否循环,到最后一个切到第一个
                        switContainEl.data('carousel-SetInterval') 绑定了int 可手动关闭;
                attrs 一下属性标记节点类型
                    [last] 切换到上个的节点
                    [next] 切换到下个
                    [tonum] 切到指定位置的El模版的包含节点
                    [tonum-one] 切到指定位置的El模版 (!num! 可以替换 位置num) 
                (switContainEl.data('carousel-ReSet'))(NowToNum,NowOnceNum); 绑定了重置的函数
                switContainEl.data('carousel-SwitItem',switItem); 绑定了切换函数的函数
                    switItem(side, is_animate, isLoop) side = next | last | num
                    
         * @example 
           <div class="swit-contain">
            <div class="viwepager">
            <div class="allitem">
                <div class="item"><img src="XXXX" /></div>
            </div>
            </div>
            <a class="last" last>last</a>
            <a class="next" next>next</a>
            <div class="tonum_p">
            <div class="tonum" tonum><a class="tonum-one" tonum-one>!num!</a></div>
            </div>
            </div>
            
         * @prompt css约束请自行添加
         * @author hank
         */
        $.fn.carousel = function (options){
            var my = $(this);
            var defaults = $.extend({}, $.fn.carousel.defaults);
            var opts = $.extend(defaults, options);
            my.each(function (){
                var switContainEl = $(this);
                var viewEl = switContainEl.find(opts.viewEl);
                var w;
                var num = opts.onceNum;
                var marginR = opts.marginR;
                var oneW;
                var deNum = opts.deNum;
                var sum;
                var screenSum;
                viewEl.data('carousel-num',0);
                viewEl.find(opts.allItemEl).width((sum*(oneW+marginR))+5);
                viewEl.find(opts.itemEl).width(oneW).css('margin-right',marginR);
                switContainEl.data('carousel-opt',{screenSum:screenSum,w:w});
                switContainEl.data('carousel-extopt',opts);
                switContainEl.data('carousel-ReSet',function (NowToNum,NowOnceNum){
                    if(typeof NowOnceNum !== 'undefined'){
                        num = NowOnceNum;
                    }
                    if(typeof NowToNum === 'undefined'){
                        NowToNum = 1;
                    }
                    w = viewEl.width();
                    oneW = (w-((num-1)*marginR))/num;
                    sum = viewEl.find(opts.itemEl).size();
                    screenSum = parseInt((sum%num != 0)? sum/num+1 : sum/num);
                    viewEl.data('carousel-num',0);
                    viewEl.find(opts.allItemEl).width((sum*(oneW+marginR))+5);
                    viewEl.find(opts.itemEl).width(oneW).css('margin-right',marginR);
                    switContainEl.data('carousel-opt',{screenSum:screenSum,w:w,marginR:marginR});
                    //初始化toNum按钮
                    switContainEl.find('[tonum]').each(function (){
                        var my = $(this);
                        var oneMb = my.data('carousel-onemb');
                        if(!oneMb){
                            oneMb = my.find('[tonum-one]');
                            oneMb.remove();
                            my.data('carousel-onemb',oneMb);
                        }
                        my.empty();
                        for(var i = 0;i < screenSum; i++ ){
                            var nowEl = oneMb.clone();
                            nowEl.html((nowEl.html()).replace(/!num!/g,i+1));
                            nowEl.attr('data-carousel-tonum',i+1);
                            my.append(nowEl);
                        }
                    });
                    switItem(NowToNum, false);
                });
                (switContainEl.data('carousel-ReSet'))(deNum);
                if(opts.autoSwit > 0){
                    var tempFun = _getTempFun();
                    window[tempFun] = function (){
                        if(switContainEl.is(':hidden'))
                            return;
                        switItem('next',undefined,true);
                    };
                    switContainEl.data('carousel-SetInterval', setInterval('window.'+tempFun+'()',opts.autoSwit));
                }
                //左右切换按钮
                switContainEl.find('[last]').click(function (){
                    switItem('last');
                });
                switContainEl.find('[next]').click(function (){
                    switItem('next');
                });
                switContainEl.on(opts.toNumEve+'.carousel','[data-carousel-tonum]',function (){
                    switItem($(this).data('carousel-tonum'));
                });
    
                //jqmobile 手机滑动
                if(opts.Mswipe){
                    viewEl.bind('swipeleft swiperight',function (eve){
                        var isOk = false;
                        if(eve.type === "swipeleft"){
                            isOk = switItem('next');
                        }else{
                            isOk = switItem('last');
                        }
                        if(isOk !== false){
                            eve.stopPropagation();
                            eve.preventDefault();
                        }
                    });
                }
                
                function switItem(side, is_animate, isLoop){
                    var NowOpt = switContainEl.data('carousel-opt');
                    if(viewEl.data('carousel-animating') == '1'){
                        return 'carousel-animating';
                    }
                    var allEl = viewEl.find(opts.allItemEl),
                    nowScreen = parseInt(viewEl.data('carousel-num')),
                    toLeft;
                    //变量默认值
                    if(typeof side === 'undefined'){
                        side = 'next';
                    }
                    if(typeof is_animate === 'undefined'){
                        is_animate = true;
                    }
                    if(typeof isLoop === 'undefined'){
                        isLoop = opts.isLoop;
                    }
                    if(side == 'next'){
                        side = nowScreen + 1;
                    }else if(side == 'last'){
                        side = nowScreen - 1;
                    }
                    if (typeof side === 'number'){
                        side = parseInt(side);
                        if(0< side && side <= NowOpt.screenSum && side != nowScreen){
                            toLeft = -((side-1) * (NowOpt.w+NowOpt.marginR));
                            viewEl.data('carousel-num',side);
                            viewEl.data('carousel-animating','1');
                            if(is_animate){
                                allEl.animate({left:toLeft},function (){viewEl.data('carousel-animating','0');});
                            }else{
                                allEl.css({left:toLeft});
                                viewEl.data('carousel-animating','0');
                            }
                            //处理next last按钮
                            switContainEl.find('[last]').show();
                            switContainEl.find('[next]').show();
                            if(side == 1){
                                switContainEl.find('[last]').hide();
                            }else if(side == NowOpt.screenSum){
                                switContainEl.find('[next]').hide();
                            }
                            //处理toNum
                            switContainEl.find('[tonum]').each(function (){
                                var my = $(this);
                                my.find('[tonum-one]:not([data-carousel-tonum="'+side+'"])').removeClass('now');
                                my.find('[tonum-one][data-carousel-tonum="'+side+'"]').addClass('now');
                            });
                            return true;
                        }else if(side > NowOpt.screenSum && isLoop){
                            switItem(1);
                        }
                    }
                    return false;
                }
                switContainEl.data('carousel-SwitItem',switItem);
            });
            return my;
        };
        $.fn.carousel.defaults = {
                viewEl: '.viwepager',    
                onceNum: 1,
                marginR: 2,
                deNum:1,
                allItemEl: '.allitem',
                itemEl: '.item',
                toNumEve: 'click',
                Mswipe:false,
                autoSwit:false,
                isLoop:false
        };
        //获得临时全局函数
        _getTempFun = function (){
            var tempfun = "_jstempfun_"+((Math.random()*10000000000000000).toString()).slice(0,14);
            while(typeof window[tempfun] !== 'undefined'){
                tempfun = "_jstempfun_"+((Math.random()*10000000000000000).toString()).slice(0,14);
            }
            window[tempfun] = function (){};
            return tempfun;
        };
    })(jQuery);
    
    

    相关文章

      网友评论

          本文标题:jQuery插件 hank编写 (输入框默认提示 detext,

          本文链接:https://www.haomeiwen.com/subject/yjcopftx.html