$(document).ready(function(){
// 判断浏览器是否支持sticky 属性
var sticky = (function () {
var vendorList = ['', '-webkit-', '-ms-', '-moz-', '-o-'],
vendorListLength = vendorList.length,
stickyElement = document.createElement('div');
for (var i = 0; i < vendorListLength; i++) {
stickyElement.style.position = vendorList[i] + 'sticky';
//console.log('stickyElement',stickyElement)
if (stickyElement.style.position !== '') {
return true;
}
}
return false;
})();
console.log(location);
$('.language').on('click', function(e){
e.preventDefault();
var url = $(this).attr('href').replace(/\/$/, '');
url += location.pathname;
location.href = url;
})
var heightFix = 55;
var mdHeightFix = 70 + 50;
var qrCode = $('img.qrCode');
if(qrCode.length > 0){
var url = location.href.replace(/\/(similar|literature|archive|relation_.*?)\.html$/gi, '/');
qrCode.attr('src', 'http://www.ye8.net/yixuezhishi/qr.svg?msg=' + url);
console.log('url='+url);
}
var colors = ['#FF99CC', '#FFCCCC', '#FFFFCC', '#FFCCFF', '#CCFFFF', '#CCCCFF', '#CCFFCC', '#CCFFCC', '#99CCCC', '#FFCC66', '#99CC99'];
$(".cloud a").each(function(){
var index = Math.floor((Math.random() * colors.length));
$(this).css('background-color', colors[index]);
});
if($('.menu').css('display') == 'block'){
$('.navbar-drawer').height($(window).height());
} else {
$('.navbar-drawer').removeAttr('style');
}
$('.menu').on('click', function(){
showMenu();
});
$('.navbar-drawer-back').on('click',function(){
hideMenu();
});
function showMenu(){
if(!$('.menu').hasClass('show')){
$('.navbar-drawer-back').removeClass('hide');
$('.menu').addClass('show');
$('.navbar-drawer').addClass('in');
$('body').addClass('noScroll');
}
}
function hideMenu(){
if($('.menu').hasClass('show')){
$('.navbar-drawer-back').addClass('hide');
$('.menu').removeClass('show');
$('.navbar-drawer').removeClass('in');
$('body').removeClass('noScroll');
}
}
$('.searchBtn').on('click', function(){
//console.log($('input[name="q"]').val()!= '');
if($('input[name="q"]').val().replace(/\s+/gi, '') != ''){
$('.searcher').submit();
} else {
alert('请输入关键词');
}
});
$('.dropdown-ep').css('width', $('input[name="q"]').outerWidth() + 'px');
$('.dropdown-ep').hide();
var to;
function search(){
var query = $('input[name="q"]').val();
if(query) {
console.log('search ' + query);
$.ajax({
method: 'post',
url: '/expanded',
data: {
'q': query
},
dataType: 'json',
success: function(res){
console.log(res);
if(res.status == 'success') {
var ul = $('
');
res.data.map(function(item){
ul.append('' + item + '');
});
$('.dropdown-ep .data').html(ul);
$('.dropdown-ep').show();
$('.ex').off('click').on('click', function(){
$('input[name="q"]').val($(this).html());
$('form').submit();
});
}
}
});
}
};
$('input[name="q"]').on('input propertychange', function(){
clearTimeout(to);
var query = $(this).val();
if(query) {
to = setTimeout(search, 1000);
} else {
$('.dropdown-ep').hide();
}
}).on('blur', function() {
setTimeout(function(){
$('.dropdown-ep').hide();
}, 100);
});
if($('.indexBar').length == 0){
$('body').append(
''
);
}
$('.content img').on('click', function(){
var src = $(this).attr('src');
if(/.*\/ZoomImg/i.test(src)){
src = src.replace(/\.(png|jpg|gif|jpeg)$/gi, '');
src = src.replace(/ZoomImg/gi, 'Img');
console.log('showPic:' + src);
$('#staticBackdrop').find('img').attr('src', src);
$('#staticBackdrop').modal('show');
}
});
//左侧目录识别
var hasLeft = $('.left').length > 0;
var position = []; //内容中各标题锚点位置
var keys= []; //内容中各标题锚点name
var isScrolling = false;
//有左侧栏的页面(词条页)
if(hasLeft){
//标记当前页面连接qrCode
$('.qrcode').html(
'
\
可用医学百科App / 微信 / 手机浏览器 扫码
'
);
//将目录按钮添加至右下角悬浮按钮组
$('.stickyBottom').append(
''
);
$('body').append('');
$('.showCataButton a').on('click', function(e){
e.preventDefault();
showCata();
});
$('.cata-drawer-back').on('click', function(){
hideCata();
});
$('.cata-drawer-back')[0].addEventListener('touchmove', function(e){
e.preventDefault();
console.log('touchmove');
}, {passive: false});
var widthTool = $('');
widthTool.find('.addWidth').click(function(){
console.log('clickf');
var _left = $('.left').parent();
var _right = $('.main');
var lgWidth = 3;
var mdWidth = 4;
_left.attr('class').split(' ').forEach(function(e){
if(/col-lg-(\d+)/i.test(e)){
lgWidth = parseInt(e.replace(/col-lg-(\d+)/gi, '$1'));
} else if(/col-md-(\d+)/i.test(e)){
mdWidth = parseInt(e.replace(/col-md-(\d+)/gi, '$1'));
}
});
if(lgWidth + 1 <= 5){
_left.removeClass('col-lg-' + lgWidth).addClass('col-lg-'+ (lgWidth + 1));
_right.removeClass('col-lg-' + (12 - lgWidth)).addClass('col-lg-' + (11 - lgWidth));
}
if(mdWidth + 1 <= 6){
_left.removeClass('col-md-' + mdWidth).addClass('col-md-'+ (mdWidth + 1));
_right.removeClass('col-md-' + (12 - mdWidth)).addClass('col-md-' + (11 - mdWidth));
}
});
widthTool.find('.reduceWidth').click(function(){
console.log('clickf');
var _left = $('.left').parent();
var _right = $('.main');
var lgWidth = 3;
var mdWidth = 4;
_left.attr('class').split(' ').forEach(function(e){
if(/col-lg-(\d+)/gi.test(e)){
lgWidth = parseInt(e.replace(/col-lg-(\d+)/gi, '$1'));
} else if(/col-md-(\d+)/i.test(e)){
mdWidth = parseInt(e.replace(/col-md-(\d+)/gi, '$1'));
}
});
if(lgWidth - 1 >= 3){
_left.removeClass('col-lg-' + lgWidth).addClass('col-lg-'+ (lgWidth - 1));
_right.removeClass('col-lg-' + (12 - lgWidth)).addClass('col-lg-' + (13 - lgWidth));
}
if(mdWidth - 1 >= 4){
_left.removeClass('col-md-' + mdWidth).addClass('col-md-'+ (mdWidth - 1));
_right.removeClass('col-md-' + (12 - mdWidth)).addClass('col-md-' + (13 - mdWidth));
}
});
var catalogueTool = $('.tab-view').popover({
content: widthTool,
placement: 'left',
html: true,
container: $('.tab-view'),
});
$('.left').mouseover(function(){
catalogueTool.popover('show');
});
catalogueTool.on('shown.bs.popover', function(){
console.log('shown');
})
function showCata(){
if(!$('.left').hasClass('in')){
$('.cata-drawer-back').removeClass('hide');
$('.left').addClass('in');
$('.left .tab-view').height($(window).height() - heightFix);
$('body').addClass('noScroll');
scrollToCurrentCata();
}
}
function hideCata(){
if($('.left').hasClass('in')){
$('.cata-drawer-back').addClass('hide');
$('.left').removeClass('in');
$('.left .tab-view').removeAttr('style');
$('body').removeClass('noScroll');
}
}
//当前目录,用于滚动到location.hash位置
var currentCata;
var currentCataPO;
//用于防止滚动监听干扰手动点击
var clickScrolling = false;
console.log(location.hash);
//highlight location.hash给的当前目录
if(location.hash){
currentCata =$(".catalogue a[href='" + location.hash + "']");
scrollToCurrentCata();
}
//点击目录,高亮及滚动内容
$(".catalogue li a").on('click', function(e){
e.preventDefault();
hideCata();
currentCata = $(this);
clickScrolling = true;
scrollToCurrentCata(true);
});
//构建内容中锚点位置和name数组
$('a[name]').each(function(){
var k = $(this).attr('name');
var v = $(this).offset().top;
position.push(v);
keys.push(k);
});
var current = 0;
var lastPO = 0;
//初始滚动位置
var po = $(document).scrollTop();
for(i=0; i lastPO){ //向上滚动
lastPO = po;
for(i=start; i=0; i--){
if(po > position[i]){
start = i;
break;
}
}
//console.log(start);
if(current != start){
current = start;
currentCata = $('.catalogue a[href="#' + keys[current] + '"');
scrollToCurrentCata();
}
}
}
});
if($('.left').css('position') == 'sticky'){
$('.tab-view').animate({
height: $(window).height() - mdHeightFix
}, 300);
} else {
$('.tab-view').animate({
height: $(window).height() - heightFix
}, 300);
}
scrollToCurrentCata();
//目录\二维码切换
$('.tab-bar button').on('click', function(){
var name = $(this).attr('name');
//切换
changeButton(name);
});
function changeButton(name){
$('.tab-bar button').each(function(){
var _this = $(this);
var _name = $(this).attr('name');
if(_name == name){
_this.addClass('active');
$('.' + _name).removeClass('hide');
} else {
_this.removeClass('active');
$('.' + _name).addClass('hide');
}
})
}
//滚动目录
function scrollToCurrentCata(contentScroll = false){
if(currentCata && currentCata.length > 0){
//高亮当前目录
$(".catalogue a").removeClass('active');
$(".catalogue a").parent().removeClass('active');
currentCata.addClass('active');
currentCata.parent().addClass('active');
//滚动目录到能显示当前目录位置
currentCataPO = currentCata.offset().top - $('.tab-view').offset().top - 80 + $('.tab-view').scrollTop();
if(currentCata < 0) currentCata = 0;
//如果正在滚动,只高亮,不再叠加新的滚动
if($('.tab-view').scrollTop() != currentCataPO && !isScrolling){
isScrolling = true;
$('.tab-view').animate({
scrollTop: currentCataPO
}, 500, function(){
//滚动结束,检查当前位置是否正确,不正确则调整
if($('.tab-view').scrollTop() != currentCataPO){
$('.tab-view').animate({
scrollTop: currentCataPO
}, 500, function(){
isScrolling = false;
});
} else {
isScrolling = false;
}
});
}
//需要滚动内容的滚动内容(点击才需要)
if(contentScroll){
var position = $(".main a[name='" + currentCata.attr('href').replace(/#/, '') + "']").offset().top - 100;
//console.log(position);
$(document).scrollTop(position);
setTimeout(function(){
clickScrolling = false;
}, 500);
}
}
}
}
var winW = $(window).width();
$(window).on('resize', function(){
//小屏幕显示菜单按钮时
if($('.menu').css('display') == 'block'){
if($('.navbar-drawer').height() != $(window).height()){
$('.navbar-drawer').height($(window).height());
}
}
//没显示菜单按钮时
else {
$('.navbar-drawer').removeAttr('style');
hideMenu();
if(hasLeft){
hideCata();
}
}
if(hasLeft){
//大屏幕
if($('.left').css('position') == 'sticky') {
if($('.tab-view').height() != $(window).height() - mdHeightFix){
$('.tab-view').height( $(window).height() - mdHeightFix);
}
}
//小屏幕
else {
if($('.left .tab-view').height() != $(window).height() - heightFix){
$('.left .tab-view').height($(window).height() - heightFix);
}
//切换回目录
if($('.tab-view .catalogue').hasClass('hide')){
changeButton('catalogue');
}
}
//宽度更改超过150(基本是横竖平切换了)时重新滚动目录和内容
if(Math.abs($(window).width() - winW) > 150){
winW = $(window).width();
scrollToCurrentCata(true);
}
//窗口改变后重新构建内容中锚点位置数组(name没变化,不用更新)
if($('.left').css('position') == 'sticky'){
position = [];
$('a[name]').each(function(){
var v = $(this).offset().top;
position.push(v);
});
}
}
});
//防止广告破坏页面结构,navbar中出现广告先后挪、row中出现广告则向前挪
let nodes = [];
var timer;
document.addEventListener('DOMNodeInserted', function(e) {
let target = $(e.target);
let related = $(e.relatedNode);
if(target.hasClass('google-auto-placed') && nodes.indexOf(target) < 0 && (related.hasClass('row') || related.hasClass('navbar-drawer'))) {
nodes.push(target);
clearTimeout(timer);
timer = setTimeout(function() {
for(let n in nodes) {
let parent = nodes[n].parent();
if(parent.hasClass('navbar-drawer')) {
$('.navbar').after(nodes[n]);
} else if(parent.hasClass('row')) {
parent.before(nodes[n]);
}
}
}, 100);
}
}, false);
});
function activeTab(name){
$('.nav-tabs li').each(function(){
var _this = $(this);
if(_this.attr('name') == name){
_this.find('a').addClass('active');
} else {
_this.find('a').removeClass('active');
}
});
}