jQuery已经成为任何web项目的重要组成部分。它为网站提供了交互性的通过移动HTML元素,创建自定义动画,处理事件,选择DOM元素,检索整个document ,让最终用户有一个更好的体验。
在这篇文章中我已经收集了20 +个可重复使用的jQuery代码片段,你可以很容易地复制并直接粘贴到你的项目中。

图片的延迟加载
1 |
jQuery(document).ready(function() { |
2 |
jQuery("img.lazy").lazy({ |
Source
预先载入图像
03 |
// Arguments are image paths relative to the current page. |
04 |
$.preLoadImages = function() { |
05 |
var args_len = arguments.length; |
06 |
for (var i = args_len; i--;) { |
07 |
var cacheImage = document.createElement('img'); |
08 |
cacheImage.src = arguments[i]; |
09 |
cache.push(cacheImage); |
Source
部分页面刷新
1 |
setInterval(function() { |
2 |
$("#refresh").load(location.href+" #refresh>*",""); |
Source
延迟动画/效果
1 |
$(".alert").delay(2000).fadeOut(); |
Source
Open external link in New Window
01 |
$('a').each(function() { |
02 |
var a = new RegExp('/' + window.location.host + '/'); |
03 |
if(!a.test(this.href)) { |
04 |
$(this).click(function(event) { |
05 |
event.preventDefault(); |
06 |
event.stopPropagation(); |
07 |
window.open(this.href, '_blank'); |
Source
Make Everything Mobile Friendly
01 |
var scr = document.createElement('script'); |
02 |
scr.setAttribute('src', 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/ |
04 |
document.body.appendChild(scr); |
06 |
scr.onload = function(){ |
08 |
$('div').attr('class', '').attr('id', '').css({ |
Source
Image Resize Using jQuery
01 |
$(window).bind("load", function() { |
03 |
$('#product_cat_list img').each(function() { |
07 |
var width = $(this).width(); |
08 |
var height = $(this).height(); |
11 |
ratio = maxWidth / width; |
12 |
$(this).css("width", maxWidth); |
13 |
$(this).css("height", height * ratio); |
14 |
height = height * ratio; |
16 |
var width = $(this).width(); |
17 |
var height = $(this).height(); |
18 |
if(height > maxHeight){ |
19 |
ratio = maxHeight / height; |
20 |
$(this).css("height", maxHeight); |
21 |
$(this).css("width", width * ratio); |
22 |
width = width * ratio; |
25 |
//$("#contentpage img").show(); |
Source
Smooth Scrolling
02 |
$('a[href*=#]:not([href=#])').click(function() { |
03 |
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') |
04 |
&& location.hostname == this.hostname) { |
05 |
var target = $(this.hash); |
06 |
target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); |
08 |
$('html,body').animate({ |
09 |
scrollTop: target.offset().top |
Source
Window load event with minimum delay
05 |
$(window).load(function() { |
07 |
if (+new Date - fn.now < 500) setTimeout(fn, 500); |
Source
jQuery Accordion
03 |
var allPanels = $('.accordion > dd').hide(); |
05 |
$('.accordion > dt > a').click(function() { |
07 |
$(this).parent().next().slideDown(); |
Source
Simple Auto-Playing Slideshow
01 |
$("#slideshow > div:gt(0)").hide(); |
03 |
setInterval(function() { |
04 |
$('#slideshow > div:first') |
09 |
.appendTo('#slideshow'); |
Source
Shuffle DOM Elements
03 |
$.fn.shuffle = function() { |
05 |
var allElems = this.get(), |
06 |
getRandom = function(max) { |
07 |
return Math.floor(Math.random() * max); |
09 |
shuffled = $.map(allElems, function(){ |
10 |
var random = getRandom(allElems.length), |
11 |
randEl = $(allElems[random]).clone(true)[0]; |
12 |
allElems.splice(random, 1); |
16 |
this.each(function(i){ |
17 |
$(this).replaceWith($(shuffled[i])); |
Source
Scroll Page Horizontally With Mouse Wheel
03 |
$("body").mousewheel(function(event, delta) { |
05 |
this.scrollLeft -= (delta * 30); |
07 |
event.preventDefault(); |
Source
Load Only a Section of a Page
1 |
$("#mainNav").load("/store #mainNav") |
Source
Highlight Related Label when Input in Focus
1 |
$("form :input").focus(function() { |
2 |
$("label[for='" + this.id + "']").addClass("labelfocus"); |
4 |
$("label").removeClass("labelfocus"); |
Source
Highlight All Links To Current Page
2 |
$("a").each(function(){ |
3 |
if ($(this).attr("href") == window.location.pathname){ |
4 |
$(this).addClass("selected"); |
Source
Better Broken Image Handling
2 |
$('img').error(function(){ |
3 |
$(this).attr('src', 'missing.png'); |
7 |
$("img").error(function(){ |
Source
Load Content on Scroll Automatically
02 |
$(window).scroll(function(){ |
03 |
if((($(window).scrollTop()+$(window).height())+250)>=$(document). |
07 |
$('#loadingbar').css("display","block"); |
08 |
$.get("load.php?start="+$('#loaded_max').val(), |
10 |
$('body').append(loaded); |
11 |
$('#loaded_max').val(parseInt($('#loaded_max') |
13 |
$('#loadingbar').css("display","none"); |
20 |
$(document).ready(function() { |
21 |
$('#loaded_max').val(50); |
Source
Prevent Multiple Submit of Your Form
01 |
$(document).ready(function() { |
02 |
$('form').submit(function() { |
03 |
if(typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') { |
04 |
jQuery.data(this, "disabledOnSubmit", { submited: true }); |
05 |
$('input[type=submit], input[type=button]', this).each(function() { |
06 |
$(this).attr("disabled", "disabled"); |
Source
Make Entire Div Clickable
1 |
$(".myBox").click(function(){ |
2 |
window.location=$(this).find("a").attr("href"); |
Source
Toggle Text
1 |
$("#more-less-options-button").click(function() { |
2 |
var txt = $("#extra-options").is(':visible') ? 'more options': 'less |
4 |
$("#more-less-options-button").text(txt); |
5 |
$("#extra-options").slideToggle(); |
Source
- 50个jquery代码片段(转)
本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...
- 可以直接拿来用的15个jQuery代码片段
jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画.特效,还会提高网站的用户体验. 本文收集了15段非常实用的jQue ...
- 50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段
50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段 本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从j ...
- 经验分享:10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...
- 高效Web开发的10个jQuery代码片段(10 JQUERY SNIPPETS FOR EFFICIENT WEB DEVELOPMENT)
在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...
- 10个可以直接拿来用的JQuery代码片段
jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画.特效,还会提高网站的用户体验. 本文收集了10段非常实用的jQue ...
- 10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库. 今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 1.平滑滚动到 ...
- 【转】jQuery代码片段备用
在CSDN看到的,记下备用.原文:http://www.csdn.net/article/2013-07-16/2816238-15-jquery-code-snippets-for-develope ...
- 一些实用的JQuery代码片段收集
本文将展示50个非常实用的JQuery代码片段,这些代码能够给你的JavaScript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够 ...
随机推荐
- NuGet 的使用
install-package entityframework//Enable-Migrations -ContextTypeName College.Models.CollegeEntities ...
- wpf的datepicker处理
如果有2个datepicker,控制时间起和止的话,可以把第二个datepicker加一个属性,DisplayDateStart = "{Binding SelectedDate,Eleme ...
- iOS中远程推送实现—在Apple的生产环境上测试Push Notifications功能
1.在“Provisioning Profiles”中点击“Add”按钮. 2.在“What type of provisioning profile do you need?”页面中选择“Distr ...
- java与IOS之间的RSA加解密
很简单的一个需求,ipad端给密码RSA加密,传到java后台,解密.RSA加密算法是基于一个密钥对的,分为公钥和私钥,一般情况公钥加密,私钥解密,但也可私钥加密,公钥解密.还可以验签,就是先用私钥对 ...
- Python实现kNN(k邻近算法)
Python实现kNN(k邻近算法) 运行环境 Pyhton3 numpy科学计算模块 计算过程 st=>start: 开始 op1=>operation: 读入数据 op2=>op ...
- qt 焦点设置策略
focusPolicy 一个QWidget获得焦点的方式受 focusPolicy 控制 Qt::TabFocus 通过Tab键获得焦点 Qt::ClickFocus 通过被单击获得焦点 Qt::St ...
- 20145120 《Java程序设计》第10周学习总结
20145120 <Java程序设计>第10周学习总结 教材学习内容总结 转自:http://www.cnblogs.com/springcsc/archive/2009/12/03/16 ...
- 【每日scrum】NO.3
1.感觉需求分析没有想象的那么简单,今天由于某些原因没有完成.
- 4、android xml中drawableTop(drawableBoottom、drawableLeft、drawableRight)在java代码中的动态配置
做安卓开发的朋友都知道,我们在xml中可以通过这样来对button设置其上部或者(下.左.右)的图片资源: 那么如果需要动态配置图片呢?我们不得不使用java代码来进行操作: Drawable dra ...
- IOS常用加密DES
NSString+DES.h // // NSString+DES.h // haochang // // Created by Administrator on 14-4-15. // Copyri ...