首頁  >  文章  >  web前端  >  8個超實用的jQuery功能代碼分享_jquery

8個超實用的jQuery功能代碼分享_jquery

WBOY
WBOY原創
2016-05-16 16:21:38975瀏覽

本文我們將為jQuery使用者分享8個超實用的技巧攻略。 jQuery是JavaScript最好的函式庫之一,主要用於製作動畫、事件處理,支援Ajax及HTML 腳本用戶端。此外,jQuery還擁有各種插件,以幫助開發者在最短時間內快速建立網站/網頁。

文中分享的這些實用程式碼,希望你會喜歡。

1)停用右鍵點選功能
如果你想為用戶節省網站信息,那麼開發者可以使用這段程式碼——停用右鍵點擊功能。

複製程式碼 程式碼如下:

$(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)使用jQuery設定文字大小

使用這段程式碼,使用者可依需求重新設定文字尺寸(增加或減少)。

複製程式碼 程式碼如下:

$(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)在新視窗開啟連結

使用這段程式碼會幫助用戶在新視窗中打開鏈接,為用戶帶來更好的用戶體驗。

複製程式碼 程式碼如下:

$(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軸

複製程式碼 程式碼如下:

$().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)偵測目前滑鼠座標

使用這個腳本,你可以在任何網頁瀏覽器取得滑鼠座標。

複製程式碼 程式碼如下:

$(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 {
jQuery("").attr("src", arguments[ctr]);
}
}
To use the above method, you can use the following piece of code:
$.preloadImages("image1.gif", "image2.gif", "image3.gif");
To check whether an image has been loaded, you can use the following piece of code:
$('#imageObject').attr('src', 'image1.gif').load(function() {
alert('The image has been loaded…');
});
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn