(function scrollToChapter(undefined) { 'use strict'; $('#loading_screen').show(); var $to = false, jump = true, loaded = false, checkInterval, scrollDone = false, observer; let finalize = ()=>{ if(scrollDone) return; $('#loading_screen').hide(); scrollDone = true; if(observer) observer.disconnect(); clearInterval(checkInterval); console.log('Finalized intial scrolling'); }; let needScrollResult = null; let needScroll = ()=>{ if(needScrollResult === null){ let hashMatch = !!location.hash && location.hash.indexOf('#sel=') { if(scrollDone) return false; if( !needScroll() || scrollToHash() || scrollToJump() || highlightByLocalStorage() ) finalize(); }; //init if(window.az_book_ready){ console.log('az_book_ready'); chooseScrollMethod(); }else{ //Проверяем по мере подгрузки, появился ли элемент, к которому нужно прокрутить var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; if(MutationObserver){ observer = new MutationObserver(mutationsList=>{ console.log('Mutation'); chooseScrollMethod(); }); observer.observe(document.querySelector('body'), { childList:true, subtree:true }); }else{ checkInterval = setInterval(chooseScrollMethod, 150); } $(window).on('load', ()=>{ loaded = true; chooseScrollMethod(); finalize(); }); } // scroll methods function scrollToHash (){ if ('scrollRestoration' in history) history.scrollRestoration = 'auto'; var path = location.hash.substr(1); if (path && (/^p(\d+-)+\d+$/.test(path) || /^z(\d+-)*\d+$/.test(path))) { path = path.substr(1).split('-'); path[0]++; //Индекс почему-то всегда попадает в заголовок, а должен в div за ним. $to = $('.book > :nth-child(' + path.join(') > :nth-child(') + ')'); if (!$to.length && path.length > 1 && path[0] >= 15 && path[0] .img-ico').first().nextAll('div:visible').first().find(' > :nth-child(' + path.slice(1).join(') > :nth-child(') + ')'); } if (!$to.length && path.length > 1) { // @fixme: последнему сдвигу нельяз доверять (зависит от наличия выделения маши/поиска) $to = $('.book > :nth-child(' + path.slice(0,-1).join(') > :nth-child(') + ')'); } if ($to.length) { jump = false; console.log('scrollToHash found'); return scrollToJump(); } } return false; } function scrollToJump() { if (!$to) $to = find(); if (!$to){ console.log('scrollToJump target not found'); return false; } console.log('scrollToJump go'); if (!loaded && ('scrollRestoration' in history)) history.scrollRestoration = 'manual'; if ($to[0].scrollIntoView) $to[0].scrollIntoView({behavior: jump ? 'instant' : 'smooth', block: 'start'}); else $('html, body').animate({scrollTop: $to.offset().top}, jump ? 0 : 400); return true; } function find() { if (book.jump) { var $jump = $(book.jump), $hash; if (!$jump.attr('id')) $(function() {$(document).triggerHandler('clearHighlight')}); if (/^#\w+$/.test(location.hash) && ($hash = $(location.hash)).length && $hash[0].className === $jump[0].className) { book.jump = location.hash; $jump = $hash; } return $jump; } var base = location.pathname.match(/\/([\d_]+)$/), anchor, exact = [], $target; if (!base) return false; base = base[1].split('_'); if (base.length