開發中可能會用到的幾個 jQuery 小提示和技巧


  今天,我們將分享一些很有用的技巧和竅門給 jQuery 開發人員。jQuery 是最好的 JavaScript 庫之一,用於簡化動畫,事件處理,支持 Ajax 和 HTML 的客戶端腳本。網絡中有大量的 jQuery 插件,有助於在短時間內通過簡單容易的方法創建網站。

  今天我們選取了幾個隊 jQuery 開發人員非常有用的代碼片段。希望你的下一個項目中能用得上這些代碼。

您可能感興趣的相關文章

  

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…');
});

  

您可能感興趣的相關文章

  

本文鏈接:Web 開發中實用的幾個 jQuery 提示和技巧

編譯來源:夢想天空 ◆ 關注前端開發技術 ◆ 分享網頁設計資源

本文來自【夢想天空(http://www.cnblogs.com/lhb25/)】


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM