You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by db...@apache.org on 2016/03/04 00:24:39 UTC

docs commit: CB-10744 Handling name-based anchors and anchors to IDs with special characters.

Repository: cordova-docs
Updated Branches:
  refs/heads/master 72c439e6c -> b7a4a1920


CB-10744 Handling name-based anchors and anchors to IDs with special characters.


Project: http://git-wip-us.apache.org/repos/asf/cordova-docs/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-docs/commit/b7a4a192
Tree: http://git-wip-us.apache.org/repos/asf/cordova-docs/tree/b7a4a192
Diff: http://git-wip-us.apache.org/repos/asf/cordova-docs/diff/b7a4a192

Branch: refs/heads/master
Commit: b7a4a192087b4fdf06a6a510dee1757b5d206485
Parents: 72c439e
Author: Dmitry Blotsky <dm...@gmail.com>
Authored: Mon Feb 29 23:06:21 2016 -0800
Committer: Dmitry Blotsky <dm...@gmail.com>
Committed: Thu Mar 3 15:24:19 2016 -0800

----------------------------------------------------------------------
 www/static/js/index.js | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-docs/blob/b7a4a192/www/static/js/index.js
----------------------------------------------------------------------
diff --git a/www/static/js/index.js b/www/static/js/index.js
index c134d7b..4e5c272 100644
--- a/www/static/js/index.js
+++ b/www/static/js/index.js
@@ -145,17 +145,46 @@ $(document).ready(function () {
 
     // Smooth scroll to anchor links
     $("a[href^='#']").on('click', function(e) {
-        if(this.hash) {
+
+        // scroll only if there is a hash in the link's href
+        var hash = this.hash;
+        if (hash) {
 
             // prevent default anchor click behavior
             e.preventDefault();
 
-            // store hash
-            var hash = this.hash;
+            // get the fragment without the "#" symbol because location.hash
+            // is returned with it
+            var targetName = hash.slice(1);
+
+            // escape single quotes in target name because
+            // we use them in the selector to find matching targets
+            targetName.replace(/'/g, "\\\'").replace(/"/g, "\\\"");
+
+            // check if the target exists by looking at either ID or name
+            // NOTE:
+            //      we're not using "# + targetName" to select by ID
+            //      because the ID might contain special characters that
+            //      are annoying to escape
+            var targetSelector = "*[id='" + targetName + "'], *[name='" + targetName + "']";
+
+            var matchingTargets  = $(targetSelector);
+            if (matchingTargets.length < 1) {
+                return;
+            }
+            if (matchingTargets.length > 1) {
+                console.warn("found more than one anchor to go to; will go to the first one");
+            }
+
+            // get resulting scroll height
+            // NOTE:
+            //      offset() returns the offset for the first element
+            //      if the array contains more than one element
+            var scrollHeight = matchingTargets.offset().top;
 
             // animate
             $('html, body').animate(
-                {scrollTop: $(hash).offset().top},
+                {scrollTop: scrollHeight},
                 300,
                 function () {
                     // when done, add hash to url (default click behaviour)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org