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

來源:互聯網
上載者:User

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

本文我們將為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<arguments.length; 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…');
});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.