JavaScript 实现 Toast 吐司效果

JS

<script>
function Toast(msg,duration){
    duration=isNaN(duration)?3000:duration;
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText="width: 60%;min-width: 150px;opacity: 0.7;height: 30px;color: rgb(255, 255, 255);line-height: 30px;text-align: center;border-radius: 5px;position: fixed;top: 40%;left: 20%;z-index: 999999;background: rgb(0, 0, 0);font-size: 12px;";
    document.body.appendChild(m);
    setTimeout(function() {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function() { document.body.removeChild(m) }, d * 1000);
    }, duration);
}
</script>

HTML

<html>
	<head>
	<style>
	body {
		font-family: 'Lucida Grande', 'Helvetica', sans-serif;
	}
	</style>
	</head>
	<body>
	<button onclick="Toast('这是一个吐司',2000);">Toast</button>
	</body>
</html>

jQuery 短信获取倒计时

<head>
	<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
</head>

<input id="smspwd" type="button" value="短信获取" onclick='getSMS()'>

<script type="text/javascript">		
	function getSMS(){
		console.log(1)
		$("#smspwd").smsCountdown({begin: 10})
	}
</script>


<script type="text/javascript">		
	$.fn.extend({
		smsCountdown:function(){
			var params = {
				begin: arguments[0].begin || 0,
				way: arguments[0].way || 'input'
			};
			var $me = this;
			if(params.begin>0){
				$me.attr('disabled',true);
				var count = params.begin;		
				if(params.way == 'button'){
					var resend = setInterval(function(){
						count--;
						if (count > 0){
							$me.html("\u7b49\u5f85"+count+"\u79d2");
						}else {
							clearInterval(resend);
							$me.removeAttr("disabled").html("\u77ed\u4fe1\u83b7\u53d6");
						}
					}, 1000);
				}else{
					var resend = setInterval(function(){
						count--;
						if (count > 0){
							$me.val("\u7b49\u5f85"+count+"\u79d2");
						}else {
							clearInterval(resend);
							$me.removeAttr("disabled").val("\u77ed\u4fe1\u83b7\u53d6");
						}
					}, 1000);
				}
			}else{
				if(params.way == 'button'){
					$me.removeAttr("disabled").html("\u77ed\u4fe1\u83b7\u53d6");
				}else{
					$me.removeAttr("disabled").val("\u77ed\u4fe1\u83b7\u53d6");
				}
			}
		},
	})
</script>

JavaScript POST提交数据并跳转

jQuery

    $.extend({
        StandardPost:function(url,args){
            console.log(args)
            var body = $(document.body),
                form = $("<form method='post'></form>"),
                input;
            form.attr({"action":url});
            $.each(args,function(key,value){
                input = $("<input type='hidden'>");
                input.attr({"name":key});
                input.val(value);
                form.append(input);
            });

            form.appendTo(document.body);
            form.submit();
            document.body.removeChild(form[0]);
        }
    });

via: https://segmentfault.com/q/1010000000473230

判断当前浏览器是否为微信内置浏览器 MicroMessenger

通过微信内置浏览器的 User Agent

首先我们通过 PHP 内置的 $_SERVER[“HTTP_USER_AGENT”] server 数组来获取 User Agent。

iPhone 通过微信内置浏览器访问网页时得到 User Agent 是:

Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1

Android 通过微信内置浏览器访问网页时得到 User Agent 是:

Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255

JS 判断

function is_weixin(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        return true;
     } else {
        return false;
    }
}

PHP 判断

function is_weixin(){ 
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            return true;
    }    
    return false;
}