From 8b80efd53433821f694a3aec50d1485bd1416180 Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Thu, 23 Feb 2017 19:05:36 +0100 Subject: [PATCH] Fix scroll to anchor --- .../legislation_annotatable.js.coffee | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/legislation_annotatable.js.coffee b/app/assets/javascripts/legislation_annotatable.js.coffee index 3c77238f2..d843d4629 100644 --- a/app/assets/javascripts/legislation_annotatable.js.coffee +++ b/app/assets/javascripts/legislation_annotatable.js.coffee @@ -121,14 +121,21 @@ App.LegislationAnnotatable = anchor = $(location).attr('hash') if anchor && anchor.startsWith('#annotation') ann_id = anchor.split("-")[-1..] - el = $("span[data-annotation-id='" + ann_id + "']") - App.LegislationAllegations.show_comments() - $('html,body').animate({scrollTop: el.offset().top}) - $.event.trigger - type: "renderLegislationAnnotation" - annotation_id: ann_id - annotation_url: el.closest(".legislation-annotatable").data("legislation-annotatable-base-url") - offset: el.offset()["top"] + + checkExist = setInterval((-> + if $("span[data-annotation-id='" + ann_id + "']").length + el = $("span[data-annotation-id='" + ann_id + "']") + el.addClass('current-annotation') + App.LegislationAllegations.show_comments() + $('html,body').animate({scrollTop: el.offset().top}) + $.event.trigger + type: "renderLegislationAnnotation" + annotation_id: ann_id + annotation_url: el.closest(".legislation-annotatable").data("legislation-annotatable-base-url") + offset: el.offset()["top"] + clearInterval checkExist + return + ), 100) initialize: -> $(document).off("renderLegislationAnnotation").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments)