首頁 web前端 js教程 開發中可能會用到的jQuery小技巧_jquery

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

May 16, 2016 pm 04:56 PM

1) 禁止右鍵
在開發 Web 應用的時候,有些情況需要停用右鍵功能。使用此程式碼,jQuery 開發人員可以在網頁上停用滑鼠右鍵。程式碼如下:

複製程式碼 程式碼如下:

$(document).read ) {
//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('html').css('html').css('html').css('html').css('html').css('html').css('html'). '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').$('html").s('html" ', newFontSize);
return false;
});

//Decrease the Text Size
$(".decreaseFont").click(function() {
var currentFontSize = $('html').css('font-size');
var currentFontSizeNum = parseFloat(currentFontSize, 10);

var newFontSize = currentFontFontizeNum*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) 回到頂部
這是現在網站中很常用的回到頂部功能,特別適合頁面很長的情況。程式碼很簡單,如下:複製程式碼
程式碼如下:


$read(document). (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() {

$(document).ready(function() {
$().mousemove(function(e){ $('# MouseCoordinates ').html("X Axis Position = " e.pageX " and Y Axis Position = " e.pageY); }) ;
8) 預先載入圖片
這個圖片預先載入片段讓你能夠快速的預先載入圖片,不需要等待。程式碼如下:
複製程式碼 程式碼如下:

jQuery.preloadI. 🎜>for(var ctr = 0; ctrjQuery("").attr("src", arguments[ctr]);
}
}

呼叫方法:

複製程式碼 程式碼如下:

程式碼如下:


("image1.gif", "image2.gif", "image3.gif");
判斷圖片是否已載入:
$('#imageObject').attr('src', 'image1.gif ').load(function() {
alert('The image has been loaded…'); });
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在JavaScript中替換字符串字符 在JavaScript中替換字符串字符 Mar 11, 2025 am 12:07 AM

在JavaScript中替換字符串字符

自定義Google搜索API設置教程 自定義Google搜索API設置教程 Mar 04, 2025 am 01:06 AM

自定義Google搜索API設置教程

示例顏色json文件 示例顏色json文件 Mar 03, 2025 am 12:35 AM

示例顏色json文件

8令人驚嘆的jQuery頁面佈局插件 8令人驚嘆的jQuery頁面佈局插件 Mar 06, 2025 am 12:48 AM

8令人驚嘆的jQuery頁面佈局插件

構建您自己的Ajax Web應用程序 構建您自己的Ajax Web應用程序 Mar 09, 2025 am 12:11 AM

構建您自己的Ajax Web應用程序

什麼是這個'在JavaScript? 什麼是這個'在JavaScript? Mar 04, 2025 am 01:15 AM

什麼是這個'在JavaScript?

通過來源查看器提高您的jQuery知識 通過來源查看器提高您的jQuery知識 Mar 05, 2025 am 12:54 AM

通過來源查看器提高您的jQuery知識

10張移動秘籍用於移動開發 10張移動秘籍用於移動開發 Mar 05, 2025 am 12:43 AM

10張移動秘籍用於移動開發

See all articles