Skip to content

Instantly share code, notes, and snippets.

View wangwen1220's full-sized avatar

Steven wangwen1220

View GitHub Profile
@wangwen1220
wangwen1220 / js-browser-test.js
Created January 4, 2014 01:34
JS: 检测浏览器 | test browser
/* IE 6 */
(checkIE = document.createElement("b")).innerHTML = "<!--[if IE 6]><i></i><![endif]-->";
var isIE = checkIE.getElementsByTagName("i").length == 1;
/* IE 7 */
(checkIE = document.createElement("b")).innerHTML = "<!--[if IE 7]><i></i><![endif]-->";
var isIE = checkIE.getElementsByTagName("i").length == 1;
navigator.appVersion.indexOf("MSIE 7.")!=-1
/* IE 8 */
@wangwen1220
wangwen1220 / js-oninput.js
Created January 4, 2014 01:43
JS: 实时监听输入框值变化的完美方案 | oninput onpropertychange
$('textarea').bind('input propertychange', function() {
$('.msg').html($(this).val().length + ' characters');
});
@wangwen1220
wangwen1220 / js-smoothscroll.js
Last active January 2, 2016 10:49
JS: jQuery 实现的内部链接平滑滚动 | smoothscroll web links
// jQuery 实现的内部链接平滑滚动
$('a[href^="#"]').bind('click.smoothscroll', function(e) {
e.preventDefault();
var anchor = this.hash;
var $target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 500, 'swing', function() {
window.location.hash = anchor;
@wangwen1220
wangwen1220 / js-contents-each.js
Last active January 2, 2016 10:58
JS: jQuery 处理所有节点文本 | element contents each
// jQuery 处理所有节点文本
$('#elem').contents().each(function() {
if(this.nodeType === 3 && $.trim($(this).text())) {
$(this).wrap('');
}
});
@wangwen1220
wangwen1220 / js-select-length.js
Last active January 2, 2016 10:58
JS: jQuery 限制选择框选择个数 | limit select length
// jQuery 限制选择框选择个数
$("#categories option").click(function(e) {
if ($(this).parent().val().length < 2) {
$(this).removeAttr("selected");
}
});
@wangwen1220
wangwen1220 / js-gotop.js
Last active January 2, 2016 10:59
JS: jQuery 平滑回到顶端链接 | gotop back to top
// jQuery 平滑回到顶端链接
$(function() {
$("a.gotop").click(function() {
$("html, body").animate({
scrollTop: $($(this).attr("href")).offset().top + "px"
}, {
duration: 500,
easing: "swing"
});
return false;
@wangwen1220
wangwen1220 / js-img-size.js
Last active January 2, 2016 10:59
JS: jQuery 处理图片尺寸 | set img size
// jQuery 处理图片尺寸
$(window).bind("load", function() {
$('#imglist img').each(function() {
var maxWidth = 120;
var maxHeight = 120;
var ratio = 0;
var width = $(this).width();
var height = $(this).height();
if (width > maxWidth) {
@wangwen1220
wangwen1220 / js-password-strong.js
Last active January 2, 2016 10:59
JS: jQuery 测试密码强度 | password strong test
// jQuery 测试密码强度
$('#pass').keyup(function(e) {
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
if (false == enoughRegex.test($(this).val())) {
$('#passstrength').html('More Characters');
} else if (strongRegex.test($(this).val())) {
$('#passstrength').className = 'ok';
$('#passstrength').html('Strong!');
@wangwen1220
wangwen1220 / js-div-maxheight.js
Created January 7, 2014 02:44
JS: jQuery 实现的元素高度保持一致 | div maxheight
var maxHeight = 0;
$("div").each(function() {
if ($(this).height() > maxHeight) maxHeight = $(this).height();
});
$("div").height(maxHeight);
@wangwen1220
wangwen1220 / js-img-preload-facebook.js
Last active January 2, 2016 10:59
JS: jQuery 实现的 Facebook 风格的图片预加载效果 | img preload facebook
// jQuery 实现的 Facebook 风格的图片预加载效果
var preloadimg = "preload.jpg";
$(function() {
window.setTimeout(function() {
var img = $("<img>").attr("src", preloadimg).load(function() {
$('div').append(img);
});
}, 100);
});