jQuery实现的内链接平滑滚动
不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$(
'a[href^="#"]'
).bind(
'click.smoothscroll'
,
function
(e) {
e.preventDefault();
var
anchor =
this
.hash,
$<span id=
"13_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"13_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=target&k0=target&kdi0=0&luki=8&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"13"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>target</span></a></span> = $(target);
$(
'<span id="14_nwp" style="width: auto; height: auto; float: none;"><a id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=html&k0=html&kdi0=0&luki=7&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0" target="_blank" mpid="14" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">html</span></a></span>, body'
).stop().animate({
'scrollTop'
: $target.offset().top
}, 500,
'swing'
,
function
() {
window.location.hash = anchor;
});
});
|
使用jQuery获取所有节点
1
2
3
4
5
6
7
|
var
$element = $(
'#gbtags'
);
var
$nodes = $element.contents();
$nodes.each(
function
() {
if
(
this
.nodeType === 3 && $.trim($(
this
).text())) {
$(
this
).<span id=
"12_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"12_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=wrap&k0=wrap&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"12"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>wrap</span></a></span>(
''
);
}
});
|
限制选择框选择个数
1
2
3
4
5
|
$(
"#categories option"
).click(
function
(e){
if
($(
this
).parent().val().length < 2) {
$(
this
).removeAttr(
"selected"
);
}
});
|
jQuery使用通配符来删除class
1
2
3
4
5
|
var
_c =
'your-icon-class'
$(
'.currency'
).removeClass (
function
(index, <span id=
"11_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"11_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=css&k0=css&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"11"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>css</span></a></span>) {
return
(css.match (/\bicon-\S+/g) || []).join(
' '
);
}).addClass(
'icon-'
+_c);
|
切换启用和禁用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/* HTML
|
|
<
input
type
=
"text"
value
=
"欢迎访问www.admin10000.com"
/><
input
type
=
"button"
value
=
"禁用<span id="
10_nwp"
style
=
"width: auto; height: auto; float: none;"
><
a
id
=
"10_nwl"
href
=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=%B0%B4%C5%A5&k0=%B0%B4%C5%A5&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target
=
"_blank"
mpid
=
"10"
style
=
"text-decoration: none;"
><
span
style
=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>按钮</
span
></
a
></
span
>" />
|
|
*/
// 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();
});
});
|
平滑滚动返回顶端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
h1
id
=
"anchor"
>admin10000.com</
h1
>
<
a
class
=
"topLink"
href
=
"#anchor"
>返回顶端</
a
>
$(document).ready(function () {
$("a.topLink").click(function () {
$("<
span
id
=
"9_nwp"
style
=
"width: auto; height: auto; float: none;"
><
a
id
=
"9_nwl"
href
=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=html&k0=html&kdi0=0&luki=7&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target
=
"_blank"
mpid
=
"9"
style
=
"text-decoration: none;"
><
span
style
=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>html</
span
></
a
></
span
>, body").animate({
scrollTop: $($(this).attr("href")).offset().top + "px"
}, {
duration: 500,
easing: "swing"
});
return false;
});
});
|
使用jQuery和Google Analytics来跟踪表单
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 +
'"'
]);
});
});
|
超简单的密码强度提示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$(
'#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'
).<span id=
"8_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"8_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=html&k0=html&kdi0=0&luki=7&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"8"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>html</span></a></span>(
'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生成一个自动停靠页尾效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
// 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.<span id=
"7_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"7_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=css&k0=css&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"7"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>css</span></a></span>({
position:
"absolute"
}).stop().animate({
top: footerTop
});
}
else
{
$footer.css({
position:
"static"
});
}
}
$(window)
.scroll(positionFooter)
.resize(positionFooter);
});
|
预防对表单进行多次提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$(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
;
}
});
});
|
图像等比例缩放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
$(window).bind(
"load"
, function() {
// IMAGE RESIZE
$(
'#product_cat_list <span id="4_nwp" style="width: auto; height: auto; float: none;"><a id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=img&k0=img&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0" target="_blank" mpid="4" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">img</span></a></span>'
).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
).<span id=
"5_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"5_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=css&k0=css&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"5"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>css</span></a></span>(
"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 <span id="6_nwp" style="width: auto; height: auto; float: none;"><a id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=img&k0=img&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">img</span></a></span>").show();
// IMAGE RESIZE
});
|
鼠标滑动时的渐入和渐出
1
2
3
4
5
6
7
8
9
|
$(document).ready(
function
(){
$(
".thumbs <span id="
2_nwp
" style="
width: auto; height: auto; float: none;
"><a id="
2_nwl
" href="
http:
//cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=img&k0=img&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0" target="_blank" mpid="2" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">img</span></a></span>").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 <span id="3_nwp" style="width: auto; height: auto; float: none;"><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=mouse&k0=mouse&kdi0=0&luki=1&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0" target="_blank" mpid="3" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">mouse</span></a></span>out
});
});
|
让整个DIV可以被点击
1
2
3
4
5
6
7
8
|
<
div
class
=
"myBox"
>
</
div
>
$(".myBox").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});
|
在新窗口打开链接 (target=”blank”)
1
2
3
4
5
6
7
8
|
$(
'a[@rel$='
external
']'
).click(
function
(){
this
.<span id=
"1_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"1_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=target&k0=target&kdi0=0&luki=8&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"1"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>target</span></a></span> =
"_blank"
;
});
/*
Usage:
<a href="http://www.admin10000.com" rel="external">admin10000.com</a>
*/
|
制作等高的列
1
2
3
4
5
|
var
max_height = 0;
$(
"div.col"
).each(
function
(){
if
($(
this
).height() > max_height) { max_height = $(
this
).height(); }
});
$(
"div.col"
).height(max_height);
|
图片预加载
1
2
3
4
5
6
7
8
9
10
11
12
13
|
(
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(
'<span id="0_nwp" style="width: auto; height: auto; float: none;"><a id="0_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=e2912f0e5f26992b&k=img&k0=img&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2b99265fe2f91e2&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3540%2Ehtml&urlid=0" target="_blank" mpid="0" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">img</span></a></span>'
);
cacheImage.src = arguments[i];
cache.push(cacheImage);
}
}
jQuery.preLoadImages(
"image1.gif"
,
"/path/to/image2.png"
);
|
获取 URL 中传递的参数
1
2
3
4
5
|
$.urlParam =
function
(name){
var
results =
new
RegExp(
'[\\?&]'
+ name +
'=([^&#]*)'
).exec(window.location.href);
if
(!results) {
return
0; }
return
results[1] || 0;
}
|
禁用表单的回车键提交
1
2
3
4
5
|
$(
"#form"
).keypress(
function
(e) {
if
(e.which == 13) {
return
false
;
}
});
|