常用jquery

2014年11月29日 | | 常用jquery已关闭评论
//autoHeight
var autoHeight = function(mySelect){
    mySelect.css({"min-height": "inherit"});
    var textHeight = mySelect.map(function() {
        return $(this).outerHeight();
    }).get();
    var maxHeight = Math.max.apply(null, textHeight);
    mySelect.css({"min-height": maxHeight});
}
autoHeight($('.ministries-selector li,.free-links ul li'));
$(window).resize(function() {
    autoHeight($('.ministries-selector li,.free-links ul li'));
});
//about scroll
var animateGoto = function(postion,callback){
    $("body,html").animate({"scrollTop":postion},800,callback || function(){});
}
$(".about .widget li").each(function() {
    var myClick = $('a', $(this)),
        This = $(this).index();
    myClick.click(function () {
        jumpTo = $("body").find("h2.title").eq(This);
        animateGoto(jumpTo.position().top - 20);
        return false;
    });
});
//
$(".faq_r ul li .question").click(function() {
    if ( $(this).parent("li").hasClass('open')) {
        $(this).parent("li").find('.answer').slideUp();
        $(".faq_r ul li").removeClass('open');
    }else{
        $(".faq_r ul li .answer").slideUp();
        $(this).parent("li").find('.answer').slideDown();
        $(".faq_r ul li").removeClass('open');
        $(this).parent("li").addClass('open');
    };
});
//bxslider
$('.slider ul').bxSlider({
    minSlides: 2,
    maxSlides: 3,
    slideWidth: 170,
    slideMargin: 10,
    auto: true,
    moveSlides: 1,
    autoControls: false
});
//placeholder
$('input.placeholder, textarea.placeholder').each(function() {
    var $this = $(this),
        $label = $this.siblings('label.placeholder');
    if ($label.length < 1) {
        $label = $('<label/>').attr('class', 'placeholder');
        $label.text($(this).attr('placeholder')).insertBefore($this);
    }
    $this.prop('placeholder', '').bind('focus', function() {
        //$label.hide();
    }).bind('blur change updateState input propertychange', function() {
        if ($this.val() == '') {
            $label.show();
        } else {
            $label.hide();
        }
    }).trigger('updateState');
    $label.bind('click', function() {
        $this.trigger('focus');
    });
});
//select
$(".select").each(function() {
    var selects = "",
        selected = "";
    $(this).find("select option").each(function() {
        selects += "<li>" + $(this).html() + "</li>";
    });
    selected = $(this).find("select option:selected").html();
    //select mandatory
    $(this).find("select option:first").val(''); //z
    if ($(this).find("select").size() > 0) {
        $(this).append('<div class="selected current">' + selected + '</div><ul>' + selects + '</ul>');
        $(this).find("li:eq(0)").addClass("hover");
        //cf7
        //$(this).find("option:eq(0)").val("");
    }
    $(this).find("li").removeClass("hover")
    $(this).find("li:eq(" + $(this).find("select option:selected").index() + ")").addClass("hover");
});
$(".select .selected").on("click", function(e) {
    $(".select").parent().css("z-index", 0);
    $(this).parent().parent().css("z-index", 100);
    $(".select .selected").removeClass("ss");
    if ($(this).parent().find("ul").css("display") == "block") {
        $(this).removeClass("ss");
        $(this).parent().find("ul").fadeOut();
    } else {
        $(this).addClass("ss");
        $(".select ul").hide();
        $(this).parent().find("ul").fadeIn();
    }
});
$(".select li").on("click", function() {
    $(this).parent().parent().find(".select option").removeAttr("selected");
    $(this).parent().parent().find(".nselect option:eq(" + $(this).index() + ")").attr("selected", "selected");
    if ($(this).parent().parent().find(".nselect option:eq(" + $(this).index() + ")").val() != "") {
        $(this).parent().parent().find(".selected").removeClass("current");
    } else {
        $(this).parent().parent().find(".selected").addClass("current");
    }
    $(this).addClass("hover").siblings(".select li").removeClass("hover");
    $(this).parent().parent().find(".selected").html($(this).html());
    $(this).parent().hide();
    $(this).parent().parent().find(".selected").removeClass("ss");
})
$(".select").click(function(e) {
    e.stopPropagation();
});
$(document).click(function() {
    $(".select ul").hide();
    $(".selected").removeClass("ss");
});
//tabs
$(".tabs .titles li").each(function() {
    var This = $(this).index();
    $(this).click(function () {
        $(".tabs .titles li,.contents > ul > li").removeClass('current');
        tabTo = $(".tabs").find(".contents > ul > li").eq(This);
        $(this).addClass('current');
        tabTo.addClass('current');
        if ($(this).hasClass('location')) {
            $(".map iframe").attr('src' , $(".map iframe").attr('src'));
        };
    });
});
//fixed menu hover bug
$(".menu li").each(function(){
    $(this).css({
        width: $(this).outerWidth(),
        "padding-right" : "0"
    });
})
//wordpress jquery
var $ = jQuery.noConflict();
//click
$("html").on('click', function(event) {
  var $target = $(event.target);
  if (!$target.parents().addBack().is("selector")){
    //do something
  };
});
//svgConvert
$('img.svg').each(function(){
    var $img = jQuery(this);
    var imgID = $img.attr('id');
    var imgClass = $img.attr('class');
    var imgURL = $img.attr('src');
    $.get(imgURL, function(data) {
        // Get the SVG tag, ignore the rest
        var $svg = $(data).find('svg');
        // Add replaced image's ID to the new SVG
        if(typeof imgID !== 'undefined') {
            $svg = $svg.attr('id', imgID);
        }
        // Add replaced image's classes to the new SVG
        if(typeof imgClass !== 'undefined') {
            $svg = $svg.attr('class', imgClass+' replaced-svg');
        }
        // Remove any invalid XML tags as per http://validator.w3.org
        $svg = $svg.removeAttr('xmlns:a');
        // Replace image with new SVG
        $img.replaceWith($svg);
    }, 'xml');
});
//scroll
if ( $(window).scrollTop() > $('.menu').position().top ) {
    $('.menu').addClass('fixed');
} else {
    $('.menu').removeClass('fixed');
}