您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

94 行
2.5 KiB

  1. // import $ from 'jquery';
  2. const $ = require('jquery');
  3. class Anchors {
  4. constructor(section) {
  5. this.section = section.addClass('initiated');
  6. this.scrolling = false;
  7. this.headlines = [];
  8. this.initHeadlines();
  9. this.buildLinks();
  10. const anchorLink = window.location.href.match(/#(.*)/);
  11. if (anchorLink != null) {
  12. this.scrollToTarget(anchorLink);
  13. }
  14. }
  15. initHeadlines() {
  16. $('.main-col h2, article.col h2, .action-col .richtext h2').each((i, element) => {
  17. const h2 = $(element);
  18. const closestActionContetnt = h2.closest('.action-content');
  19. const hideOnlineMagazineContactH2 = h2.closest('.online-magazine-contact').length;
  20. if (!h2.closest('.accordion-content').length && !closestActionContetnt.closest(".richtext").length && !hideOnlineMagazineContactH2 && h2.text().length > 0) {
  21. if (h2.is(".showInTableOfContents") || !h2.closest(".link-collection").length) {
  22. this.headlines.push(h2);
  23. }
  24. }
  25. });
  26. }
  27. buildLinks() {
  28. const t = this;
  29. const ul = t.section.find('ul li a');
  30. $(ul).each((i, element) => {
  31. // Add Ids to text h2
  32. const textH2 = $(this.headlines[i]);
  33. if(textH2.length) {
  34. $(textH2).addClass('has-anchor');
  35. $(textH2).append($('<span class="anchor" />').attr('id', 'titleInText' + i));
  36. }
  37. });
  38. this.anchors = $('.main-col, article.col, .action-col').find('h2 .anchor');
  39. t.links = t.section.find('a').on('click', (e) => {
  40. e.preventDefault();
  41. this.handleClick($(e.currentTarget));
  42. });
  43. this.imageAnchors = $('a').find('.image-full-wrapper');
  44. if(this.imageAnchors){
  45. this.imageAnchors.parent('a').addClass('image-link')
  46. }
  47. }
  48. handleClick(a) {
  49. const index = parseInt(a.attr('data-index'));
  50. const target = this.anchors.eq(index).offset().top;
  51. let time = Math.round(Math.abs($(window).scrollTop() - target) / 6 + 300);
  52. if (time > 800) {
  53. time = 800;
  54. }
  55. this.scrolling = true;
  56. $('html, body').animate({'scrollTop': target}, time, 'swing', () => {
  57. this.scrolling = false;
  58. });
  59. }
  60. scrollToTarget(anchorLink) {
  61. const anchorLinkElement = $("#" + anchorLink[1]);
  62. if (anchorLinkElement.length > 0) {
  63. $('html, body').animate({
  64. scrollTop: anchorLinkElement.first().offset().top - 10
  65. }, 10);
  66. }
  67. }
  68. }
  69. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  70. $('.anchorlinks:not(.initiated)').each(function (i) {
  71. new Anchors($(this));
  72. });
  73. });