scrollspy.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. $(function () {
  2. 'use strict'
  3. QUnit.module('scrollspy plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).scrollspy, 'scrollspy method is defined')
  7. })
  8. QUnit.module('scrollspy', {
  9. beforeEach: function () {
  10. // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
  11. $.fn.bootstrapScrollspy = $.fn.scrollspy.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.scrollspy = $.fn.bootstrapScrollspy
  15. delete $.fn.bootstrapScrollspy
  16. $('#qunit-fixture').html('')
  17. }
  18. })
  19. QUnit.test('should provide no conflict', function (assert) {
  20. assert.expect(1)
  21. assert.strictEqual(typeof $.fn.scrollspy, 'undefined', 'scrollspy was set back to undefined (org value)')
  22. })
  23. QUnit.test('should throw explicit error on undefined method', function (assert) {
  24. assert.expect(1)
  25. var $el = $('<div/>').appendTo('#qunit-fixture')
  26. $el.bootstrapScrollspy()
  27. try {
  28. $el.bootstrapScrollspy('noMethod')
  29. } catch (err) {
  30. assert.strictEqual(err.message, 'No method named "noMethod"')
  31. }
  32. })
  33. QUnit.test('should return jquery collection containing the element', function (assert) {
  34. assert.expect(2)
  35. var $el = $('<div/>').appendTo('#qunit-fixture')
  36. var $scrollspy = $el.bootstrapScrollspy()
  37. assert.ok($scrollspy instanceof $, 'returns jquery collection')
  38. assert.strictEqual($scrollspy[0], $el[0], 'collection contains element')
  39. })
  40. QUnit.test('should only switch "active" class on current target', function (assert) {
  41. assert.expect(1)
  42. var done = assert.async()
  43. var sectionHTML = '<div id="root" class="active">' +
  44. '<div class="topbar">' +
  45. '<div class="topbar-inner">' +
  46. '<div class="container" id="ss-target">' +
  47. '<ul class="nav">' +
  48. '<li class="nav-item"><a href="#masthead">Overview</a></li>' +
  49. '<li class="nav-item"><a href="#detail">Detail</a></li>' +
  50. '</ul>' +
  51. '</div>' +
  52. '</div>' +
  53. '</div>' +
  54. '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
  55. '<div style="height: 200px;">' +
  56. '<h4 id="masthead">Overview</h4>' +
  57. '<p style="height: 200px">' +
  58. 'Ad leggings keytar, brunch id art party dolor labore.' +
  59. '</p>' +
  60. '</div>' +
  61. '<div style="height: 200px;">' +
  62. '<h4 id="detail">Detail</h4>' +
  63. '<p style="height: 200px">' +
  64. 'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
  65. '</p>' +
  66. '</div>' +
  67. '</div>' +
  68. '</div>'
  69. var $section = $(sectionHTML).appendTo('#qunit-fixture')
  70. var $scrollspy = $section
  71. .show()
  72. .find('#scrollspy-example')
  73. .bootstrapScrollspy({
  74. target: '#ss-target'
  75. })
  76. $scrollspy.one('scroll', function () {
  77. assert.ok($section.hasClass('active'), '"active" class still on root node')
  78. done()
  79. })
  80. $scrollspy.scrollTop(350)
  81. })
  82. QUnit.test('should only switch "active" class on current target specified w element', function (assert) {
  83. assert.expect(1)
  84. var done = assert.async()
  85. var sectionHTML = '<div id="root" class="active">' +
  86. '<div class="topbar">' +
  87. '<div class="topbar-inner">' +
  88. '<div class="container" id="ss-target">' +
  89. '<ul class="nav">' +
  90. '<li class="nav-item"><a href="#masthead">Overview</a></li>' +
  91. '<li class="nav-item"><a href="#detail">Detail</a></li>' +
  92. '</ul>' +
  93. '</div>' +
  94. '</div>' +
  95. '</div>' +
  96. '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
  97. '<div style="height: 200px;">' +
  98. '<h4 id="masthead">Overview</h4>' +
  99. '<p style="height: 200px">' +
  100. 'Ad leggings keytar, brunch id art party dolor labore.' +
  101. '</p>' +
  102. '</div>' +
  103. '<div style="height: 200px;">' +
  104. '<h4 id="detail">Detail</h4>' +
  105. '<p style="height: 200px">' +
  106. 'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
  107. '</p>' +
  108. '</div>' +
  109. '</div>' +
  110. '</div>'
  111. var $section = $(sectionHTML).appendTo('#qunit-fixture')
  112. var $scrollspy = $section
  113. .show()
  114. .find('#scrollspy-example')
  115. .bootstrapScrollspy({
  116. target: document.getElementById('#ss-target')
  117. })
  118. $scrollspy.one('scroll', function () {
  119. assert.ok($section.hasClass('active'), '"active" class still on root node')
  120. done()
  121. })
  122. $scrollspy.scrollTop(350)
  123. })
  124. QUnit.test('should correctly select middle navigation option when large offset is used', function (assert) {
  125. assert.expect(3)
  126. var done = assert.async()
  127. var sectionHTML = '<div id="header" style="height: 500px;"></div>' +
  128. '<nav id="navigation" class="navbar">' +
  129. '<ul class="navbar-nav">' +
  130. '<li class="nav-item active"><a class="nav-link" id="one-link" href="#one">One</a></li>' +
  131. '<li class="nav-item"><a class="nav-link" id="two-link" href="#two">Two</a></li>' +
  132. '<li class="nav-item"><a class="nav-link" id="three-link" href="#three">Three</a></li>' +
  133. '</ul>' +
  134. '</nav>' +
  135. '<div id="content" style="height: 200px; overflow-y: auto;">' +
  136. '<div id="one" style="height: 500px;"></div>' +
  137. '<div id="two" style="height: 300px;"></div>' +
  138. '<div id="three" style="height: 10px;"></div>' +
  139. '</div>'
  140. var $section = $(sectionHTML).appendTo('#qunit-fixture')
  141. var $scrollspy = $section
  142. .show()
  143. .filter('#content')
  144. $scrollspy.bootstrapScrollspy({
  145. target: '#navigation',
  146. offset: $scrollspy.position().top
  147. })
  148. $scrollspy.one('scroll', function () {
  149. assert.ok(!$section.find('#one-link').hasClass('active'), '"active" class removed from first section')
  150. assert.ok($section.find('#two-link').hasClass('active'), '"active" class on middle section')
  151. assert.ok(!$section.find('#three-link').hasClass('active'), '"active" class not on last section')
  152. done()
  153. })
  154. $scrollspy.scrollTop(550)
  155. })
  156. QUnit.test('should add the active class to the correct element', function (assert) {
  157. assert.expect(2)
  158. var navbarHtml =
  159. '<nav class="navbar">' +
  160. '<ul class="nav">' +
  161. '<li class="nav-item"><a class="nav-link" id="a-1" href="#div-1">div 1</a></li>' +
  162. '<li class="nav-item"><a class="nav-link" id="a-2" href="#div-2">div 2</a></li>' +
  163. '</ul>' +
  164. '</nav>'
  165. var contentHtml =
  166. '<div class="content" style="overflow: auto; height: 50px">' +
  167. '<div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>' +
  168. '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
  169. '</div>'
  170. $(navbarHtml).appendTo('#qunit-fixture')
  171. var $content = $(contentHtml)
  172. .appendTo('#qunit-fixture')
  173. .bootstrapScrollspy({
  174. offset: 0,
  175. target: '.navbar'
  176. })
  177. var done = assert.async()
  178. var testElementIsActiveAfterScroll = function (element, target) {
  179. var deferred = $.Deferred()
  180. var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top)
  181. $content.one('scroll', function () {
  182. assert.ok($(element).hasClass('active'), 'target:' + target + ', element' + element)
  183. deferred.resolve()
  184. })
  185. $content.scrollTop(scrollHeight)
  186. return deferred.promise()
  187. }
  188. $.when(testElementIsActiveAfterScroll('#a-1', '#div-1'))
  189. .then(function () {
  190. return testElementIsActiveAfterScroll('#a-2', '#div-2')
  191. })
  192. .then(function () {
  193. done()
  194. })
  195. })
  196. QUnit.test('should add the active class to the correct element (nav markup)', function (assert) {
  197. assert.expect(2)
  198. var navbarHtml =
  199. '<nav class="navbar">' +
  200. '<nav class="nav">' +
  201. '<a class="nav-link" id="a-1" href="#div-1">div 1</a>' +
  202. '<a class="nav-link" id="a-2" href="#div-2">div 2</a>' +
  203. '</nav>' +
  204. '</nav>'
  205. var contentHtml =
  206. '<div class="content" style="overflow: auto; height: 50px">' +
  207. '<div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>' +
  208. '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
  209. '</div>'
  210. $(navbarHtml).appendTo('#qunit-fixture')
  211. var $content = $(contentHtml)
  212. .appendTo('#qunit-fixture')
  213. .bootstrapScrollspy({
  214. offset: 0,
  215. target: '.navbar'
  216. })
  217. var done = assert.async()
  218. var testElementIsActiveAfterScroll = function (element, target) {
  219. var deferred = $.Deferred()
  220. var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top)
  221. $content.one('scroll', function () {
  222. assert.ok($(element).hasClass('active'), 'target:' + target + ', element' + element)
  223. deferred.resolve()
  224. })
  225. $content.scrollTop(scrollHeight)
  226. return deferred.promise()
  227. }
  228. $.when(testElementIsActiveAfterScroll('#a-1', '#div-1'))
  229. .then(function () {
  230. return testElementIsActiveAfterScroll('#a-2', '#div-2')
  231. })
  232. .then(function () {
  233. done()
  234. })
  235. })
  236. QUnit.test('should add the active class to the correct element (list-group markup)', function (assert) {
  237. assert.expect(2)
  238. var navbarHtml =
  239. '<nav class="navbar">' +
  240. '<div class="list-group">' +
  241. '<a class="list-group-item" id="a-1" href="#div-1">div 1</a>' +
  242. '<a class="list-group-item" id="a-2" href="#div-2">div 2</a>' +
  243. '</div>' +
  244. '</nav>'
  245. var contentHtml =
  246. '<div class="content" style="overflow: auto; height: 50px">' +
  247. '<div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>' +
  248. '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
  249. '</div>'
  250. $(navbarHtml).appendTo('#qunit-fixture')
  251. var $content = $(contentHtml)
  252. .appendTo('#qunit-fixture')
  253. .bootstrapScrollspy({
  254. offset: 0,
  255. target: '.navbar'
  256. })
  257. var done = assert.async()
  258. var testElementIsActiveAfterScroll = function (element, target) {
  259. var deferred = $.Deferred()
  260. var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top)
  261. $content.one('scroll', function () {
  262. assert.ok($(element).hasClass('active'), 'target:' + target + ', element' + element)
  263. deferred.resolve()
  264. })
  265. $content.scrollTop(scrollHeight)
  266. return deferred.promise()
  267. }
  268. $.when(testElementIsActiveAfterScroll('#a-1', '#div-1'))
  269. .then(function () {
  270. return testElementIsActiveAfterScroll('#a-2', '#div-2')
  271. })
  272. .then(function () {
  273. done()
  274. })
  275. })
  276. QUnit.test('should add the active class correctly when there are nested elements at 0 scroll offset', function (assert) {
  277. assert.expect(6)
  278. var times = 0
  279. var done = assert.async()
  280. var navbarHtml = '<nav id="navigation" class="navbar">' +
  281. '<ul class="nav">' +
  282. '<li class="nav-item"><a id="a-1" class="nav-link" href="#div-1">div 1</a>' +
  283. '<ul class="nav">' +
  284. '<li class="nav-item"><a id="a-2" class="nav-link" href="#div-2">div 2</a></li>' +
  285. '</ul>' +
  286. '</li>' +
  287. '</ul>' +
  288. '</nav>'
  289. var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
  290. '<div id="div-1" style="padding: 0; margin: 0">' +
  291. '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
  292. '</div>' +
  293. '</div>'
  294. $(navbarHtml).appendTo('#qunit-fixture')
  295. var $content = $(contentHtml)
  296. .appendTo('#qunit-fixture')
  297. .bootstrapScrollspy({
  298. offset: 0,
  299. target: '#navigation'
  300. })
  301. function testActiveElements() {
  302. if (++times > 3) {
  303. return done()
  304. }
  305. $content.one('scroll', function () {
  306. assert.ok($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
  307. assert.ok($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
  308. testActiveElements()
  309. })
  310. $content.scrollTop($content.scrollTop() + 10)
  311. }
  312. testActiveElements()
  313. })
  314. QUnit.test('should add the active class correctly when there are nested elements (nav markup)', function (assert) {
  315. assert.expect(6)
  316. var times = 0
  317. var done = assert.async()
  318. var navbarHtml = '<nav id="navigation" class="navbar">' +
  319. '<nav class="nav">' +
  320. '<a id="a-1" class="nav-link" href="#div-1">div 1</a>' +
  321. '<nav class="nav">' +
  322. '<a id="a-2" class="nav-link" href="#div-2">div 2</a>' +
  323. '</nav>' +
  324. '</nav>' +
  325. '</nav>'
  326. var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
  327. '<div id="div-1" style="padding: 0; margin: 0">' +
  328. '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
  329. '</div>' +
  330. '</div>'
  331. $(navbarHtml).appendTo('#qunit-fixture')
  332. var $content = $(contentHtml)
  333. .appendTo('#qunit-fixture')
  334. .bootstrapScrollspy({
  335. offset: 0,
  336. target: '#navigation'
  337. })
  338. function testActiveElements() {
  339. if (++times > 3) {
  340. return done()
  341. }
  342. $content.one('scroll', function () {
  343. assert.ok($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
  344. assert.ok($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
  345. testActiveElements()
  346. })
  347. $content.scrollTop($content.scrollTop() + 10)
  348. }
  349. testActiveElements()
  350. })
  351. QUnit.test('should add the active class correctly when there are nested elements (nav nav-item markup)', function (assert) {
  352. assert.expect(6)
  353. var times = 0
  354. var done = assert.async()
  355. var navbarHtml = '<nav id="navigation" class="navbar">' +
  356. '<ul class="nav">' +
  357. '<li class="nav-item"><a id="a-1" class="nav-link" href="#div-1">div 1</a></li>' +
  358. '<ul class="nav">' +
  359. '<li class="nav-item"><a id="a-2" class="nav-link" href="#div-2">div 2</a></li>' +
  360. '</ul>' +
  361. '</ul>' +
  362. '</nav>'
  363. var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
  364. '<div id="div-1" style="padding: 0; margin: 0">' +
  365. '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
  366. '</div>' +
  367. '</div>'
  368. $(navbarHtml).appendTo('#qunit-fixture')
  369. var $content = $(contentHtml)
  370. .appendTo('#qunit-fixture')
  371. .bootstrapScrollspy({
  372. offset: 0,
  373. target: '#navigation'
  374. })
  375. function testActiveElements() {
  376. if (++times > 3) {
  377. return done()
  378. }
  379. $content.one('scroll', function () {
  380. assert.ok($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
  381. assert.ok($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
  382. testActiveElements()
  383. })
  384. $content.scrollTop($content.scrollTop() + 10)
  385. }
  386. testActiveElements()
  387. })
  388. QUnit.test('should add the active class correctly when there are nested elements (list-group markup)', function (assert) {
  389. assert.expect(6)
  390. var times = 0
  391. var done = assert.async()
  392. var navbarHtml = '<nav id="navigation" class="navbar">' +
  393. '<div class="list-group">' +
  394. '<a id="a-1" class="list-group-item" href="#div-1">div 1</a>' +
  395. '<div class="list-group">' +
  396. '<a id="a-2" class="list-group-item" href="#div-2">div 2</a>' +
  397. '</div>' +
  398. '</div>' +
  399. '</nav>'
  400. var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
  401. '<div id="div-1" style="padding: 0; margin: 0">' +
  402. '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
  403. '</div>' +
  404. '</div>'
  405. $(navbarHtml).appendTo('#qunit-fixture')
  406. var $content = $(contentHtml)
  407. .appendTo('#qunit-fixture')
  408. .bootstrapScrollspy({
  409. offset: 0,
  410. target: '#navigation'
  411. })
  412. function testActiveElements() {
  413. if (++times > 3) {
  414. return done()
  415. }
  416. $content.one('scroll', function () {
  417. assert.ok($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
  418. assert.ok($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
  419. testActiveElements()
  420. })
  421. $content.scrollTop($content.scrollTop() + 10)
  422. }
  423. testActiveElements()
  424. })
  425. QUnit.test('should clear selection if above the first section', function (assert) {
  426. assert.expect(3)
  427. var done = assert.async()
  428. var sectionHTML = '<div id="header" style="height: 500px;"></div>' +
  429. '<nav id="navigation" class="navbar">' +
  430. '<ul class="navbar-nav">' +
  431. '<li class="nav-item"><a id="one-link" class="nav-link active" href="#one">One</a></li>' +
  432. '<li class="nav-item"><a id="two-link" class="nav-link" href="#two">Two</a></li>' +
  433. '<li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>' +
  434. '</ul>' +
  435. '</nav>'
  436. $(sectionHTML).appendTo('#qunit-fixture')
  437. var scrollspyHTML = '<div id="content" style="height: 200px; overflow-y: auto;">' +
  438. '<div id="spacer" style="height: 100px;"/>' +
  439. '<div id="one" style="height: 100px;"/>' +
  440. '<div id="two" style="height: 100px;"/>' +
  441. '<div id="three" style="height: 100px;"/>' +
  442. '<div id="spacer" style="height: 100px;"/>' +
  443. '</div>'
  444. var $scrollspy = $(scrollspyHTML).appendTo('#qunit-fixture')
  445. $scrollspy
  446. .bootstrapScrollspy({
  447. target: '#navigation',
  448. offset: $scrollspy.position().top
  449. })
  450. .one('scroll', function () {
  451. assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
  452. assert.strictEqual($('.active').is('#two-link'), true, '"active" class on second section')
  453. $scrollspy
  454. .one('scroll', function () {
  455. assert.strictEqual($('.active').length, 0, 'selection cleared')
  456. done()
  457. })
  458. .scrollTop(0)
  459. })
  460. .scrollTop(201)
  461. })
  462. QUnit.test('should NOT clear selection if above the first section and first section is at the top', function (assert) {
  463. assert.expect(4)
  464. var done = assert.async()
  465. var sectionHTML = '<div id="header" style="height: 500px;"></div>' +
  466. '<nav id="navigation" class="navbar">' +
  467. '<ul class="navbar-nav">' +
  468. '<li class="nav-item"><a id="one-link" class="nav-link active" href="#one">One</a></li>' +
  469. '<li class="nav-item"><a id="two-link" class="nav-link" href="#two">Two</a></li>' +
  470. '<li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>' +
  471. '</ul>' +
  472. '</nav>'
  473. $(sectionHTML).appendTo('#qunit-fixture')
  474. var negativeHeight = -10
  475. var startOfSectionTwo = 101
  476. var scrollspyHTML = '<div id="content" style="height: 200px; overflow-y: auto;">' +
  477. '<div id="one" style="height: 100px;"/>' +
  478. '<div id="two" style="height: 100px;"/>' +
  479. '<div id="three" style="height: 100px;"/>' +
  480. '<div id="spacer" style="height: 100px;"/>' +
  481. '</div>'
  482. var $scrollspy = $(scrollspyHTML).appendTo('#qunit-fixture')
  483. $scrollspy
  484. .bootstrapScrollspy({
  485. target: '#navigation',
  486. offset: $scrollspy.position().top
  487. })
  488. .one('scroll', function () {
  489. assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
  490. assert.strictEqual($('.active').is('#two-link'), true, '"active" class on second section')
  491. $scrollspy
  492. .one('scroll', function () {
  493. assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
  494. assert.strictEqual($('.active').is('#one-link'), true, '"active" class on first section')
  495. done()
  496. })
  497. .scrollTop(negativeHeight)
  498. })
  499. .scrollTop(startOfSectionTwo)
  500. })
  501. QUnit.test('should correctly select navigation element on backward scrolling when each target section height is 100%', function (assert) {
  502. assert.expect(5)
  503. var navbarHtml =
  504. '<nav class="navbar">' +
  505. '<ul class="nav">' +
  506. '<li class="nav-item"><a id="li-100-1" class="nav-link" href="#div-100-1">div 1</a></li>' +
  507. '<li class="nav-item"><a id="li-100-2" class="nav-link" href="#div-100-2">div 2</a></li>' +
  508. '<li class="nav-item"><a id="li-100-3" class="nav-link" href="#div-100-3">div 3</a></li>' +
  509. '<li class="nav-item"><a id="li-100-4" class="nav-link" href="#div-100-4">div 4</a></li>' +
  510. '<li class="nav-item"><a id="li-100-5" class="nav-link" href="#div-100-5">div 5</a></li>' +
  511. '</ul>' +
  512. '</nav>'
  513. var contentHtml =
  514. '<div class="content" style="position: relative; overflow: auto; height: 100px">' +
  515. '<div id="div-100-1" style="position: relative; height: 100%; padding: 0; margin: 0">div 1</div>' +
  516. '<div id="div-100-2" style="position: relative; height: 100%; padding: 0; margin: 0">div 2</div>' +
  517. '<div id="div-100-3" style="position: relative; height: 100%; padding: 0; margin: 0">div 3</div>' +
  518. '<div id="div-100-4" style="position: relative; height: 100%; padding: 0; margin: 0">div 4</div>' +
  519. '<div id="div-100-5" style="position: relative; height: 100%; padding: 0; margin: 0">div 5</div>' +
  520. '</div>'
  521. $(navbarHtml).appendTo('#qunit-fixture')
  522. var $content = $(contentHtml)
  523. .appendTo('#qunit-fixture')
  524. .bootstrapScrollspy({
  525. offset: 0,
  526. target: '.navbar'
  527. })
  528. var testElementIsActiveAfterScroll = function (element, target) {
  529. var deferred = $.Deferred()
  530. var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top)
  531. $content.one('scroll', function () {
  532. assert.ok($(element).hasClass('active'), 'target:' + target + ', element: ' + element)
  533. deferred.resolve()
  534. })
  535. $content.scrollTop(scrollHeight)
  536. return deferred.promise()
  537. }
  538. var done = assert.async()
  539. $.when(testElementIsActiveAfterScroll('#li-100-5', '#div-100-5'))
  540. .then(function () {
  541. return testElementIsActiveAfterScroll('#li-100-4', '#div-100-4')
  542. })
  543. .then(function () {
  544. return testElementIsActiveAfterScroll('#li-100-3', '#div-100-3')
  545. })
  546. .then(function () {
  547. return testElementIsActiveAfterScroll('#li-100-2', '#div-100-2')
  548. })
  549. .then(function () {
  550. return testElementIsActiveAfterScroll('#li-100-1', '#div-100-1')
  551. })
  552. .then(function () {
  553. done()
  554. })
  555. })
  556. QUnit.test('should allow passed in option offset method: offset', function (assert) {
  557. assert.expect(4)
  558. var testOffsetMethod = function (type) {
  559. var $navbar = $(
  560. '<nav class="navbar"' + (type === 'data' ? ' id="navbar-offset-method-menu"' : '') + '>' +
  561. '<ul class="nav">' +
  562. '<li class="nav-item"><a id="li-' + type + 'm-1" class="nav-link" href="#div-' + type + 'm-1">div 1</a></li>' +
  563. '<li class="nav-item"><a id="li-' + type + 'm-2" class="nav-link" href="#div-' + type + 'm-2">div 2</a></li>' +
  564. '<li class="nav-item"><a id="li-' + type + 'm-3" class="nav-link" href="#div-' + type + 'm-3">div 3</a></li>' +
  565. '</ul>' +
  566. '</nav>'
  567. )
  568. var $content = $(
  569. '<div class="content"' + (type === 'data' ? ' data-spy="scroll" data-target="#navbar-offset-method-menu" data-offset="0" data-method="offset"' : '') + ' style="position: relative; overflow: auto; height: 100px">' +
  570. '<div id="div-' + type + 'm-1" style="position: relative; height: 200px; padding: 0; margin: 0">div 1</div>' +
  571. '<div id="div-' + type + 'm-2" style="position: relative; height: 150px; padding: 0; margin: 0">div 2</div>' +
  572. '<div id="div-' + type + 'm-3" style="position: relative; height: 250px; padding: 0; margin: 0">div 3</div>' +
  573. '</div>'
  574. )
  575. $navbar.appendTo('#qunit-fixture')
  576. $content.appendTo('#qunit-fixture')
  577. if (type === 'js') {
  578. $content.bootstrapScrollspy({
  579. target: '.navbar',
  580. offset: 0,
  581. method: 'offset'
  582. })
  583. } else if (type === 'data') {
  584. $(window).trigger('load')
  585. }
  586. var $target = $('#div-' + type + 'm-2')
  587. var scrollspy = $content.data('bs.scrollspy')
  588. assert.ok(scrollspy._offsets[1] === $target.offset().top, 'offset method with ' + type + ' option')
  589. assert.ok(scrollspy._offsets[1] !== $target.position().top, 'position method with ' + type + ' option')
  590. $navbar.remove()
  591. $content.remove()
  592. }
  593. testOffsetMethod('js')
  594. testOffsetMethod('data')
  595. })
  596. QUnit.test('should allow passed in option offset method: position', function (assert) {
  597. assert.expect(4)
  598. var testOffsetMethod = function (type) {
  599. var $navbar = $(
  600. '<nav class="navbar"' + (type === 'data' ? ' id="navbar-offset-method-menu"' : '') + '>' +
  601. '<ul class="nav">' +
  602. '<li class="nav-item"><a class="nav-link" id="li-' + type + 'm-1" href="#div-' + type + 'm-1">div 1</a></li>' +
  603. '<li class="nav-item"><a class="nav-link" id="li-' + type + 'm-2" href="#div-' + type + 'm-2">div 2</a></li>' +
  604. '<li class="nav-item"><a class="nav-link" id="li-' + type + 'm-3" href="#div-' + type + 'm-3">div 3</a></li>' +
  605. '</ul>' +
  606. '</nav>'
  607. )
  608. var $content = $(
  609. '<div class="content"' + (type === 'data' ? ' data-spy="scroll" data-target="#navbar-offset-method-menu" data-offset="0" data-method="position"' : '') + ' style="position: relative; overflow: auto; height: 100px">' +
  610. '<div id="div-' + type + 'm-1" style="position: relative; height: 200px; padding: 0; margin: 0">div 1</div>' +
  611. '<div id="div-' + type + 'm-2" style="position: relative; height: 150px; padding: 0; margin: 0">div 2</div>' +
  612. '<div id="div-' + type + 'm-3" style="position: relative; height: 250px; padding: 0; margin: 0">div 3</div>' +
  613. '</div>'
  614. )
  615. $navbar.appendTo('#qunit-fixture')
  616. $content.appendTo('#qunit-fixture')
  617. if (type === 'js') {
  618. $content.bootstrapScrollspy({
  619. target: '.navbar',
  620. offset: 0,
  621. method: 'position'
  622. })
  623. } else if (type === 'data') {
  624. $(window).trigger('load')
  625. }
  626. var $target = $('#div-' + type + 'm-2')
  627. var scrollspy = $content.data('bs.scrollspy')
  628. assert.ok(scrollspy._offsets[1] !== $target.offset().top, 'offset method with ' + type + ' option')
  629. assert.ok(scrollspy._offsets[1] === $target.position().top, 'position method with ' + type + ' option')
  630. $navbar.remove()
  631. $content.remove()
  632. }
  633. testOffsetMethod('js')
  634. testOffsetMethod('data')
  635. })
  636. })