Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

87 lignes
2.5 KiB

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