可以直接拿来用的15个jQuery代码片段

jquery源代码文件库 jQuery 是一个 JavaScript 库。 jQuery 极大地简化了 JavaScript 编程。 jQuery 很容易学习。 在您开始学习 jQuery 之前,您应该对以下知识有基本的了解: HTML CSS JavaScript 立即下载

jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画、特效,还会提高网站的用户体验。

本文收集了15段非常实用的jQuery代码片段,你可以直接复制黏贴到代码里,但请开发者注意了,要理解代码再使用哦。下面就让我们一起来享受jQuery代码的魅力之处吧。

1.预加载图片

1
2
3
4
5
6
7
8
9
10
11
12
( function ($) {
   var cache = [];
   // Arguments are image paths relative to the current page.
   $.preLoadImages = function () {
     var args_len = arguments.length;
     for ( var i = args_len; i--;) {
       var cacheImage = document.createElement( 'img' );
       cacheImage.src = arguments[i];
       cache.push(cacheImage);
     }
   }
jQuery.preLoadImages( "image1.gif" , "/path/to/image2.png" );
源码

2. 让页面中的每个元素都适合在移动设备上展示

1
2
3
4
5
6
7
8
9
10
11
var scr = document.createElement( 'script' );
document.body.appendChild(scr);
scr.onload = function (){
     $( 'div' ).attr( 'class' , '' ).attr( 'id' , '' ).css({
         'margin' : 0,
         'padding' : 0,
         'width' : '100%' ,
         'clear' : 'both'
     });
};
源码

3.图像等比例缩放

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$(window).bind( "load" , function () {
     // IMAGE RESIZE
     $( '#product_cat_list img' ).each( function () {
         var maxWidth = 120;
         var maxHeight = 120;
         var ratio = 0;
         var width = $( this ).width();
         var height = $( this ).height();
         if (width > maxWidth){
             ratio = maxWidth / width;
             $( this ).css( "width" , maxWidth);
             $( this ).css( "height" , height * ratio);
             height = height * ratio;
         }
         var width = $( this ).width();
         var height = $( this ).height();
         if (height > maxHeight){
             ratio = maxHeight / height;
             $( this ).css( "height" , maxHeight);
             $( this ).css( "width" , width * ratio);
             width = width * ratio;
         }
     });
     //$("#contentpage img").show();
     // IMAGE RESIZE
});
源码

4.返回页面顶部

1
2
3
4
5
6
7
8
// Back To Top
$(document).ready( function (){
   $( '.top' ).click( function () { 
      $(document).scrollTo(0,500); 
   });
});
//Create a link defined with the class .top
<a href= "#" class= "top" >Back To Top</a>
源码

5.使用jQuery打造手风琴式的折叠效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var accordion = {
      init: function (){
            var $container = $( '#accordion' );
            $container.find( 'li:not(:first) .details' ).hide();
            $container.find( 'li:first' ).addClass( 'active' );
            $container.on( 'click' , 'li a' , function (e){
                   e.preventDefault();
                   var $ this = $( this ).parents( 'li' );
                   if ($ this .hasClass( 'active' )){
                          if ($( '.details' ).is( ':visible' )) {
                                 $ this .find( '.details' ).slideUp();
                          } else {
                                 $ this .find( '.details' ).slideDown();
                          }
                   } else {
                          $container.find( 'li.active .details' ).slideUp();
                          $container.find( 'li' ).removeClass( 'active' );
                          $ this .addClass( 'active' );
                          $ this .find( '.details' ).slideDown();
                   }
            });
      }
};

6.通过预加载图片廊中的上一幅下一幅图片来模仿Facebook的图片展示方式

1
2
3
4
5
6
7
8
var nextimage = "/images/some-image.jpg" ;
$(document).ready( function (){
window.setTimeout( function (){
var img = $( "" ).attr( "src" , nextimage).load( function (){
//all done
});
}, 100);
});

源码

7.使用jQuery和Ajax自动填充选择框
1
2
3
4
5
6
7
8
9
10
11
12
13
$( function (){
$( "select#ctlJob" ).change( function (){
$.getJSON( "/select.php" ,{id: $( this ).val(), ajax: 'true' }, function (j){
var options = '' ;
for ( var i = 0; i < j.length; i++) {
options += '
' + j[i].optionDisplay + '
' ;
}
$( "select#ctlPerson" ).html(options);
})
})
})
源码

8.自动替换丢失的图片

1
2
3
4
5
6
7
8
// Safe Snippet
$( "img" ).error( function () {
     $( this ).unbind( "error" ).attr( "src" , "missing_image.gif" );
});
// Persistent Snipper
$( "img" ).error( function () {
     $( this ).attr( "src" , "missing_image.gif" );
});

源码

9.在鼠标悬停时显示淡入/淡出特效

1
2
3
4
5
6
7
8
$(document).ready( function (){
     $( ".thumbs img" ).fadeTo( "slow" , 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads
     $( ".thumbs img" ).hover( function (){
         $( this ).fadeTo( "slow" , 1.0); // This should set the opacity to 100% on hover
     }, function (){
         $( this ).fadeTo( "slow" , 0.6); // This should set the opacity back to 60% on mouseout
     });
});
源码

10.清空表单数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function clearForm(form) {
   // iterate over all of the inputs for the form
   // element that was passed in
   $( ':input' , form).each( function () {
     var type = this .type;
     var tag = this .tagName.toLowerCase(); // normalize case
     // it's ok to reset the value attr of text inputs,
     // password inputs, and textareas
     if (type == 'text ' || type == ' password ' || tag == ' textarea ')
       this.value = "";
     // checkboxes and radios need to have their checked state cleared
     // but should *not* have their ' value ' changed
     else if (type == ' checkbox ' || type == ' radio ')
       this.checked = false;
     // select elements need to have their ' selectedIndex ' property set to -1
     // (this works for both single and multiple select elements)
     else if (tag == ' select')
       this .selectedIndex = -1;
   });
};
源码

11.预防对表单进行多次提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(document).ready( function () {
   $( 'form' ).submit( function () {
     if ( typeof jQuery.data( this , "disabledOnSubmit" ) == 'undefined' ) {
       jQuery.data( this , "disabledOnSubmit" , { submited: true });
       $( 'input[type=submit], input[type=button]' , this ).each( function () {
         $( this ).attr( "disabled" , "disabled" );
       });
       return true ;
     }
     else
     {
       return false ;
     }
   });
});
源码

12.动态添加表单元素

1
2
3
4
5
//change event on password1 field to prompt new input
$( '#password1' ).change( function () {
         //dynamically create new input and insert after password1
         $( "#password1" ).append( "" );
});
源码

13.让整个Div可点击

1
2
blah blah blah. link
The following lines of jQuery will make the entire div clickable: $( ".myBox" ).click( function (){ window.location=$( this ).find( "a" ).attr( "href" ); return false ; });
源码

14.平衡高度或Div元素

1
2
3
4
5
var maxHeight = 0;
$( "div" ).each( function (){
    if ($( this ).height() > maxHeight) { maxHeight = $( this ).height(); }
});
$( "div" ).height(maxHeight);
源码

15. 在窗口滚动时自动加载内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var loading = false ;
$(window).scroll( function (){
     if ((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){
         if (loading == false ){
             loading = true ;
             $( '#loadingbar' ).css( "display" , "block" );
             $.get( "load.php?start=" +$( '#loaded_max' ).val(), function (loaded){
                 $( 'body' ).append(loaded);
                 $( '#loaded_max' ).val(parseInt($( '#loaded_max' ).val())+50);
                 $( '#loadingbar' ).css( "display" , "none" );
                 loading = false ;
             });
         }
     }
});
$(document).ready( function () {
     $( '#loaded_max' ).val(50);
});
SVM分类源代码(可以直接使用,含详细注释) 这是基于蒙特卡洛发的SVM分类算法,里面包含了每一步的详细注释。适合初学者 立即下载

相关推荐

JQuery

JQuery

借用Snippet插件美化博客中的代码

书写博客,难免要贴出代码。然而直接贴出代码,则不美观。于是,应运而生出现了很多代码美化的插件。其中比较有名的是Syntax Highlighting插件。   笔者在网上翻阅的时候发现了Snippet插件,其支持众多的代码类的美化——例如:HTML、CSS、JS、C#等。并有39种不同的美化风格可供选择。 其官网地址:Snippet插件官网。在官网上,已经比较详细的介绍了插件的使用方法,这里...

weixin_34174322的博客 230

1173:阶乘和

1173:阶乘和 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 6010 通过数: 3049 【题目描述】 用高精度计算出S=1!+2!+3!+…+n!(n≤50),其中“!”表示阶乘,例如:5!=5×4×3×2×1。 输入正整数n,输出计算结果S。 【输入】 一个正整数n。 【输出】 计算结果S。 【输入样例】 5 【输出样例】 153 思路: ...

zqhf123的博客 1260

jquery.js和jquery.min.js的区别介绍

jquery.js和jquery.min.js的区别介绍 ballance1﹔0汷 | 浏览 16135 次  问题未开放回答 |举报 推荐于2017-10-10 11:44:10 最佳答案 1.区别:jquery官网提供2种jQuery的下载,一种是jquery.js另一种是jquery.min.js文件名不一定完全相同,但通常情况下:jquery

evilcry2013的博客 1万+

飞书日历同步Outlook翻车?我踩过的坑和最终用Google Calendar成功的完整配置记录

本文详细记录了从Outlook日历同步到飞书时遇到的常见问题及解决方案,特别是通过Google Calendar实现稳定同步的完整配置流程。文章揭示了Outlook同步方案易翻车的原因,并提供了Google Calendar的避坑指南、高级技巧和企业级部署建议,帮助用户高效实现日历同步。

weixin_33724046的博客 313

Python+OpenCV实现游戏自动刷怪脚本(附完整代码)

本文详细介绍了如何使用Python和OpenCV开发游戏自动刷怪脚本,涵盖环境搭建、图像识别、行为决策引擎及反检测优化等关键技术。通过完整的代码示例,帮助开发者实现从简单脚本到智能自动化系统的升级,提升游戏效率。

weixin_29032489的博客 518

jquery插件——jquery.slides.min.js

实现图片更换效果: 1.引入插件库:<script type="text/javascript" src="${base}/resources/shop/js/jquery.slides.min.js"></script>2.css定义样式:<style> #slides { display: none } #slides .slidesjs-navigation {

Tomorrow never comes 5456

张力控制速度闭环控制算法

这个算法的核心思想是通过比较实际速度和期望速度的差异来调整控制信号,以达到稳定的速度控制。比例项用于直接根据误差调整输出,积分项用于积累误差并进行补偿,微分项用于预测误差的变化趋势并进行调整。张力控制是在许多工程和工业应用中非常重要的一个过程,它涉及到通过调节力的大小来实现稳定的工作状态。通过合理设置比例、积分和微分系数,并结合实际系统的特性进行调试和优化,可以实现准确稳定的张力控制。速度闭环控制算法通过比较实际速度和期望速度之间的差异,并根据差异调整系统的输出,以保持速度稳定。

LiJavascript的博客 1301
上一篇: android中如何在代码中直接设置View的layout_weight属性
下一篇: Android滑动菜单特效实现,仿人人客户端侧滑效果,史上最简单的侧滑实现
爽玩一百天
博客等级 码龄14年 20粉丝 9原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值