jquery - jquery实用代码片段(一)
(2013-08-15 15:32:11)
标签:
jquery使用的代码片段it |
分类: jquery那些事 |
1.预加载图片
|
1
2
3
4
5
6
7
8
9
10
11
12
|
(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_x_x_x_x('img');
cacheImage.src
= arguments[i];
cache.push(cacheImage);
}
}
jQuery.preLoadImages("image1.gif", "/path/to/image2.png"); |
2. 让页面中的每个元素都适合在移动设备上展示
|
1
2
3
4
5
6
7
8
9
10
11
|
var scr
= document_createElement_x_x_x_x('script');
document.body.a(scr);
scr.onload = function(){
$('div').attr('class', '').attr('id', '').css({
'margin' :
0,
'padding' :
0,
'width': '100%',
'clear':'both'
});
}; |
3.图像等比例缩放
|
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
|
$(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
});
});
</script> 5.使用jQuery打造手风琴式的折叠效果
6.通过预加载图片廊中的上一幅下一幅图片来模仿Facebook的图片展示方式
7.使用jQuery和Ajax自动填充选择框
8.自动替换丢失的图片
9.在鼠标悬停时显示淡入/淡出特效
10.清空表单数据
7.使用jQuery和Ajax自动填充选择框
本文出自 |

加载中…