0 Comments

jQuery 1.4实用技巧大放送(2)

发布于:2013-06-04  |   作者:广州网站建设  |   已聚集:人围观

列高度相同

如果使用了两个CSS列,使用此种方式可以是两列的高度相同。


  1. $(document).ready(function() {     
  2.   function equalHeight(group) {     
  3.   tallest = 0;     
  4.   group.each(function() {     
  5.  
  6.   thisHeight = $(this).height();     
  7.  
  8.   if(thisHeight > tallest) {     
  9.  
  10.      tallest = thisHeight;     
  11.  
  12.   }     
  13.  
  14. });     
  15.  
  16. group.height(tallest);     
  17. }     
  18.  // how to use     
  19.  $(document).ready(function() {     
  20.  
  21.    equalHeight($(".left"));     
  22.  
  23.    equalHeight($(".right"));     
  24.  
  25.   });     
  26.  
  27. });   

动态控制页面字体大小

广州网站建设,网站建设,广州网页设计,广州网站设计
  1. $(document).ready(function() {     
  2.  
  3.   // Reset the font size(back to default)     
  4.   var originalFontSize = $('html').css('font-size');     
  5.  
  6.   $(".resetFont").click(function(){     
  7.   $('html').css('font-size', originalFontSize);     
  8.  
  9. });     
  10.  
  11.   // Increase the font size(bigger font0     
  12.   $(".increaseFont").click(function(){     
  13.  
  14. var currentFontSize = $('html').css('font-size');     
  15. var currentFontSizeNum = parseFloat(currentFontSize, 10);     
  16. var newFontSize = currentFontSizeNum*1.2;     
  17.  
  18. $('html').css('font-size', newFontSize);     
  19.  
  20.    return false;     
  21.  
  22. });     
  23.  
  24. // Decrease the font size(smaller font)     
  25. $(".decreaseFont").click(function(){     
  26.  
  27. var currentFontSize = $('html').css('font-size');     
  28. var currentFontSizeNum = parseFloat(currentFontSize, 10);     
  29. var newFontSize = currentFontSizeNum*0.8;     
  30.  
  31. $('html').css('font-size', newFontSize);     
  32.   return false;     
  33.   });     
  34. });   

返回页面顶部功能

广州网站建设,网站建设,广州网页设计,广州网站设计
  1. $(document).ready(function() {     
  2. $('a[href*=#]').click(function() {     
  3.    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')     
  4.    && location.hostname == this.hostname) {     
  5.    var $target = $(this.hash);     
  6. $target = $target.length && $target     
  7.   || $('[name=' + this.hash.slice(1) +']');     
  8.   if ($target.length) {     
  9.  
  10. var targetOffset = $target.offset().top;     
  11.  
  12. $('html,body')     
  13.   .animate({scrollTop: targetOffset}, 900);     
  14.   return false;     
  15. }     
  16.  
  17. }     
  18. });     
  19.  
  20. // how to use     
  21. // place this where you want to scroll to     
  22.  
  23. <A name=top></A>   
  24.     
  25. // the link     
  26. <A href="#top">go to top</A>     
  27. });   

获得鼠标指针XY值


  1.    $(document).ready(function() {     
  2.    $().mousemove(function(e){     
  3.      //display the x and y axis values inside the div with the id XY     
  4.    $('#XY').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);     
  5.  
  6. });     
  7.  
  8. // how to use     
  9.  
  10. <DIV id=XY></DIV>     
  11. });   

验证元素是否为空

广州网站建设,网站建设,广州网页设计,广州网站设计
  1.   $(document).ready(function() {     
  2.     if ($('#id').html()) {     
  3.     // do something     
  4.   }     
  5. });   

替换元素


  1. $(document).ready(function() {     
  2. $('#id').replaceWith('    
  3.   <DIV>I have been replaced</DIV>    
  4.   );     
  5. });   
标签:
飞机