ant-design/site/static/script.js

221 lines
6.4 KiB
JavaScript
Raw Normal View History

2015-09-11 14:57:32 +08:00
InstantClickChangeFns.push(function() {
2015-12-13 20:08:58 +08:00
// fix hash id link
if (window.location.href.indexOf('#') > 0) {
setTimeout(function() {
window.location.href = window.location.href;
}, 0);
}
2016-01-06 19:47:13 +08:00
$('.component-demos .icon-all').off('click');
2015-05-16 16:20:49 +08:00
$('.component-demos .icon-all').on('click', function() {
2015-06-03 17:23:05 +08:00
if ($(this).hasClass('expand')) {
$(this).removeClass('expand');
$('.code-box .highlight').animate({
height: 'hide',
opacity: 0
}, 150);
} else {
$(this).addClass('expand');
$('.code-box .highlight').animate({
height: 'show',
opacity: 1
}, 150);
}
2015-05-18 17:33:42 +08:00
});
$('.code-box').each(function(i, item) {
item = $(item);
item.find('.highlight').appendTo(item);
});
2016-01-06 21:40:48 +08:00
$('.code-boxes').off('click');
2015-05-18 17:33:42 +08:00
$('.code-boxes').on('click', '.collapse', function() {
2015-06-04 10:57:06 +08:00
var highlightBox = $(this).parent().parent().find('.highlight');
2015-06-08 22:42:36 +08:00
var codeVisible = highlightBox.is(':visible');
2015-06-04 10:57:06 +08:00
highlightBox.animate({
2015-06-08 22:42:36 +08:00
height: codeVisible ? 'hide' : 'show',
opacity: codeVisible ? 0 : 1
2015-06-03 17:23:05 +08:00
}, 150);
2015-06-08 22:42:36 +08:00
if (codeVisible) {
$(this).parent().parent().removeClass('expand');
} else {
$(this).parent().parent().addClass('expand');
}
2015-06-02 20:49:40 +08:00
});
2015-08-19 11:26:30 +08:00
2016-01-02 15:04:39 +08:00
function hashChange() {
2016-01-06 15:27:56 +08:00
$('.anchor a').removeClass('current');
$('.anchor a[href="' + decodeURI(location.hash) + '"]').addClass('current');
2016-01-02 15:04:39 +08:00
}
hashChange();
// 高亮侧边演示菜单
2016-01-06 19:47:13 +08:00
$(window).off('hashchange');
2016-01-02 15:04:39 +08:00
$(window).on('hashchange', hashChange);
2015-08-19 11:26:30 +08:00
// 移动 API 文档到演示下方
$('.markdown #api').nextAll().andSelf().appendTo('.api-container');
2016-01-06 21:40:48 +08:00
// 滚动时固定锚点、高亮当前项
2016-01-06 22:37:55 +08:00
if ($('.toc')[0]) {
var titles = $('.markdown :header:not(h1)');
var doc = $(document);
var tocTop = $('.toc').offset().top;
function onScroll() {
var top = doc.scrollTop(), i;
if (top >= doc.height()- $(window).height() - 20) {
i = titles.length - 1;
} else {
for (i=0; i<titles.length; i++) {
if (top < titles.eq(i).offset().top - 20) {
break;
}
2016-01-06 15:27:56 +08:00
}
2016-01-06 22:37:55 +08:00
i--;
i = (i < 0) ? 0 : i;
i = (i > titles.length - 1) ? (titles.length - 1) : i;
2016-01-06 15:27:56 +08:00
}
2016-01-06 22:37:55 +08:00
$('.anchor a').removeClass('current');
$('.anchor a').eq(i).addClass('current');
2016-01-06 21:40:48 +08:00
2016-01-06 22:37:55 +08:00
if (top >= tocTop) {
$('.toc').addClass('sticky');
} else {
$('.toc').removeClass('sticky');
}
2016-01-06 21:40:48 +08:00
}
2016-01-06 22:37:55 +08:00
onScroll();
// 滚动高亮
$(window).off('scroll');
$(window).on('scroll', onScroll);
2016-01-06 15:27:56 +08:00
}
2016-01-04 22:53:44 +08:00
// 添加上一页下一页
if ($('.aside-container li > a').length > 0) {
var links = $('.aside-container li > a');
var currentLinkIndex = -1;
links.each(function(i, item) {
if ($(item).parent().hasClass('current')) {
currentLinkIndex = i;
}
});
var prevNextNavNode = $('<div class="prev-next-nav"></div>');
var prevLink = links[currentLinkIndex - 1];
var nextLink = links[currentLinkIndex + 1];
if (prevLink) {
2016-01-06 15:27:56 +08:00
prevNextNavNode.append('<a class="prev-page" href="' + prevLink.href + '">' + prevLink.innerHTML + '</a>');
2016-01-06 18:49:43 +08:00
} else {
prevNextNavNode.append('<span class="prev-page"></span>');
2016-01-04 22:53:44 +08:00
}
if (nextLink) {
2016-01-06 15:27:56 +08:00
prevNextNavNode.append('<a class="next-page" href="' + nextLink.href + '">' + nextLink.innerHTML + '</a>');
2016-01-06 18:49:43 +08:00
} else {
prevNextNavNode.append('<span class="next-page"></span>');
2016-01-04 22:53:44 +08:00
}
prevNextNavNode.appendTo('.main-container');
}
2015-08-26 22:16:20 +08:00
$('.nav-phone-icon').click(function() {
$(this).prev().toggle();
});
2015-08-19 11:26:30 +08:00
$.easing['jswing'] = $.easing['swing'];
$.extend($.easing,{
easeInCirc: function (x, t, b, c, d) {
return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
},
easeOutCirc: function (x, t, b, c, d) {
return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
},
easeInOutCirc: function (x, t, b, c, d) {
if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
}
});
2015-06-02 20:23:18 +08:00
var navFunc = {
2015-05-27 11:35:01 +08:00
navStrArr: [],
2015-06-02 20:23:18 +08:00
init: function() {
2016-01-06 18:49:43 +08:00
if (this.navBar) {
return;
}
2015-12-19 14:00:40 +08:00
this.navBox = $(".nav");
this.navBar = this.navBox.find(".bar");
this.navList = this.navBox.find("ul li");
this.navNum = $(".current").index();
this.navBarAnim();
this.highlightCurrentNav();
$(window).bind("resize", this.highlightCurrentNav);
this.navBar.show();
2015-05-27 11:35:01 +08:00
},
2015-12-19 14:00:40 +08:00
highlightCurrentNav: function(target) {
target = target || this.navList.eq(this.navNum);
2015-12-25 17:19:24 +08:00
this.navBar && this.navBar.css({
2015-12-19 14:00:40 +08:00
left: target.position().left,
width: target.outerWidth()
});
2015-05-27 11:35:01 +08:00
},
2015-06-02 20:23:18 +08:00
navBarAnim: function() {
2015-12-19 14:00:40 +08:00
var self = this, delay;
2015-06-02 20:23:18 +08:00
self.navList.bind("mouseenter", function(e) {
2015-05-27 11:35:01 +08:00
clearTimeout(delay);
2015-12-19 14:00:40 +08:00
self.highlightCurrentNav($(e.currentTarget));
2015-05-27 11:35:01 +08:00
});
2015-06-02 20:23:18 +08:00
self.navList.bind("mouseleave", function(e) {
delay = setTimeout(function() {
2015-12-19 14:00:40 +08:00
self.highlightCurrentNav();
2015-05-27 11:35:01 +08:00
}, 500);
});
}
};
navFunc.init();
2015-06-02 20:23:18 +08:00
var listFunc = {
num: 0,
init: function() {
2015-12-19 14:00:40 +08:00
this.listBox = $(".aside-container>ul");
if (!this.listBox.length) {
2015-06-02 20:36:53 +08:00
return;
2015-06-02 20:23:18 +08:00
}
2015-12-19 14:00:40 +08:00
this.getUrlNum();
this.addTitleEvent();
2015-06-02 20:23:18 +08:00
},
getUrlNum: function() {
var self = this,
url = location.href,
str = "";
for (var i = 0; i < self.listBox.find("a").length; i++) {
var m = self.listBox.find("a").eq(i);
if (m.attr("href") == "./" || url.indexOf(m.attr("href")) >= 0) {
self.num = m.parent().parent().parent().index();
2015-05-27 18:28:38 +08:00
}
2015-06-02 20:23:18 +08:00
}
},
addTitleEvent: function() {
var self = this;
var title = self.listBox.find("h4");
title.bind("click", function(e) {
2015-06-10 11:51:51 +08:00
var parent = $(this).parent(),
2015-08-19 11:26:30 +08:00
list=parent.find("ul");
2015-06-02 20:23:18 +08:00
if (parent.attr("open")) {
parent.removeAttr("open");
if (parent.index() == self.num) {
2015-06-02 20:36:53 +08:00
$(this).addClass("current");
2015-06-02 20:23:18 +08:00
}
2015-06-10 11:51:51 +08:00
list.animate({marginTop:-list.height()},400,"easeInOutCirc",function (){
2015-08-19 11:26:30 +08:00
list.css({"display":"none"})
2015-06-10 11:51:51 +08:00
})
2015-06-02 20:23:18 +08:00
} else {
parent.attr("open", true);
if (parent.index() == self.num) {
2015-06-02 20:36:53 +08:00
$(this).removeClass("current");
2015-06-02 20:23:18 +08:00
}
2015-08-19 11:26:30 +08:00
list.css({"display":"block","margin-top":-list.height()});
list.animate({marginTop:0},400,"easeInOutCirc")
2015-06-02 20:23:18 +08:00
}
2015-06-02 20:36:53 +08:00
});
2015-06-02 20:23:18 +08:00
}
};
listFunc.init();
2015-05-08 14:57:59 +08:00
});