mirror of
https://github.com/tenrok/bootstrap.git
synced 2026-06-05 16:42:29 +03:00
Add AnchorJS for docs anchors.
This commit is contained in:
Vendored
+7
-2
File diff suppressed because one or more lines are too long
@@ -9,7 +9,7 @@
|
||||
* details, see http://creativecommons.org/licenses/by/3.0/.
|
||||
*/
|
||||
|
||||
/* global ZeroClipboard */
|
||||
/* global ZeroClipboard, addAnchors */
|
||||
|
||||
!function ($) {
|
||||
'use strict';
|
||||
@@ -174,3 +174,8 @@
|
||||
})
|
||||
|
||||
}(jQuery)
|
||||
|
||||
;(function () {
|
||||
'use strict';
|
||||
addAnchors('.bs-docs-container h1, .bs-docs-container h2, .bs-docs-container h3, .bs-docs-container h4, .bs-docs-container h5');
|
||||
})();
|
||||
|
||||
Vendored
+48
@@ -0,0 +1,48 @@
|
||||
/*!
|
||||
* AnchorJS - v0.1.0 - 2014-08-17
|
||||
* https://github.com/bryanbraun/anchorjs
|
||||
* Copyright (c) 2014 Bryan Braun; Licensed MIT
|
||||
*/
|
||||
|
||||
function addAnchors(selector) {
|
||||
'use strict';
|
||||
|
||||
// Sensible default selector, if none is provided.
|
||||
if (!selector) {
|
||||
selector = 'h1, h2, h3, h4, h5, h6';
|
||||
} else if (typeof selector !== 'string') {
|
||||
throw new Error('AnchorJS accepts only strings; you used a ' + typeof selector);
|
||||
}
|
||||
|
||||
// Select any elements that match the provided selector.
|
||||
var elements = document.querySelectorAll(selector);
|
||||
|
||||
// Loop through the selected elements.
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
var elementID;
|
||||
|
||||
if (elements[i].hasAttribute('id')) {
|
||||
elementID = elements[i].getAttribute('id');
|
||||
} else {
|
||||
// We need to create an ID on our element. First, we find which text selection method is available to the browser.
|
||||
var textMethod = document.body.textContent ? 'textContent' : 'innerText';
|
||||
|
||||
// Get the text inside our element
|
||||
var roughText = elements[i][textMethod];
|
||||
|
||||
// Refine it so it makes a good ID. Makes all lowercase and hyphen separated.
|
||||
// Ex. Hello World > hello-world
|
||||
var tidyText = roughText.replace(/\s+/g, '-').toLowerCase();
|
||||
|
||||
// Assign it to our element.
|
||||
// Currently the setAttribute element is only supported in IE9 and above.
|
||||
elements[i].setAttribute('id', tidyText);
|
||||
|
||||
// Grab it for use in our anchor.
|
||||
elementID = tidyText;
|
||||
}
|
||||
var anchor = '<a class="anchorjs-link" href="#' + elementID + '"><span class="anchorjs-icon"></span></a>';
|
||||
|
||||
elements[i].innerHTML += anchor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user