开发中可能会用到的几个 jQuery 小提示和技巧
1) 禁止右键
1 $(document).ready(function() {
2 //catch the right-click context menu
3 $(document).bind("contextmenu",function(e) {
4 //warning prompt - optional
5 alert("No right-clicking!");
6
7 //delete the default context menu
8 return false;
9 });
10 });
2 //catch the right-click context menu
3 $(document).bind("contextmenu",function(e) {
4 //warning prompt - optional
5 alert("No right-clicking!");
6
7 //delete the default context menu
8 return false;
9 });
10 });
2) 文本缩放
$(document).ready(function() {
//find the current font size
var originalFontSize = $('html').css('font-size');
//Increase the text size
$(".increaseFont").click(function() {
var currentFontSize = $('html').css('font-size');
var currentFontSizeNumber = parseFloat(currentFontSize, 10);
var newFontSize = currentFontSizeNumber*1.2;
$('html').css('font-size', newFontSize);
return false;
});
//Decrease the Text Size
$(".decreaseFont").click(function() {
var currentFontSize = $('html').css('font-size');
var currentFontSizeNum = parseFloat(currentFontSize, 10);
var newFontSize = currentFontSizeNum*0.8;
$('html').css('font-size', newFontSize);
return false;
});
// Reset Font Size
$(".resetFont").click(function(){
$('html').css('font-size', originalFontSize);
});
});
//find the current font size
var originalFontSize = $('html').css('font-size');
//Increase the text size
$(".increaseFont").click(function() {
var currentFontSize = $('html').css('font-size');
var currentFontSizeNumber = parseFloat(currentFontSize, 10);
var newFontSize = currentFontSizeNumber*1.2;
$('html').css('font-size', newFontSize);
return false;
});
//Decrease the Text Size
$(".decreaseFont").click(function() {
var currentFontSize = $('html').css('font-size');
var currentFontSizeNum = parseFloat(currentFontSize, 10);
var newFontSize = currentFontSizeNum*0.8;
$('html').css('font-size', newFontSize);
return false;
});
// Reset Font Size
$(".resetFont").click(function(){
$('html').css('font-size', originalFontSize);
});
});
3) 在新窗口打开链接
1 $(document).ready(function() {
2 //select all anchor tags that have http in the href
3 //and apply the target=_blank
4 $("a[href^='http']").attr('target','_blank');
5 });
2 //select all anchor tags that have http in the href
3 //and apply the target=_blank
4 $("a[href^='http']").attr('target','_blank');
5 });
4) 样式表切换
你知道网站换肤是怎么做的吗?下面的代码可以帮助你实现样式表切换功能,如下:
1 $(document).ready(function() {
2 $("a.cssSwap").click(function() {
3 //swap the link rel attribute with the value in the rel
4 $('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
5 });
6 });
2 $("a.cssSwap").click(function() {
3 //swap the link rel attribute with the value in the rel
4 $('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
5 });
6 });
5) 回到顶部
1 $(document).ready(function() {
2 //when the id="top" link is clicked
3 $('#top').click(function() {
4 //scoll the page back to the top
5 $(document).scrollTo(0,500);
6 }
7 });
2 //when the id="top" link is clicked
3 $('#top').click(function() {
4 //scoll the page back to the top
5 $(document).scrollTo(0,500);
6 }
7 });
预加载图片
这个图片预加载片段让你能够快速的预先载入图片,不需要等待。代码如下:
1 2 3 4 5 | jQuery.preloadImagesInWebPage = function () { for ( var ctr = 0; ctr<arguments.length; ctr++){ jQuery( "" ).attr( "src" , arguments[ctr]); } } |
调用方法:
1 | $.preloadImages( "image1.gif" , "image2.gif" , "image3.gif" ); |
判断图片是否已加载:
1 2 3 | $( '#imageObject' ).attr( 'src' , 'image1.gif' ).load( function () { alert( 'The image has been loaded…' ); }); |