ITeye博客

9 个超实用的 jQuery 代码片段

wangxiaoxu · 2013-07-15 14:49 · js · Web前端 · Markdown

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

1.  jQuery平滑回到顶端效果 

Javascript代码 

  1. $(document).ready(function() {
  2. $("a.topLink").click(function() {
  3. $("html, body").animate({
  4. scrollTop: $($(this).attr("href")).offset().top + "px"
  5. }, {
  6. duration: 500,
  7. easing: "swing"
  8. });
  9. return false;
  10. });
  11. });

在线调试 

2.  jQuery处理图片尺寸 

Javascript代码 

  1. $(window).bind("load", function() {
  2. // 图片修改大小
  3. $('#imglist img').each(function() {
  4. var maxWidth = 120;
  5. var maxHeight = 120;
  6. var ratio = 0;
  7. var width = $(this).width();
  8. var height = $(this).height();
  9. if(width > maxWidth){
  10. ratio = maxWidth / width;
  11. $(this).css("width", maxWidth);
  12. $(this).css("height", height * ratio);
  13. height = height * ratio;
  14. }
  15. if(height > maxHeight){
  16. ratio = maxHeight / height;
  17. $(this).css("height", maxHeight);
  18. $(this).css("width", width * ratio);
  19. width = width * ratio;
  20. }
  21. });
  22. });

在线调试 

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

Javascript代码 

  1. var loading = false;
  2. $(window).scroll(function(){
  3. if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){
  4. if(loading == false){
  5. loading = true;
  6. $('#loadingbar').css("display","block");
  7. $.get("load.php?start="+$('#loaded_max').val(), function(loaded){
  8. $('body').append(loaded);
  9. $('#loaded_max').val(parseInt($('#loaded_max').val())+50);
  10. $('#loadingbar').css("display","none");
  11. loading = false;
  12. });
  13. }
  14. }
  15. });
  16. $(document).ready(function() {
  17. $('#loaded_max').val(50);
  18. });

4.  jQuery测试密码强度 

Javascript代码 

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

在线调试 

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

Javascript代码 

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

在线调试 

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

Javascript代码 

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

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

7.  jQuery处理JSON 

Javascript代码 

  1. function parseJson(){
  2. //Start by calling the json object, I will be using a
  3. //file from my own site for the tutorial
  4. //Then we declare a callback function to process the data
  5. $.getJSON('hcj.json',getPosts);
  6. //The process function, I am going to get the title,
  7. //url and excerpt for 5 latest posts
  8. function getPosts(data){
  9. //Start a for loop with a limit of 5
  10. for(var i = 0; i < 5; i++){
  11. //Build a template string of
  12. //the post title, url and excerpt
  13. var strPost = '<h2>'
  14. + data.posts[i].title
  15. + '</h2>'
  16. + '<p>'
  17. + data.posts[i].excerpt
  18. + '</p>'
  19. + '<a href="'
  20. + data.posts[i].url
  21. + '" title="Read '
  22. + data.posts[i].title
  23. + '">Read ></a>';
  24. //Append the body with the string
  25. $('body').append(strPost);
  26. }
  27. }
  28. }
  29. //Fire off the function in your document ready
  30. $(document).ready(function(){
  31. parseJson();
  32. });

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

Javascript代码 

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

在线调试 

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

Javascript代码 

  1. var nextimage = "http://www.gbtags.com/gb/networks/uploadimgthumb/55d67b14-fb25-45e7-acc8-211a41047ef0.jpg";
  2. $(document).ready(function(){
  3. window.setTimeout(function(){
  4. var img = $("<img>").attr("src", nextimage).load(function(){
  5. $('div').append(img);
  6. });
  7. }, 100);
  8. });
  9. [url=http://www.gbin1.com/gb/debug/b1a87e30-e33f-4369-92fc-55e8fd628816.htm]在线调试[/url]

希望大家喜欢这些实用的jQuery代码,如果你也有更多的jQuery代码片段,请与我们分享,谢谢!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。