.
--- To put it simply, the tag under must have a href="#id" attribute, and there must be a corresponding such a tag; when the content scrolls to the tag, the corresponding will automatically selected.
--In fact, friends who have done web development know this. In previous versions of HTML, anchor tags usually used , but anchor tags in HTML5 have been abandoned. Instead of the name attribute, use the id attribute
ScrollSpy.prototype.activate = function (target) {
this.activeTarget = target
this.clear()
var selector = this.selector +
'[data-target="' + target + '"],' +
this.selector + '[href="' + target + '"]'
var active = $(selector)
.parents('li')
.addClass('active')
if (active.parent('.dropdown-menu').length) {
active = active
.closest('li.dropdown')
.addClass('active')
}
active.trigger('activate.bs.scrollspy')
}
Copy after login
3. No matter the implementation method, scrollspy requires the use of position: relative; on the element you're spying on. In most cases this is the
. When scrollspying on elements other than the
--- If you monitor the scrolling of the Body, then you must add the position:relative style to the body
--- If what is being monitored is not the Body, but other elements [seemingly this method is common], then you need to add three styles: position:relative;height:500px;overflow-y :scroll;
ScrollSpy.prototype.refresh = function () {
var that = this
var offsetMethod = 'offset'
var offsetBase = 0
this.offsets = []
this.targets = []
this.scrollHeight = this.getScrollHeight()
if (!$.isWindow(this.$scrollElement[0])) {
offsetMethod = 'position'
offsetBase = this.$scrollElement.scrollTop()
}
Copy after login
4. To easily add scrollspy behavior to your topbar navigation, add data-spy="scroll" to the element you want to spy on (most typically this would be the
). Then add the data-target attribute with the ID or class of the parent element of any Bootstrap .navcomponent.
--- You need to add the data-spy="scroll" attribute and data-target attribute to the label of the scrolling content
The data-spy attribute specifies the element to be monitored, and the data-target attribute specifies the nav highlighting that needs to be controlled during scrolling
Look at the initialization source code below again. The position marked in red, the value of this.options.target, is equal to the value of the data-target of the scrolling content element. Seeing this, you may have thought that before defining .nav When making a component, we need to put the .nav in another container (such as a div), and the container needs to have an id attribute (the same value as the data-target needs to be set here).
function ScrollSpy(element, options) {
this.$body = $(document.body)
this.$scrollElement = $(element).is(document.body) ? $(window) : $(element)
this.options = $.extend({}, ScrollSpy.DEFAULTS, options)
this.selector = (this.options.target || '') + ' .nav li > a'
this.offsets = []
this.targets = []
this.activeTarget = null
this.scrollHeight = 0
this.$scrollElement.on('scroll.bs.scrollspy', $.proxy(this.process, this))
this.refresh()
this.process()
}
Copy after login
Copy after login
5. After adding position: relative; in your CSS, call the scrollspy via JavaScript:
$('yourTag').scrollspy({ target: 'nav-parent-div-id' })
-- yourTag is the ID of the element to carry the scrolling content, nav-parent-div-id is the id of the parent element of the .nav element (that is, the value of data-target)
I wrote a bunch of messy stuff, here is a summary of a few simple steps:
1. Add tag
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31