開發中可能會用到的jQuery小技巧

來源:互聯網
上載者:User

1) 禁止右鍵
在開發 Web 應用程式的時候,有些情況需要禁用按右鍵功能。使用此代碼,jQuery 開發人員可以在網頁上禁用滑鼠右鍵點擊。代碼如下:
複製代碼 代碼如下:
$(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) 文本縮放
使用下面的代碼,使用者可以更具需要增大或者縮放網頁中的字型大小,代碼如下:
複製代碼 代碼如下:
$(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) 在新視窗開啟連結
使用這個 jQuery 代碼,使用者會點擊你的網站的任何連結都會在新的視窗中開啟。如下:
複製代碼 代碼如下:
$(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) 樣式表切換
你知道網站換膚是怎麼做的嗎?下面的代碼可以協助你實現樣式表切換功能,如下:
複製代碼 代碼如下:
$(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) 回到頂部
這是現在網站中很常用的回到頂部功能,特別適合頁面很長的情況。代碼很簡單,如下:
複製代碼 代碼如下:
$(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) 擷取滑鼠的 X、Y 座標
下面的代碼可以擷取滑鼠的 X,Y 座標,代碼如下:
複製代碼 代碼如下:
$().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) 檢測當前滑鼠的座標
使用下面的代碼,能夠在任何支援 jQuery 的地方擷取當前滑鼠的座標,如下:
複製代碼 代碼如下:
$(document).ready(function() {
$().mousemove(function(e){
$('# MouseCoordinates ').html("X Axis Position = " + e.pageX + " and Y Axis Position = " + e.pageY);
});

8) 預先載入圖片
這個圖片預先載入片段讓你能夠快速的預先載入圖片,不需要等待。代碼如下:
複製代碼 代碼如下:
jQuery.preloadImagesInWebPage = function() {
for(var ctr = 0; ctr<arguments.length; ctr++){
jQuery("").attr("src", arguments[ctr]);
}
}

調用方法:
複製代碼 代碼如下:
$.preloadImages("image1.gif", "image2.gif", "image3.gif");
判斷圖片是否已載入:
$('#imageObject').attr('src', 'image1.gif').load(function() {
alert('The image has been loaded…');
});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.