很棒的jQuery代码片段分享
jQuery实现的内链接平滑滚动
不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动
$(
'a[href^="#"]'
).bind(
'click.smoothscroll'
,
function
(e) {
e.preventDefault();
var
anchor =
this
.hash,
$target = $(target);
$(
'html, body'
).stop().animate({
'scrollTop'
: $target.offset().top
}, 500,
'swing'
,
function
() {
window.location.hash = anchor;
});
});
使用jQuery获取所有节点
var
$element = $(
'#gbtags'
);
var
$nodes = $element.contents();
$nodes.each(
function
() {
if
(
this
.nodeType === 3 && $.trim($(
this
).text())) {
$(
this
).wrap(
''
);
}
});
限制选择框选择个数
$(
"#categories option"
).click(
function
(e){
if
($(
this
).parent().val().length < 2) {
$(
this
).removeAttr(
"selected"
);
}
});
jQuery使用通配符来删除class
var
_c =
'your-icon-class'
$(
'.currency'
).removeClass (
function
(index, css) {
return
(css.match (/\bicon-\S+/g) || []).join(
' '
);
}).addClass(
'icon-'
+_c);
切换启用和禁用
/* HTML
|
|
<
input
type
=
"text"
value
=
"欢迎访问www.admin10000.com"
/><
input
type
=
"button"
value
=
"禁用按钮"
/>
|
|
*/
// Plugin
(function ($) {
$.fn.toggleDisabled = function () {
return this.each(function () {
var $this = $(this);
if ($this.attr('disabled')) $this.removeAttr('disabled');
else $this.attr('disabled', 'disabled');
});
};
})(jQuery);
// TEST
$(function () {
$('input:button').click(function () {
$('input:text').toggleDisabled();
});
});
平滑滚动返回顶端
<
h1
id
=
"anchor"
>admin10000.com</
h1
>
<
a
class
=
"topLink"
href
=
"#anchor"
>返回顶端</
a
>
$(document).ready(function () {
$("a.topLink").click(function () {
$("html, body").animate({
scrollTop: $($(this).attr("href")).offset().top + "px"
}, {
duration: 500,
easing: "swing"
});
return false;
});
});
使用jQuery和Google Analytics来跟踪表单
var
array1 = [];
$(document).ready(
function
() {
$(
'input'
).change(
function
() {
var
formbox = $(
this
).attr(
'id'
);
array1.push(formbox);
console.log(
"you filled out box "
+ array1);
});
$(
'#submit'
).click(
function
() {
console.log(
'tracked '
+ array1);
//alert('this is the order of boxes you filled out: ' + array1);
_gaq.push([
'_trackEvent'
,
'Form'
,
'completed'
,
'"'
+ array1 +
'"'
]);
});
});
超简单的密码强度提示
$(
'#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
;
});
jQuery生成一个自动停靠页尾效果
// Window load event used just in case window height is dependant upon images
$(window).bind(
"load"
,
function
() {
var
footerHeight = 0,
footerTop = 0,
$footer = $(
"#footer"
);
positionFooter();
function
positionFooter() {
footerHeight = $footer.height();
footerTop = ($(window).scrollTop() + $(window).height() - footerHeight) +
"px"
;
/* DEBUGGING
console.log("Document height: ", $(document.body).height());
console.log("Window height: ", $(window).height());
console.log("Window scroll: ", $(window).scrollTop());
console.log("Footer height: ", footerHeight);
console.log("Footer top: ", footerTop);
*/
if
(($(document.body).height() + footerHeight) < $(window).height()) {
$footer.css({
position:
"absolute"
}).stop().animate({
top: footerTop
});
}
else
{
$footer.css({
position:
"static"
});
}
}
$(window)
.scroll(positionFooter)
.resize(positionFooter);
});
预防对表单进行多次提交
$(document).ready(
function
() {
$(
'form'
).submit(
function
() {
if
(
typeof
jQuery.data(
this
,
"disabledOnSubmit"
) ==
'undefined'
) {
jQuery.data(
this
,
"disabledOnSubmit"
, { submited:
true
});
$(
'input[type=submit], input[type=button]'
,
this
).each(
function
() {
$(
this
).attr(
"disabled"
,
"disabled"
);
});
return
true
;
}
else
{
return
false
;
}
});
});
图像等比例缩放
$(window).bind(
"load"
, function() {
// IMAGE RESIZE
$(
'#product_cat_list 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;
}
var width = $(
this
).width();
var height = $(
this
).height();
if
(height > maxHeight){
ratio = maxHeight / height;
$(
this
).css(
"height"
, maxHeight);
$(
this
).css(
"width"
, width * ratio);
width = width * ratio;
}
});
//$("#contentpage img").show();
// IMAGE RESIZE
});
鼠标滑动时的渐入和渐出
$(document).ready(
function
(){
$(
".thumbs img"
).fadeTo(
"slow"
, 0.6);
// This sets the opacity of the thumbs to fade down to 60% when the page loads
$(
".thumbs img"
).hover(
function
(){
$(
this
).fadeTo(
"slow"
, 1.0);
// This should set the opacity to 100% on hover
},
function
(){
$(
this
).fadeTo(
"slow"
, 0.6);
// This should set the opacity back to 60% on mouseout
});
});
让整个DIV可以被点击
<
div
class
=
"myBox"
>
</
div
>
$(".myBox").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});
图片预加载
(
function
($) {
var
cache = [];
// Arguments are image paths relative to the current page.
$.preLoadImages =
function
() {
var
args_len = arguments.length;
for
(
var
i = args_len; i--;) {
var
cacheImage = document.createElement(
'img'
);
cacheImage.src = arguments[i];
cache.push(cacheImage);
}
}
jQuery.preLoadImages(
"image1.gif"
,
"/path/to/image2.png"
);
获取 URL 中传递的参数
$.urlParam =
function
(name){
var
results =
new
RegExp(
'[\\?&]'
+ name +
'=([^&#]*)'
).exec(window.location.href);
if
(!results) {
return
0; }
return
results[1] || 0;
}
禁用表单的回车键提交
$(
"#form"
).keypress(
function
(e) {
if
(e.which == 13) {
return
false
;
}
});
很棒的jQuery代码片段分享的更多相关文章
- 十条很实用的jQuery代码片段
本文转自:http://developer.51cto.com/art/201604/509093.htm 作者:核子可乐译来源:51CTO 原文标题:10 jQuery Snippets for E ...
- 很实用的JQuery代码片段(转)
1 元素屏幕居中 jQuery.fn.center = function () { this.css("position","absolute"); this. ...
- 经验分享:10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...
- 分享10个超实用的jQuery代码片段
来源:GBin1.com jQuery以其强大的功能和简单的使用成为了前端开发者最喜欢的JS类库,在这里我们分享一组实用的jQuery代码片段,希望大家喜欢! jQuery平滑回到顶端效果 $(doc ...
- 10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库. 今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 1.平滑滚动到 ...
- 高效Web开发的10个jQuery代码片段(10 JQUERY SNIPPETS FOR EFFICIENT WEB DEVELOPMENT)
在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...
- 50个jquery代码片段(转)
本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...
- 20+个可重复使用的jQuery代码片段
jQuery已经成为任何web项目的重要组成部分.它为网站提供了交互性的通过移动HTML元素,创建自定义动画,处理事件,选择DOM元素,检索整个document ,让最终用户有一个更好的体验. 在这篇 ...
- 50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段
50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段 本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从j ...
随机推荐
- notepad++使用心得
最近和网友谈论软件编译器的时候,很多人都推荐使用notepad++这款软件,于是怀着一份好奇心安装了这款软件耍了耍,感觉确实不错.特将网友的分享以及自己的心得总结如下: 1)首先notepad++支持 ...
- Windows开发技术的历史
原文地址:http://www.kuqin.com/windows/20090315/40172.html Windows已经有22年的历史,这22年来,微软官方主力推行的编程语言与API有四个分水岭 ...
- [BZOJ 1084] [SCOI2005] 最大子矩阵 【DP】
题目链接:BZOJ - 1084 题目分析 我看的是神犇BLADEVIL的题解. 1)对于 m = 1 的情况, 首先可能不取 Map[i][1],先 f[i][k] = f[i - 1][k]; ...
- Computational Methods in Bayesian Analysis
Computational Methods in Bayesian Analysis Computational Methods in Bayesian Analysis [Markov chain ...
- javascript design patterns
http://jsdesignpatterns.com/ http://www.joezimjs.com/tag/design-patterns/ http://codecube.net/#archi ...
- IE7和IE8出现的计算判断问题
吸住底部菜单 IE7和IE8下会卡死的算法 ; } function fixedBar(){ var _height=$(this).height()+$(this).scrollTop(); var ...
- Linux Kernel 空指针逆向引用拒绝服务漏洞
漏洞名称: Linux Kernel 空指针逆向引用拒绝服务漏洞 CNNVD编号: CNNVD-201306-449 发布时间: 2013-07-01 更新时间: 2013-07-01 危害等级: ...
- jquery禁用右键、文本选择功能、刷新
//禁用右键.文本选择功能.刷新 $(document).bind(“contextmenu”,function(){return false;}); $(document).bind(“select ...
- 图论(网络流):SCOI 2007 修车
同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待的时间最小 ...
- openssl生成RSA格式,并转为pkcs8格式
原文地址:http://www.thinkingquest.net/articles/391.html?utm_source=tuicool 支付宝接口开发相关:openssl 加密工具 支付宝“手机 ...