bootstrap源码之滚动监听组件scrollspy.js详解

前端开发 作者: 2024-08-20 16:25:01
其实滚动监听使用的情况还是很多的,比如导航居于右侧,当主题内容滚动某一块的时候,右侧导航对应的要高亮。 实现功能 1、当滚动区域内设置的hashkey距离顶点到有效位置时,就关联设置其导航上的指定项
<div id="selector" class="navbar navbar-default">  
ul class="nav navbar-nav"li><a href="#one">one</a> ="#two">two="#three">threeul>
divdata-spy="scroll" data-target="#selector" style="height:100px; overflow:hidden;overflow-y: auto;" h4 ="one" >ibeh4p>One的具体内容br/>One的具体内容/></="two" ="three" >
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))
    .refresh()
    .process()
  }
this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight,document.documentElement.scrollHeight)
ScrollSpy.prototype.refresh =  () {
    var that          = this
    var offsetMethod  = 'offset'
    var offsetBase    = 0

    this.offsets      =this.targets      =this.scrollHeight = .getScrollHeight()

    if (!$.isWindow(this.$scrollElement[0])) {
      offsetMethod = 'position'
      offsetBase   = .$scrollElement.scrollTop()
    }

    .$body
      .find(.selector)
      .map( () {
        var $el   = $()
        var href  = $el.data('target') || $el.attr('href'var $href = /^#./.test(href) && $(href)
        
        return ($href
          && $href.length
          && $href.is(':visible')
          && [[$href[offsetMethod]().top + offsetBase,href]]) || null
      })
      .sort(function (a,b) { return a[0] - b[0] })
      .each( () {
        that.offsets.push(this[0])
        that.targets.push(this[1])
      })

  }
.$scrollElement.scrollTop()
    }
])
      })
ScrollSpy.prototype.process = var scrollTop    = this.$scrollElement.scrollTop() + .options.offset
    var scrollHeight = .getScrollHeight()
    var maxScroll    = this.options.offset + scrollHeight - .$scrollElement.height()
    var offsets      = .offsets
    var targets      = .targets
    var activeTarget = .activeTarget
    var i

    if (this.scrollHeight != scrollHeight) {
      .refresh()
    }

    if (scrollTop >= maxScroll) {
      return activeTarget != (i = targets[targets.length - 1]) && .activate(i)
    }

    if (activeTarget && scrollTop < offsets[0]) {
      this.activeTarget = null
      return .clear()
    }

    for (i = offsets.length; i--;) {
      activeTarget != targets[i]
        && scrollTop >= offsets[i]
        && (offsets[i + 1] === undefined || scrollTop < offsets[i + 1])
        && .activate(targets[i])
    }
  }
this.options.offset
this.$scrollElement.height()
.activate(targets[i])
    }
ScrollSpy.prototype.activate =  (target) {
    this.activeTarget = target

    .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')
  }
ScrollSpy.prototype.clear =  () {
    $(.selector)
      .parentsUntil(this.options.target,'.active')
      .removeClass('active')
  }
+ ($) {
  'use strict';

  // SCROLLSPY CLASS DEFINITION
   ==========================

  .process()
  }

  ScrollSpy.VERSION  = '3.3.7'

  ScrollSpy.DEFAULTS = {
    offset: 10
  }

  ScrollSpy.prototype.getScrollHeight = this.$body[0].scrollHeight,document.documentElement.scrollHeight)
  }

  ScrollSpy.prototype.refresh = ])
      })

  }

  ScrollSpy.prototype.process = .activate(targets[i])
    }
  }

  ScrollSpy.prototype.activate = )
  }

  ScrollSpy.prototype.clear = )
  }


   SCROLLSPY PLUGIN DEFINITION
   ===========================

   Plugin(option) {
    this.each( () {
      var $this   = $()
      var data    = $this.data('bs.scrollspy'var options = typeof option == 'object' && option

      if (!data) $this.data('bs.scrollspy',(data = new ScrollSpy(,options)))
      typeof option == 'string') data[option]()
    })
  }

  var old = $.fn.scrollspy

  $.fn.scrollspy             = Plugin
  $.fn.scrollspy.Constructor = ScrollSpy


   SCROLLSPY NO CONFLICT
   =====================

  $.fn.scrollspy.noConflict =  () {
    $.fn.scrollspy = old
    
  }


   SCROLLSPY DATA-API
   ==================

  $(window).on('load.bs.scrollspy.data-api',1)"> () {
    $('[data-spy="scroll"]').each(var $spy = $()
      Plugin.call($spy,$spy.data())
    })
  })

}(jQuery);
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65608.html