来源:GBin1.com

jQuery以其强大的功能和简单的使用成为了前端开发者最喜欢的JS类库,在这里我们分享一组实用的jQuery代码片段,希望大家喜欢!

jQuery平滑回到顶端效果

$(document).ready(function() {

    $("a.topLink").click(function() {
$("html, body").animate({
scrollTop: $($(this).attr("href")).offset().top + "px"
}, {
duration: 500,
easing: "swing"
});
return false;
}); });

运行代码:

GBdebug在线调试地址:http://www.gbin1.com/gb/debug/20864b59-a995-4318-a242-b3038f83f2c3.htm

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){
ratio = maxWidth / width;
$(this).css("width", maxWidth);
$(this).css("height", height * ratio);
height = height * ratio;
} if(height > maxHeight){
ratio = maxHeight / height;
$(this).css("height", maxHeight);
$(this).css("width", width * ratio);
width = width * ratio;
}
}); });

运行代码:

GBdebug在线调试地址:http://www.gbin1.com/gb/debug/5a2271a5-f363-4b34-8d2f-f0ad03121ced.htm

jQuery实现的滚动自动加载代码

var loading = false;
$(window).scroll(function(){
if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){
if(loading == false){
loading = true;
$('#loadingbar').css("display","block");
$.get("load.php?start="+$('#loaded_max').val(), function(loaded){
$('body').append(loaded);
$('#loaded_max').val(parseInt($('#loaded_max').val())+50);
$('#loadingbar').css("display","none");
loading = false;
});
}
}
}); $(document).ready(function() {
$('#loaded_max').val(50);
});

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!');
} else if (mediumRegex.test($(this).val())) {
$('#passstrength').className = 'alert';
$('#passstrength').html('Medium!');
} else {
$('#passstrength').className = 'error';
$('#passstrength').html('Weak!');
}
return true;
});

运行代码:

GBdebug在线调试地址:http://www.gbin1.com/gb/debug/5ae6bca8-b12b-48b8-861a-8d9ab09e6641.htm

jQuery实现的DIv高度保持一致

var maxHeight = 0;

$("div").each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
}); $("div").height(maxHeight);

运行代码:

GBdebug在线调试地址:http://www.gbin1.com/gb/debug/ac7c5175-9282-41c0-a766-c56707946c7b.htm

简单处理IE6上PNG格式文件

$('.pngfix').each( function() {
$(this).attr('writing-mode', 'tb-rl');
$(this).css('background-image', 'none');
$(this).css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="path/to/image.png",sizingMethod="scale")');
});

将class=pngfix添加到需要处理的对象即可。

jQuery处理JSON

function parseJson(){
//Start by calling the json object, I will be using a
//file from my own site for the tutorial
//Then we declare a callback function to process the data
$.getJSON('hcj.json',getPosts); //The process function, I am going to get the title,
//url and excerpt for 5 latest posts
function getPosts(data){ //Start a for loop with a limit of 5
for(var i = 0; i < 5; i++){
//Build a template string of
//the post title, url and excerpt
var strPost = '<h2>'
+ data.posts[i].title
+ '</h2>'
+ '<p>'
+ data.posts[i].excerpt
+ '</p>'
+ '<a href="'
+ data.posts[i].url
+ '" title="Read '
+ data.posts[i].title
+ '">Read ></a>'; //Append the body with the string
$('body').append(strPost); }
} } //Fire off the function in your document ready
$(document).ready(function(){
parseJson();
});

jQuery实现让整个div可以被点击

$(".myBox").click(function(){      window.location=$(this).find("a").attr("href");       return false; });

运行代码:

GBdebug在线调试地址:http://www.gbin1.com/gb/debug/4bfb3dce-1681-470c-b47a-7490df12f3b0.htm

jQuery实现的Facebook风格的图片预加载效果

var nextimage = "http://www.gbtags.com/gb/networks/uploadimgthumb/55d67b14-fb25-45e7-acc8-211a41047ef0.jpg";
$(document).ready(function(){
window.setTimeout(function(){
var img = $("<img>").attr("src", nextimage).load(function(){
$('div').append(img);
});
}, 100);
});

运行代码:

GBdebug在线调试地址:http://www.gbin1.com/gb/debug/b1a87e30-e33f-4369-92fc-55e8fd628816.htm

希望大家喜欢这些实用的jQuery代码,如果你也有更多的jQuery代码片段,请与我们分享,谢谢!

via 极客社区

来源:分享10个超实用的jQuery代码片段

分享10个超实用的jQuery代码片段的更多相关文章

  1. 超实用的jQuery代码片段

    1.jQuery回到顶部效果 HTML代码:<a href="javascript:;" id="btn" title="回到顶部"& ...

  2. 经验分享:10个简单实用的 jQuery 代码片段

    尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...

  3. 10个简单实用的 jQuery 代码片段

    尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库. 今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 1.平滑滚动到 ...

  4. 高效Web开发的10个jQuery代码片段(10 JQUERY SNIPPETS FOR EFFICIENT WEB DEVELOPMENT)

    在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...

  5. 10个可以直接拿来用的JQuery代码片段

    jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画.特效,还会提高网站的用户体验. 本文收集了10段非常实用的jQue ...

  6. Web开发者必须知道的10个jQuery代码片段

    在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...

  7. Jquery学习总结(4)——高效Web开发的10个jQuery代码片段

    在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...

  8. 最实用、最常用的jQuery代码片段

    // chinacoder.cn JavaScript Document $(document).ready(function() { //.filter(":not(:has(.selec ...

  9. 50个jquery代码片段(转)

    本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...

随机推荐

  1. 数据库的主从复制常用Xshell命令

    mysql配置 1.设置数据库用户名和密码 mysqladmin -u root password "root" 2.打开3306端口号 iptables -I INPUT -p ...

  2. 原来Notepad++也有列模式(转)

    引子 一直在用Notepad++,小巧.顺手.偶尔使用UltraEdit来处理列模式:UE越来越大,启动时间太长,早都烦了.今天上网,偶然间看到,Notepad++也有列模式.拜拜UE,彻底删除你. ...

  3. 在sublime Text 3上编写并运行java程序

    参考 首先肯定是安装JDK配置环境变量,这个就不多说了. 第二步下载和安装sublime Text3. 第三步,创建一个批处理文件,命名为runJava.bat,放在JDK的bin下: @ECHO O ...

  4. Bzoj 2286 & Luogu P2495 消耗战(LCA+虚树+欧拉序)

    题面 洛谷 Bzoj 题解 很容易想到$O(nk)$的树形$dp$吧,设$f[i]$表示处理完这$i$颗子树的最小花费,同时再设一个$mi[i]$表示$i$到根节点$1$路径上的距离最小值.于是有: ...

  5. ARP扫描工具arp-scan

    ARP扫描工具arp-scan   arp-scan是Kali Linux自带的一款ARP扫描工具.该工具可以进行单一目标扫描,也可以进行批量扫描.批量扫描的时候,用户可以通过CIDR.地址范围或者列 ...

  6. WebService协议

    http://www.cnblogs.com/lm3515/archive/2011/03/17/1987009.html http://blog.csdn.net/chjttony/article/ ...

  7. JZYZOJ 1388 旅游 状压dp

    http://172.20.6.3/Problem_Show.asp?id=1388   求拓扑排序方案数 状压dp,最开始以为是拓扑排序加数论或者搜索,没想到是状压dp,突然气死.jpg: 完全没有 ...

  8. List the Books

    描述 Jim is fond of reading books, and he has so many books that sometimes it's hard for him to manage ...

  9. Problem A: 指针:调用自定义交换函数,完成三个数整从小到大排列

    #include<stdio.h> int swap(int *p1,int *p2) { int flag; if(*p1>*p2) { flag=*p1; *p1=*p2; *p ...

  10. 十一. 图形、图像与多媒体4.Graphics类的绘图方法

    Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力.本节讲解Graphics类,下节讲解Graphics2D. Graphics类提供基本的几何图形绘制方法,主要有:画线段 ...