天天看點

高效web開發的10個jQuery代碼片段

1.檢測Internet Explorer版本

當涉及到CSS設計時,對開發者和設計師而言Internet Explorer一直是個問題。盡管IE6的黑暗時代已經過去,IE也越來越不流行,它始終是一個能夠容易檢測的好東西。當然了,下面的代碼也能用于檢測别的浏覽器。

$(document).ready(function(){
    if(navigator.userAgent.match(/msie/i)){
        alert('I am an old fashioned Internet Explorer');
    }
});
           

2.平穩滑動到頁面頂部

這是一個廣泛使用的jQuery效果:對一個連結點選下會平穩地将頁面移動到頂部。這是沒什麼新的内容,但是每個開發者必須要會偶爾編寫一下類似函數

$("a[href='#top']").click(function(){
    $("html, body").animate({scrollTop: }, "slow");
        return false;
});
           

3.固定在頂部

非常有用的代碼片段,它允許一個元素固定在頂部。對導航按鈕、工具欄或重要資訊框是超級有用的。

$(function(){
    var $win = $(window);
    var $nav = $(".mytoolbar");
    var navTop = $(".mytoolbar").length && $(".mytoolbar").offset().top;
    var isFixed = ;

    processScroll();
    $win.on("scroll", processScroll);

    function processScroll(){
        var i, scrollTop = $win.scrollTop();

        if(scrollTop >= navTop && !isFixed){
            isFixed = ;
            $nav.addClass("subnav-fixed");
        }else if (scrollTop <= navTop && !isFixed){
            isFixed = ;
            $nav.removeClass("subnav-fixed");
        }
    }
});
           

4.用其他内容取代html标志

jQuery使得用另外一個東西取代html标志很簡單。可以利用的餘地無窮無盡。

$("li").replaceWith(function(){
    return $("<div />").append($(this).contents());
});
           

5.檢測視窗寬度

現在移動裝置比過時的電腦更普遍,能夠友善去檢測一個更小的視窗寬度會很有幫助。幸運的是,用jQuery來做超級簡單。

var responsive_viewport = $(window).width();

/* if is below 481px */
if (responsive_viewport < ) {
    alert('Viewport is smaller than 481px.');
} /* end smallest screen */
           

6.自動定位并修複損壞圖檔

如果你的站點比較大而且已經線上運作了好多年,你或多或少會遇到界面上某個地方有損壞的圖檔。這個有用的函數能夠幫助檢測損壞圖檔并用你中意的圖檔替換它,并會将此問題通知給訪客。

$('img').error(function(){
    $(this).attr('src', 'img/broken.png');
});
           

7.檢測複制、粘貼和剪切的操作

使用jQuery可以很容易去根據你的要求去檢測複制、粘貼和剪切的操作。

$("#textA").bind('copy', function() {
    $('span').text('copy behaviour detected!')
}); 
$("#textA").bind('paste', function() {
    $('span').text('paste behaviour detected!')
}); 
$("#textA").bind('cut', function() {
    $('span').text('cut behaviour detected!')
});
           

8.遇到外部連結自動添加 target=”blank”的屬性

當連結到外部站點時,你可能使用target=”blank”的屬性去在新界面中打開站點。問題在于target=”blank”屬性并不是W3C有效的屬性。讓我們用jQuery來補救:下面這段代碼将會檢測是否連結是外鍊,如果是,會自動添加一個target=”blank”屬性。

var root = location.protocol + '//' + location.host;
$('a').not(':contains(root)').click(function(){
    this.target = "_blank";
});
           

9.在圖檔上停留時逐漸增強或減弱的透明效果

另一個“經典的”代碼,它要放到你的工具箱裡,因為你會不時地要實作它。

$(document).ready(function(){
    $(".thumbs img").fadeTo("slow", ); // This sets the opacity of the thumbs to fade down to 60% when the page loads

    $(".thumbs img").hover(function(){
        $(this).fadeTo("slow", ); // This should set the opacity to 100% on hover
    },function(){
        $(this).fadeTo("slow", ); // This should set the opacity back to 60% on mouseout
    });
});
           

10.在文本或密碼輸入時禁止空格鍵

在很多表格領域都不需要空格鍵,例如,電子郵件,使用者名,密碼等等等。這裡是一個簡單的技巧可以用于在標明輸入中禁止空格鍵。

$('input.nospace').keydown(function(e) {
    if (e.keyCode == ) {
        return false;
    }
});
           

文章摘自碼農網

繼續閱讀