Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • B bootstrap
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 263
    • Issues 263
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 114
    • Merge requests 114
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Bootstrap
  • bootstrap
  • Issues
  • #6013
Closed
Open
Issue created Nov 26, 2012 by Administrator@rootContributor

scrollSpy - offset calculation

Created by: davidgengenbach

I had an issue with the offset calculation of the items within a scrollspy element.

In the refresh function (which I had to use to recalculate the offsets) there is a calculation issue!

The offset is calculated with

$href.position().top

If the $scrollElement is already scrolled the offsets can be negative (like -200). You have to add the scrollTop() of the $scrollElement to it!

Sorry for my bad english!

refresh: function() {
    var self = this,
        $targets;

    // added
    var scrollTop = self.$scrollElement.scrollTop();

    this.offsets = $([]);
    this.targets = $([]);

    $targets = this.$body.find(this.selector).map(function() {
        var $el = $(this),
            href = $el.data('target') || $el.attr('href'),
            $href = /^#\w/.test(href) && $(href);
        return ($href && $href.length && [
            [$href.position().top, href]
        ]) || null;
    }).sort(function(a, b) {
        return a[0] - b[0];
    }).each(function() {
        self.offsets.push(this[0] + scrollTop); // added + scrollTop
        self.targets.push(this[1]);
    });
},
Assignee
Assign to
Time tracking