Skip to content

Instantly share code, notes, and snippets.

@simplelife7
simplelife7 / pageshow.js
Created January 19, 2016 11:23
【JS】判断页面是否从缓存取的onpageshow
window.addEventListener('pageshow', function(e) {
if (e.persisted) {// 从缓存加载
//...
}
}, false);
//jquery中没有这个对象,需要用原生事件对象
$(window).bind("pageshow", function(event) {
if (event.originalEvent.persisted) {
@simplelife7
simplelife7 / isSupportCss3Prop.js
Created October 26, 2015 17:58
【JS】检测是否支持CSS3属性
var _isSupport = function(prop){
var div = document.createElement('div'),
vendors = 'Khtml O Moz Webkit'.split(' '),
len = vendors.length;
if ( prop in div.style ) return true;
if ('-ms-' + prop in div.style) return true;
prop = prop.replace(/^[a-z]/, function(val) {
return val.toUpperCase();
});
@simplelife7
simplelife7 / traverse.js
Created July 9, 2015 16:42
【JS】遍历JSON所有节点
//your object
var o = {
foo:"bar",
arr:[1,2,3],
subo: {
foo2:"bar2"
}
};
//called with every property and it's value
@simplelife7
simplelife7 / setParmsValue.js
Last active May 10, 2017 03:15
【JS】URL参数活动、设置
//设置URL参数的方法
function setParmsValue(parms, parmsValue) {
var urlstrings = document.URL;
var args = GetUrlParms();
var values = args[parms];
//如果参数不存在,则添加参数
if (values == undefined) {
var query = location.search.substring(1); //获取查询串
//如果Url中已经有参数,则附加参数
if (query) {
@simplelife7
simplelife7 / getEnv.js
Created October 30, 2014 13:24
【JS】判断是手Q还是微信还是普通浏览器
function getEnv(){
var ua = navigator.userAgent.toLowerCase();
if(/micromessenger(\/[\d\.]+)*/.test(ua)){
return "weixin"
}else if(/qq\/(\/[\d\.]+)*/.test(ua) || /qzone\//.test(ua)){
return "qq";
}else{
return "web";
}
}
@simplelife7
simplelife7 / arrSortByField.js
Last active September 7, 2017 07:54
【JS】根据数组里的对象集合的某个字段对数组进行排序
var arr = [
{name:'hdj', age:28},
{name:'yim', age:25},
{name:'hdq', age:26},{name:'hdq', age:22},{name:'hdq', age:555},{name:'hdq', age:1111},{name:'hdq', age:99}
];
function arrSortByField(arr, field,order,primer){
@simplelife7
simplelife7 / prefix.js
Last active August 29, 2015 14:07
【JS】判断补全CSS3属性浏览器前缀
//source:http://www.webhek.com/vendor-prefix/
//return: {dom: "WebKit", lowercase: "webkit", css: "-webkit-", js: "Webkit"}
var prefix = (function () {
var styles = window.getComputedStyle(document.documentElement, ''),
pre = (Array.prototype.slice
.call(styles)
.join('')
.match(/-(moz|webkit|ms)-/) || (styles.OLink === '' && ['', 'o'])
)[1],
dom = ('WebKit|Moz|MS|O').match(new RegExp('(' + pre + ')', 'i'))[1];
@simplelife7
simplelife7 / transJson2str.js
Created September 18, 2014 06:23
【JS】JSON转换成字符串,兼容低级浏览器
function $transJson2str(o) {
if (o == undefined) {
return "";
}
var r = [];
if (typeof o == "string") return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
if (typeof o == "object") {
if (o.nodeName){
r.push(["document node", "nodeName:"+o.nodeName, "id:"+o.id, "class:"+o.className].join('-'));
r = "{" + r.join() + "}";
@simplelife7
simplelife7 / parse_json.js
Created September 18, 2014 06:12
【JS】字符串转换成JSON,兼容低级浏览器
function parseJSON( data ) {
var // JSON RegExp
rvalidchars = /^[\],:{}\s]*$/
, rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g
, rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g
, rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g
;
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
@simplelife7
simplelife7 / smartFloat.js
Created May 5, 2014 02:58
【JS】滚动吸顶
// 时间倒数吸顶
$.fn.smartFloat = function() {
var position = function(element) {
var top = element.position().top, pos = element.css("position");
$(window).scroll(function() {
var scrolls = $(this).scrollTop();
if (scrolls > top) {
if (window.XMLHttpRequest) {
element.css({
position: "fixed",