開發(fā)中可能會(huì)用到的jQuery小技巧
更新時(shí)間:2014年03月07日 15:33:08 作者:
這篇文章主要介紹了開發(fā)中可能會(huì)用到的幾個(gè)jQuery小提示和技巧,需要的朋友可以參考下
1) 禁止右鍵
在開發(fā) Web 應(yīng)用的時(shí)候,有些情況需要禁用右鍵單擊功能。使用此代碼,jQuery 開發(fā)人員可以在網(wǎng)頁上禁用鼠標(biāo)右鍵點(diǎn)擊。代碼如下:
復(fù)制代碼 代碼如下:
$(document).ready(function() {
//catch the right-click context menu
$(document).bind("contextmenu",function(e) {
//warning prompt - optional
alert("No right-clicking!");
//delete the default context menu
return false;
});
});
2) 文本縮放
使用下面的代碼,用戶可以更具需要增大或者縮放網(wǎng)頁中的字體大小,代碼如下:
復(fù)制代碼 代碼如下:
$(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);
});
});
3) 在新窗口打開鏈接
使用這個(gè) jQuery 代碼,用戶會(huì)點(diǎn)擊你的網(wǎng)站的任何鏈接都會(huì)在新的窗口中打開。如下:
復(fù)制代碼 代碼如下:
$(document).ready(function() {
//select all anchor tags that have http in the href
//and apply the target=_blank
$("a[href^='http']").attr('target','_blank');
});
4) 樣式表切換
你知道網(wǎng)站換膚是怎么做的嗎?下面的代碼可以幫助你實(shí)現(xiàn)樣式表切換功能,如下:
復(fù)制代碼 代碼如下:
$(document).ready(function() {
$("a.cssSwap").click(function() {
//swap the link rel attribute with the value in the rel
$('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
});
});
5) 回到頂部
這是現(xiàn)在網(wǎng)站中很常用的回到頂部功能,特別適合頁面很長(zhǎng)的情況。代碼很簡(jiǎn)單,如下:
復(fù)制代碼 代碼如下:
$(document).ready(function() {
//when the id="top" link is clicked
$('#top').click(function() {
//scoll the page back to the top
$(document).scrollTo(0,500);
}
});
6) 獲取鼠標(biāo)的 X、Y 坐標(biāo)
下面的代碼可以獲取鼠標(biāo)的 X,Y 坐標(biāo),代碼如下:
復(fù)制代碼 代碼如下:
$().mousemove(function(e){
//display the x and y axis values inside the P element
$('p').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
});
7) 檢測(cè)當(dāng)前鼠標(biāo)的坐標(biāo)
使用下面的代碼,能夠在任何支持 jQuery 的地方獲取當(dāng)前鼠標(biāo)的坐標(biāo),如下:
復(fù)制代碼 代碼如下:
$(document).ready(function() {
$().mousemove(function(e){
$('# MouseCoordinates ').html("X Axis Position = " + e.pageX + " and Y Axis Position = " + e.pageY);
});
8) 預(yù)加載圖片
這個(gè)圖片預(yù)加載片段讓你能夠快速的預(yù)先載入圖片,不需要等待。代碼如下:
復(fù)制代碼 代碼如下:
jQuery.preloadImagesInWebPage = function() {
for(var ctr = 0; ctr<arguments.length; ctr++){
jQuery("").attr("src", arguments[ctr]);
}
}
調(diào)用方法:
復(fù)制代碼 代碼如下:
$.preloadImages("image1.gif", "image2.gif", "image3.gif");
判斷圖片是否已加載:
$('#imageObject').attr('src', 'image1.gif').load(function() {
alert('The image has been loaded…');
});
相關(guān)文章
Jquery 獲取表單text,areatext,radio,checkbox,select值的代碼
Jquery 獲取表單text,areatext,radio,checkbox,select值的實(shí)現(xiàn)代碼,學(xué)習(xí)jquery的朋友可以參考下。都是必須要了解的。2009-11-11
jQuery實(shí)現(xiàn)tab標(biāo)簽自動(dòng)切換的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)tab標(biāo)簽自動(dòng)切換的方法,實(shí)例分析了jQuery操作tab標(biāo)簽的技巧,涉及addClass、siblings及removeClass鏈?zhǔn)讲僮鞯募记?需要的朋友可以參考下2015-02-02
從零開始學(xué)習(xí)jQuery (八) 插播:jQuery實(shí)施方案
本篇文章屬于臨時(shí)插播, 用于介紹我在本公司的jQuery實(shí)施方案.2011-02-02
jQuery使用eraser.js插件實(shí)現(xiàn)擦除、刮刮卡效果的方法【附eraser.js下載】
這篇文章主要介紹了jQuery使用eraser.js插件實(shí)現(xiàn)擦除、刮刮卡效果的方法,結(jié)合實(shí)例形式分析了jQuery.eraser.js插件的功能、使用方法與相關(guān)注意事項(xiàng),并附帶eraser.js插件源碼供讀者下載使用,需要的朋友可以參考下2017-04-04

