关于h5设配手机的一些jquery封装

关于h5设配手机的一些jquery封装

众所周知,h5的项目在开发中会有很多适配的问题,不像pc中那么的单纯,给自己记下的笔记,就当学习了~。
首先是关于手机终端问题

1.判断是否为微信:

/*判断微信==================*/
function isWeiXin() { //返回true/false
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true;
	} else {
		return false;
	}
}
/*判断微信end*/

2.判断是否为移动端(乐视手机)

/*判断是否为移动端==================*/
function isMobile() { //返回true/false
	var userAgentInfo = navigator.userAgent;
	/*var Agents = new Array("Android", "iPhone","ipad","iPad", "SymbianOS", "Windows Phone", "iPad", "iPod");*/
	var Agents = ["Android", "iPhone", "ipad", "iPad"];
	var flag = false;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) >= 0) {
			flag = true;
			return flag;
		}
	}
	//匹配乐视手机max2
	if (userAgentInfo ==
		"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/37.0.696.34 Safari/534.24") {

		flag = true;
	}
	return flag;
}
/*判断是否为移动端end*/

3.若是移动端给body加一些class

/*根据浏览器 给 body 加 class 自动执行==================*/
(function() {
	function browser() { //移动端 body标签写入 class="Android"/ "iPhone"
		var userAgentInfo = navigator.userAgent;
		/*var Agents = new Array("Android", "iPhone","ipad","iPad", "SymbianOS", "Windows Phone", "iPad", "iPod");*/
		var Agents = ["Android", "iPhone", "ipad", "iPad"];
		for (var v = 0; v < Agents.length; v++) {
			if (userAgentInfo.indexOf(Agents[v]) >= 0) {
				$("body").addClass(Agents[v]);
				return;
			}
		}
	}
	$().ready(function() {
		browser();
	});
})();
/*根据浏览器 给 body 加 class end==================*/

4.解决三星note2 数字键盘没有小数点

function mCommon20150221_note2NumInput(obj) { //参数是查找范围 jq对象,默认是document
	var box = $(document); //查找范围
	if (obj != null && obj != "") { //如果obj有参数
		box = obj; //查找范围是obj
	}
	var userAgentInfo = navigator.userAgent;
	var note2 = "GT-N7108"; //note的标记
	var qqBrowser = "MQQBrowser"; //qq浏览器
	if (userAgentInfo.indexOf(note2) > 0 && userAgentInfo.indexOf(qqBrowser) < 0) { //如果是note2,且不是qq浏览器
		box.find('input[type="number"]').attr("type", "text");
	}
}
$().ready(function() {
	mCommon20150221_note2NumInput();
});
/*解决三星note2 数字键盘没有小数点 end================*/

5.纯jquery写验证码倒计时。
在这里插入图片描述
首先html页面

<div class="mRegisterpage_form">
			<div id="_inputTelcontainer" class="mCommon_basicInputTel mCommon_delPicHide">
				<input id='mobile2' type="tel" maxlength="11" placeholder="请输入手机号">
				<img src="../images/mRegisterpage_deletePw.png">
			</div>
			<p id='tips'  class="tips">!手机号不能为空</p>
			<div class="mCommon_basicInputValidateCode_box" id="mCommon_container">
			    <div class="mCommon_basicInputValidateCode_boxInput">
			        <input type="text"  maxlength="6" placeholder="请输入验证码" type="number">
			    </div>
			    <div class="mCommon_basicInputValidateCode_boxOperation">
			        <div class="mCommon_basicInputValidateCode_boxCode mCommon_basicInputValidateCode_boxObtain">获取验证码</div>
			        <div class="mCommon_basicInputValidateCode_boxCode mCommon_basicInputValidateCode_boxTiming">重新获取<i>60</i>s</div>
			    </div>
			</div>
		</div>
------------------------------------------------------------------------------
<style>
/*输入框*/
.mRegisterpage_form{
	display:flex;
	flex-direction:column;
	padding:15px 20px;
}
.mCommon_basicInputTel {
    display:flex;
    border: 1px solid #e1e2e6;
    padding: 7px 10px;
    background-color: #f5f6f8;
	border-radius:4px;
}
.mCommon_basicInputTel > input {
    border: 0;
    height: 24px;
    font-size: 14px;
    color: #333333;
    width: 100%;
	background-color:#f5f6f8;
}
.mCommon_delPicHide > img {
    display: none;
}
.mCommon_basicInputTel > img {
    width: 24px;
    height: 24px;
}
/*验证码*/
.mCommon_basicInputValidateCode_box{
    display: table;
    width:100%;
    table-layout: fixed;
	margin-top:15px;
	border-radius:4px;
}
.mCommon_basicInputValidateCode_boxInput{
    display: table-cell;
    width:100%;
    padding:7px 10px;
    background-color:#f5f6f8;
    border:1px solid #e1e2e6;
    border-right:none;
	border-top-left-radius:4px;
	border-bottom-left-radius:4px;
}
.mCommon_basicInputValidateCode_boxInput>input{
    width:100%;
    line-height: 26px;
    height: 26px;
    color: #333;
    font-size: 14px;
    border:0;
	background-color:#f5f6f8;
}
.mCommon_basicInputValidateCode_boxOperation{
    display: table-cell;
    width: 110px;
    padding:7px 0px;
    background-color:#f5f6f8;
    font-size:14px;
    text-align: center;
    border:1px solid #e1e2e6;
    border-left:none;
	border-top-right-radius:4px;
	border-bottom-right-radius:4px;
}
.mCommon_basicInputValidateCode_boxCode{
    height:26px;
    line-height:26px;
    border-left: 1px solid #e1e2e6;
}
.mCommon_basicInputValidateCode_boxObtain{
    color:rgb(26,134,199);
    cursor: pointer;
}
.mCommon_basicInputValidateCode_boxObtainDefault{
    color:#bfbfbf;
    cursor: default;
}
.mCommon_basicInputValidateCode_boxTiming{
    color:#878f95;
    cursor: default;
    display: none;
}
</style>

当前页面的js部分

<script>
var idObj = {
		_inputTelcontainer:$("#_inputTelcontainer"),//手机号输入框
		mCommon_container:$("#mCommon_container"),//验证码
	};
	
	var _inputTel = new mCommon_basicInputTel({
	   //输入框外容器,jq对象,必填
	   telInputBox:idObj._inputTelcontainer,
	   //手机号输入框删除回调事件,选填
	   telDeleteClickCB:function(){
		   console.log('清空内容回调');
	   },
	   //内容改变的回调事件,选填
	   inputChangeValCB:function(){
		   console.log('内容更改了啊');
	   }
   });
   
	var _setTimeObj= new mCommon_basicInputValidateCode_setTime({
	    //验证码外容器
	    codeBox:idObj.mCommon_container,
	    //点击获取按钮执行的回调事件,选填
	    obtainClick:function(){
	        var isContinue = true;
			console.log('回调');
			if($('#mobile2').val() == undefined ||$('#mobile2').val()=='' ) {
			
			}else {
				let mobile2 = $("#mobile2").val()
				$.ajax({
				type: "post",
				url: "",
				data: {mobile:mobile2},
				dataType: "json",  
				jsonp:'callback',
				success: function(res){
			     if(res.errcode == '1002')	 {

				 } else if(res.errcode=='1000' || res.errcode=='1001') {
				
					}, 2000);   
				 }	
				}
			});	
	        return isContinue;
			}
	    }
	});
	</script>

在公共js里封装的js:

/**
 * 验证码倒计时
 */
function mCommon_basicInputValidateCode_setTime(options){
    this.setting={
        //验证码外容器
        codeBox:null,
        //点击获取按钮执行的回调事件,选填
        obtainClick:null
    };
    options = options || {};
    //合并参数
    $.extend(true, this.setting, options);
    //容器必须存在
    if(!this.setting.codeBox || this.setting.codeBox.length == 0){
        return false;
    }
    //获取验证码按钮不可编辑
    this.$obtainDefault = this.setting.codeBox.find('.mCommon_basicInputValidateCode_boxObtainDefault');
    //获取验证码按钮可编辑时
    this.$obtain = this.setting.codeBox.find('.mCommon_basicInputValidateCode_boxObtain');
    //倒计时按钮
    this.$timing = this.setting.codeBox.find('.mCommon_basicInputValidateCode_boxTiming');
    //倒计时读秒放入的容器
    this.$timeBox = this.$timing.children('i');
    //倒计时时间
    this.timeNum = 60;

    var _self = this;
    //按钮状态改变为不可编辑
    this.changeNoEdit = function(){
        _self.$obtainDefault.show();
        _self.$obtain.hide();
        _self.$timing.hide();
        _self.setObtainStyleFun();
    };
    //按钮状态改变为可编辑
    this.changeEdit = function(){
        _self.$obtainDefault.hide();
        _self.$obtain.show();
        _self.$timing.hide();
    };
    //获取验证码的点击事件
    this.obtainClick = function(){
        this.$obtain.click(function(){
            var isContinue;
            try {
                isContinue = _self.setting.obtainClick();
            }catch (e){

            }
            //判断是否要继续执行倒计时
            if(!isContinue){
                return false;
            }
            _self.$obtain.hide();
            _self.$timing.show();
            _self.countDown();
        });
    };

    //倒计时
    this.countDown = function(){
        if(0 == _self.timeNum){
            _self.clearTimeoutFun();
            _self.setObtainStyleFun();
            _self.changeEdit(); //20161024
            return;
        }else{
            _self.$timeBox.text(_self.timeNum);
            _self.timeNum -= 1;
        }
        self.timeoutId = setTimeout(function(){
            _self.countDown();
        },1000);
    };
    //清除setTimeout事件
    this.clearTimeoutFun = function(){
        //判断
        if(self.timeoutId){
            clearTimeout(self.timeoutId);
        }
    };
    //退出时把按钮设置为初始样子
    this.setObtainStyleFun = function(){
        _self.$timing.hide();
        _self.$obtain.hide();
        _self.$obtainDefault.show();
        _self.timeNum = 60;
        _self.clearTimeoutFun();
    };
    //初始化
    this.initFun = function(){
        this.obtainClick();
    };
    //执行初始化
    this.initFun();
};

/*
*手机号删除事件
*/
function mCommon_basicInputTel(options){
    this.setting = {
        //输入框外容器,jq对象,必填
        telInputBox:null,
        //手机号输入框删除回调事件,选填
        telDeleteClickCB:null,
        //内容改变的回调事件,选填
        inputChangeValCB:null
    };
    options = options || {};
    //合并参数
    $.extend(true, this.setting, options);

    if(!this.setting.telInputBox || this.setting.telInputBox.length == 0){
       return false;
    }

    //电话输入框旁的删除按钮
    this.$telDeleteBtn = this.setting.telInputBox.find('img');

    var self = this;

    //电话输入框旁的删除事件
    this.telDeleteClickFun = function(){
        this.$telDeleteBtn.click(function(){
            if('' != $.trim($(this).prev().val())){
                $(this).prev().val('');
                self.setting.telInputBox.addClass('mCommon_delPicHide');
                try{
                    self.setting.telDeleteClickCB();
                }catch (e){

                }
            }
        });
    };
    //当输入框的内容改变时判断删除按钮是否要隐藏
    this.setting.telInputBox.find('input').on('input propertychange',function() { //输入框内的文字改变
        if('' == $(this).val()){ //当输入的文字删除后,隐藏 清除图标
            self.setting.telInputBox.addClass('mCommon_delPicHide');
        }else{
            self.setting.telInputBox.removeClass('mCommon_delPicHide');
        }
        try {
            self.setting.inputChangeValCB();
        }catch (e){

        }
    });

    //初始化
    this.init = function(){
        this.telDeleteClickFun();
    };

    //执行初始化
    this.init();
}

以上可以直接用哦,当然要引入 jquery或 zepto;

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>  
或者
<script type="text/javascript" src="https://qiniu.woaap.com/base_cdn/zepto.min.js"></script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值