麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

20+個(gè)可重復(fù)使用的jQuery代碼片段

2019-11-14 16:43:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

jQuery已經(jīng)成為任何web項(xiàng)目的重要組成部分。它為網(wǎng)站提供了交互性的通過(guò)移動(dòng)HTML元素,創(chuàng)建自定義動(dòng)畫(huà),處理事件,選擇DOM元素,檢索整個(gè)document ,讓最終用戶有一個(gè)更好的體驗(yàn)。

在這篇文章中我已經(jīng)收集了20 +個(gè)可重復(fù)使用的jQuery代碼片段,你可以很容易地復(fù)制并直接粘貼到你的項(xiàng)目中。

Reusable jQuery Code Snippets for Developers

圖片的延遲加載

1jQuery(document).ready(function() {
2    jQuery("img.lazy").lazy({
3        delay: 2000
4    });
5});

Source

預(yù)先載入圖像

01(function($) {
02  var cache = [];
03  // Arguments are image paths relative to the current page.
04  $.PReLoadImages = function() {
05    var args_len = arguments.length;
06    for (var i = args_len; i--;) {
07      var cacheImage = document.createElement('img');
08      cacheImage.src = arguments[i];
09      cache.push(cacheImage);
10    }
11  }
12})(jQuery)

Source

分頁(yè)面刷新

1setInterval(function() {
2    $("#refresh").load(location.href+" #refresh>*","");
3}, 10000);

Source

延遲動(dòng)畫(huà)/效果

1$(".alert").delay(2000).fadeOut();

Source

Open external link in New Window

01$('a').each(function() {
02   var a = new RegExp('/' + window.location.host + '/');
03   if(!a.test(this.href)) {
04       $(this).click(function(event) {
05           event.preventDefault();
06           event.stopPropagation();
07           window.open(this.href, '_blank');
08       });
09   }
10});

Source

Make Everything Mobile Friendly

01var scr = document.createElement('scr);
02scr.setAttribute('src', 'https://Ajax.googleapis.com/ajax/libs/jquery/1.5.2/
03jquery.min.js');
04document.body.appendChild(scr);
05 
06scr.onload = function(){
07 
08    $('div').attr('class''').attr('id''').CSS({
09        'margin' : 0,
10        'padding' : 0,
11        'width''100%',
12        'clear':'both'
13    });
14};

Source

Image Resize Using jQuery

01$(window).bind("load"function() {
02    // IMAGE RESIZE
03    $('#product_cat_list img').each(function() {
04        var maxWidth = 120;
05        var maxHeight = 120;
06        var ratio = 0;
07        var width = $(this).width();
08        var height = $(this).height();
09 
10        if(width > maxWidth){
11            ratio = maxWidth / width;
12            $(this).css("width", maxWidth);
13            $(this).css("height", height * ratio);
14            height = height * ratio;
15        }
16        var width = $(this).width();
17        var height = $(this).height();
18        if(height > maxHeight){
19            ratio = maxHeight / height;
20            $(this).css("height", maxHeight);
21            $(this).css("width", width * ratio);
22            width = width * ratio;
23        }
24    });
25    //$("#contentpage img").show();
26    // IMAGE RESIZE
27});

Source

Smooth Scrolling

01$(function() {
02  $('a[href*=#]:not([href=#])').click(function() {
03    if (location.pathname.replace(/^///,'') == this.pathname.replace(/^///,'')
04 && location.hostname == this.hostname) {
05      var target = $(this.hash);
06      target = target.length ? target : $('[name=' this.hash.slice(1) +']');
07      if (target.length) {
08        $('html,body').animate({
09          scrollTop: target.offset().top
10        }, 1000);
11        return false;
12      }
13    }
14  });
15});

Source

Window load event with minimum delay

01(function fn() {
02 
03  fn.now = +new Date;
04 
05  $(window).load(function() {
06 
07     if (+new Date - fn.now < 500) setTimeout(fn, 500);
08 
09         // Do something
10 
11  });
12 
13})();

Source

jQuery Accordion

01(function($) {
02 
03  var allPanels = $('.accordion > dd').hide();
04 
05  $('.accordion > dt > a').click(function() {
06    allPanels.slideUp();
07    $(this).parent().next().slideDown();
08    return false;
09  });
10 
11})(jQuery);

Source

Simple Auto-Playing Slideshow

01$("#slideshow > div:gt(0)").hide();
02 
03setInterval(function() {
04  $('#slideshow > div:first')
05    .fadeOut(1000)
06    .next()
07    .fadeIn(1000)
08    .end()
09    .appendTo('#slideshow');
10},  3000);

Source

Shuffle DOM Elements

01(function($){
02 
03    $.fn.shuffle = function() {
04 
05        var allElems = this.get(),
06            getRandom = function(max) {
07                return Math.floor(Math.random() * max);
08            },
09            shuffled = $.map(allElems, function(){
10                var random = getRandom(allElems.length),
11                    randEl = $(allElems[random]).clone(true)[0];
12                allElems.splice(random, 1);
13                return randEl;
14           });
15 
16        this.each(function(i){
17            $(this).replaceWith($(shuffled[i]));
18        });
19 
20        return $(shuffled);
21 
22    };
23 
24})(jQuery);

Source

Scroll Page Horizontally With Mouse Wheel

01$(function() {
02 
03   $("body").mousewheel(function(event, delta) {
04 
05      this.scrollLeft -= (delta * 30);
06 
07      event.preventDefault();
08 
09   });
10 
11});

Source

Load Only a Section of a Page

1$("#mainNav").load("/store #mainNav")

Source

Highlight Related Label when Input in Focus

1$("form :input").focus(function() {
2  $("label[for='" this.id + "']").addClass("labelfocus");
3}).blur(function() {
4  $("label").removeClass("labelfocus");
5});

Source

Highlight All Links To Current Page

1$(function(){
2    $("a").each(function(){
3       if ($(this).attr("href") == window.location.pathname){
4            $(this).addClass("selected");
5       }
6    });
7});

Source

Better Broken Image Handling

1// Replace source
2$('img').error(function(){
3        $(this).attr('src''missing.png');
4});
5 
6// Or, hide them
7$("img").error(function(){
8        $(this).hide();
9});

Source

Load Content on Scroll Automatically

01var loading = false;
02$(window).scroll(function(){
03    if((($(window).scrollTop()+$(window).height())+250)>=$(document).
04height()){
05        if(loading == false){
06            loading = true;
07            $('#loadingbar').css("display","block");
08            $.get("load.php?start="+$('#loaded_max').val(),
09function(loaded){
10                $('body').append(loaded);
11                $('#loaded_max').val(parseInt($('#loaded_max')
12.val())+50);
13                $('#loadingbar').css("display","none");
14                loading = false;
15            });
16        }
17    }
18});
19 
20$(document).ready(function() {
21    $('#loaded_max').val(50);
22});

Source

Prevent Multiple Submit of Your Form

01$(document).ready(function() {
02  $('form').submit(function() {
03    if(typeof jQuery.data(this"disabledOnSubmit") == 'undefined') {
04      jQuery.data(this"disabledOnSubmit", { submited: true });
05      $('input[type=submit], input[type=button]'this).each(function() {
06        $(this).attr("disabled""disabled");
07      });
08      return true;
09    }
10    else
11    {
12      return false;
13    }
14  });
15});

Source

Make Entire Div Clickable

1$(".myBox").click(function(){
2     window.location=$(this).find("a").attr("href");
3     return false;
4});

Source

Toggle Text

1$("#more-less-options-button").click(function() {
2     var txt = $("#extra-options").is(':visible') ? 'more options': 'less
3options';
4     $("#more-less-options-button").text(txt);
5     $("#extra-options").slideToggle();
6});

Source


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 一区二区三区精品国产 | 作爱在线观看 | 一区二区三区国产在线 | 一级尻逼视频 | 日韩毛片一区二区三区 | 中文字幕在线观看91 | 老女人碰碰在线碰碰视频 | 中文字幕在线观看免费视频 | 欧美精品久久久久久久久久 | 啪啪毛片 | 一级黄色毛片子 | 一级α片免费看刺激高潮视频 | 色蜜桃av | 一区二区三区在线视频观看58 | 午夜视频中文字幕 | 久久久视频免费观看 | 天天草天天干天天射 | 国内精品视频饥渴少妇在线播放 | 国产精品999在线观看 | 牛牛碰在线视频 | 一级看片免费视频 | 视频一区 中文字幕 | av黄色在线免费观看 | a免费看| a免费看 | 日本黄色不卡视频 | 国产午夜精品一区二区三区四区 | 国产午夜精品久久久久久免费视 | 久久久精品视频网站 | 黄色网战入口 | 日本在线播放一区二区三区 | 久久久久久中文字幕 | 一级在线观看视频 | 91精品国产乱码久久久久久久久 | 一区二区三区在线视频观看58 | 粉嫩av一区二区三区四区在线观看 | 久久久久免费精品 | 99在线在线视频免费视频观看 | 成人在线观看一区 | 调教小男生抽打尿孔嗯啊视频 | 亚州精品国产 |