常用jquery

2014.11.29 11278 人浏览 常用jquery已关闭评论
  1. //autoHeight
  2. var autoHeight = function(mySelect){
  3.     mySelect.css({"min-height""inherit"});
  4.     var textHeight = mySelect.map(function() {
  5.         return $(this).outerHeight();
  6.     }).get();
  7.     var maxHeight = Math.max.apply(null, textHeight);
  8.     mySelect.css({"min-height": maxHeight});
  9. }
  10. autoHeight($('.ministries-selector li,.free-links ul li'));
  11. $(window).resize(function() {
  12.     autoHeight($('.ministries-selector li,.free-links ul li'));
  13. });
  14. //about scroll
  15. var animateGoto = function(postion,callback){
  16.     $("body,html").animate({"scrollTop":postion},800,callback || function(){});
  17. }
  18. $(".about .widget li").each(function() {
  19.     var myClick = $('a', $(this)),
  20.         This = $(this).index();
  21.     myClick.click(function () {
  22.         jumpTo = $("body").find("h2.title").eq(This);
  23.         animateGoto(jumpTo.position().top - 20);
  24.         return false;
  25.     });
  26. });
  27. //
  28. $(".faq_r ul li .question").click(function() {
  29.     if ( $(this).parent("li").hasClass('open')) {
  30.         $(this).parent("li").find('.answer').slideUp();
  31.         $(".faq_r ul li").removeClass('open');
  32.     }else{
  33.         $(".faq_r ul li .answer").slideUp();
  34.         $(this).parent("li").find('.answer').slideDown();
  35.         $(".faq_r ul li").removeClass('open');
  36.         $(this).parent("li").addClass('open');
  37.     };
  38. });
  39. //bxslider
  40. $('.slider ul').bxSlider({
  41.     minSlides: 2,
  42.     maxSlides: 3,
  43.     slideWidth: 170,
  44.     slideMargin: 10,
  45.     auto: true,
  46.     moveSlides: 1,
  47.     autoControls: false
  48. });
  49. //placeholder
  50. $('input.placeholder, textarea.placeholder').each(function() {
  51.     var $this = $(this),
  52.         $label = $this.siblings('label.placeholder');
  53.     if ($label.length < 1) {
  54.         $label = $('<label/>').attr('class', 'placeholder');
  55.         $label.text($(this).attr('placeholder')).insertBefore($this);
  56.     }
  57.     $this.prop('placeholder', '').bind('focus', function() {
  58.         //$label.hide();
  59.     }).bind('blur change updateState input propertychange', function() {
  60.         if ($this.val() == '') {
  61.             $label.show();
  62.         } else {
  63.             $label.hide();
  64.         }
  65.     }).trigger('updateState');
  66.     $label.bind('click', function() {
  67.         $this.trigger('focus');
  68.     });
  69. });
  70. //select
  71. $(".select").each(function() {
  72.     var selects = "",
  73.         selected = "";
  74.     $(this).find("select option").each(function() {
  75.         selects += "<li>" + $(this).html() + "</li>";
  76.     });
  77.     selected = $(this).find("select option:selected").html();
  78.     //select mandatory
  79.     $(this).find("select option:first").val(''); //z
  80.     if ($(this).find("select").size() > 0) {
  81.         $(this).append('<div class="selected current">' + selected + '</div><ul>' + selects + '</ul>');
  82.         $(this).find("li:eq(0)").addClass("hover");
  83.         //cf7
  84.         //$(this).find("option:eq(0)").val("");
  85.     }
  86.     $(this).find("li").removeClass("hover")
  87.     $(this).find("li:eq(" + $(this).find("select option:selected").index() + ")").addClass("hover");
  88. });
  89. $(".select .selected").on("click"function(e) {
  90.     $(".select").parent().css("z-index", 0);
  91.     $(this).parent().parent().css("z-index", 100);
  92.     $(".select .selected").removeClass("ss");
  93.     if ($(this).parent().find("ul").css("display") == "block") {
  94.         $(this).removeClass("ss");
  95.         $(this).parent().find("ul").fadeOut();
  96.     } else {
  97.         $(this).addClass("ss");
  98.         $(".select ul").hide();
  99.         $(this).parent().find("ul").fadeIn();
  100.     }
  101. });
  102. $(".select li").on("click"function() {
  103.     $(this).parent().parent().find(".select option").removeAttr("selected");
  104.     $(this).parent().parent().find(".nselect option:eq(" + $(this).index() + ")").attr("selected""selected");
  105.     if ($(this).parent().parent().find(".nselect option:eq(" + $(this).index() + ")").val() != "") {
  106.         $(this).parent().parent().find(".selected").removeClass("current");
  107.     } else {
  108.         $(this).parent().parent().find(".selected").addClass("current");
  109.     }
  110.     $(this).addClass("hover").siblings(".select li").removeClass("hover");
  111.     $(this).parent().parent().find(".selected").html($(this).html());
  112.     $(this).parent().hide();
  113.     $(this).parent().parent().find(".selected").removeClass("ss");
  114. })
  115. $(".select").click(function(e) {
  116.     e.stopPropagation();
  117. });
  118. $(document).click(function() {
  119.     $(".select ul").hide();
  120.     $(".selected").removeClass("ss");
  121. });
  122. //tabs
  123. $(".tabs .titles li").each(function() {
  124.     var This = $(this).index();
  125.     $(this).click(function () {
  126.         $(".tabs .titles li,.contents > ul > li").removeClass('current');
  127.         tabTo = $(".tabs").find(".contents > ul > li").eq(This);
  128.         $(this).addClass('current');
  129.         tabTo.addClass('current');
  130.         if ($(this).hasClass('location')) {
  131.             $(".map iframe").attr('src' , $(".map iframe").attr('src'));
  132.         };
  133.     });
  134. });
  135. //fixed menu hover bug
  136. $(".menu li").each(function(){
  137.     $(this).css({
  138.         width: $(this).outerWidth(),
  139.         "padding-right" : "0"
  140.     });
  141. })
  142. //wordpress jquery
  143. var $ = jQuery.noConflict();
  144. //click
  145. $("html").on('click', function(event) {
  146.   var $target = $(event.target);
  147.   if (!$target.parents().addBack().is("selector")){
  148.     //do something
  149.   };
  150. });
  151. //svgConvert
  152. $('img.svg').each(function(){
  153.     var $img = jQuery(this);
  154.     var imgID = $img.attr('id');
  155.     var imgClass = $img.attr('class');
  156.     var imgURL = $img.attr('src');
  157.     $.get(imgURL, function(data) {
  158.         // Get the SVG tag, ignore the rest
  159.         var $svg = $(data).find('svg');
  160.         // Add replaced image's ID to the new SVG
  161.         if(typeof imgID !== 'undefined') {
  162.             $svg = $svg.attr('id', imgID);
  163.         }
  164.         // Add replaced image's classes to the new SVG
  165.         if(typeof imgClass !== 'undefined') {
  166.             $svg = $svg.attr('class', imgClass+' replaced-svg');
  167.         }
  168.         // Remove any invalid XML tags as per http://validator.w3.org
  169.         $svg = $svg.removeAttr('xmlns:a');
  170.         // Replace image with new SVG
  171.         $img.replaceWith($svg);
  172.     }, 'xml');
  173. });
  174. //scroll
  175. if ( $(window).scrollTop() > $('.menu').position().top ) {
  176.     $('.menu').addClass('fixed');
  177. else {
  178.     $('.menu').removeClass('fixed');
  179. }

文章地址:https://huilang.me/isay/chang-yong-jquery/