jQuery从入门到精通  9个超实用的jQuery代码片段解析
沉沙 2018-06-22 来源 : 阅读 1053 评论 0

摘要:jQuery以其强大的功能和简单的使用成为了前端开发者最喜欢的JS类库,在这里我们分享一组实用的jQuery代码片段,希望阅读本篇文章以后大家有所收获,帮助大家对jQuery的理解更加深入。

jQuery以其强大的功能和简单的使用成为了前端开发者最喜欢的JS类库,在这里我们分享一组实用的jQuery代码片段,希望大家喜欢! 

1.  jQuery平滑回到顶端效果 

 $(document).ready(function() {  
   
     $("a.topLink").click(function() {  
         $("html, body").animate({  
             scrollTop: $($(this).attr("href")).offset().top + "px"  
         }, {  
             duration: 500,  
             easing: "swing"  
         });  
         return false;  
     });  
   
 });


2.  jQuery处理图片尺寸 

 $(window).bind("load", function() {  
     // 图片修改大小  
     $('#imglist 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;  
         }  
         
         if(height > maxHeight){  
             ratio = maxHeight / height;  
            $(this).css("height", maxHeight);  
             $(this).css("width", width * ratio);  
             width = width * ratio;  
         }  
     });  
   
 });


3.  jQuery实现的滚动自动加载代码 

 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);  
 });



4.  jQuery测试密码强度 

 $('#pass').keyup(function(e) {  
      var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");  
      var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");  
      var enoughRegex = new RegExp("(?=.{6,}).*", "g");  
      if (false == enoughRegex.test($(this).val())) {  
              $('#passstrength').html('More Characters');  
      } else if (strongRegex.test($(this).val())) {  
              $('#passstrength').className = 'ok';  
              $('#passstrength').html('Strong!');  
      } else if (mediumRegex.test($(this).val())) {  
              $('#passstrength').className = 'alert';  
              $('#passstrength').html('Medium!');  
      } else {  
              $('#passstrength').className = 'error';  
              $('#passstrength').html('Weak!');  
      }  
      return true;  
 });


5.  jQuery实现的DIv高度保持一致 

 var maxHeight = 0;  
   
 $("div").each(function(){  
    if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }  
 });  
   
 $("div").height(maxHeight);


6.  简单处理IE6上PNG格式文件 

 $('.pngfix').each( function() {  
    $(this).attr('writing-mode', 'tb-rl');  
    $(this).css('background-image', 'none');  
    $(this).css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="path/to/image.png",sizingMethod="scale")');  
 });

将class=pngfix添加到需要处理的对象即可。 

7.  jQuery处理JSON 

 function parseJson(){  
     //Start by calling the json object, I will be using a   
         //file from my own site for the tutorial   
     //Then we declare a callback function to process the data  
     $.getJSON('hcj.json',getPosts);  
    
     //The process function, I am going to get the title,   
         //url and excerpt for 5 latest posts  
     function getPosts(data){  
    
         //Start a for loop with a limit of 5   
         for(var i = 0; i < 5; i++){  
             //Build a template string of   
                         //the post title, url and excerpt  
             var strPost = '<h2>'   
                       + data.posts[i].title  
                       + '</h2>'  
                       + '<p>'  
                       + data.posts[i].excerpt  
                       + '</p>'  
                       + '<a href="'  
                       + data.posts[i].url  
                       + '" title="Read '  
                       + data.posts[i].title  
                       + '">Read ></a>';  
    
             //Append the body with the string  
             $('body').append(strPost);  
    
         }  
     }  
    
 }  
    
 //Fire off the function in your document ready  
 $(document).ready(function(){  
     parseJson();                     
 });


8.  jQuery实现让整个div可以被点击 

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


9.  jQuery实现的Facebook风格的图片预加载效果 

 var nextimage = "//www.gbtags.com/gb/networks/uploadimgthumb/55d67b14-fb25-45e7-acc8-211a41047ef0.jpg";  
 $(document).ready(function(){  
   window.setTimeout(function(){  
     var img = $("<img>").attr("src", nextimage).load(function(){  
     $('div').append(img);  
     });  
   }, 100);  
 });

  

本文由职坐标整理发布,学习更多的jQuery相关知识,请关注职坐标WEB前端jQuery频道!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程