govuk_frontend_toolkit 0.44.0 → 0.45.0

Sign up to get free protection for your applications and to get access to all the features.
data/app/assets/README.md CHANGED
@@ -698,6 +698,34 @@ Or to add it just to that list you could use:
698
698
  new GOVUK.PrimaryList($('#primary-list'), '.primary-item');
699
699
  ```
700
700
 
701
+ ## Stick at top when scrolling
702
+
703
+ `GOVUK.stickAtTopWhenScrolling` tries to add a class to an element when the top
704
+ of that element would be scrolled out of the viewport.
705
+
706
+ The following would cause the element to stay when you scroll:
707
+
708
+ ```html
709
+ <div class="js-stick-at-top-when-scrolling">something</div>
710
+ ```
711
+
712
+ ```css
713
+ .content-fixed {
714
+ position: fixed;
715
+ top: 0;
716
+ }
717
+ .shim {
718
+ display: block;
719
+ }
720
+ ```
721
+
722
+ ```javascript
723
+ GOVUK.stickAtTopWhenScrolling.init();
724
+ ```
725
+
726
+ If you also include the `stopScrollingAtFooter` JavaScript this will also try
727
+ and stop the elements before they get to the bottom.
728
+
701
729
 
702
730
  ## Licence
703
731
 
@@ -32,9 +32,11 @@
32
32
  },
33
33
  hideExtraLinks: function(){
34
34
  this.$extraLinks.addClass('visuallyhidden');
35
+ $(window).trigger('govuk.pageSizeChanged')
35
36
  },
36
37
  showExtraLinks: function(){
37
38
  this.$extraLinks.removeClass('visuallyhidden');
39
+ $(window).trigger('govuk.pageSizeChanged')
38
40
  }
39
41
  };
40
42
  GOVUK.PrimaryList = PrimaryList;
@@ -0,0 +1,76 @@
1
+ (function () {
2
+ "use strict"
3
+ var root = this,
4
+ $ = root.jQuery;
5
+ if(typeof root.GOVUK === 'undefined') { root.GOVUK = {}; }
6
+
7
+ // Stick elements to top of screen when you scroll past, documentation is in the README.md
8
+ var sticky = {
9
+ _hasScrolled: false,
10
+ _scrollTimeout: false,
11
+
12
+ init: function(){
13
+ var $els = $('.js-stick-at-top-when-scrolling');
14
+
15
+ if($els.length > 0){
16
+ sticky.$els = $els;
17
+
18
+ if(sticky._scrollTimeout === false) {
19
+ $(root).scroll(sticky.onScroll);
20
+ sticky._scrollTimeout = root.setInterval(sticky.checkScroll, 50);
21
+ }
22
+ $(root).resize(sticky.onResize);
23
+ }
24
+ if(root.GOVUK.stopScrollingAtFooter){
25
+ $els.each(function(i,el){
26
+ var $img = $(el).find('img');
27
+ if($img.length > 0){
28
+ var image = new Image();
29
+ image.onload = function(){
30
+ root.GOVUK.stopScrollingAtFooter.addEl($(el), $(el).outerHeight());
31
+ };
32
+ image.src = $img.attr('src');
33
+ } else {
34
+ root.GOVUK.stopScrollingAtFooter.addEl($(el), $(el).outerHeight());
35
+ }
36
+ });
37
+ }
38
+ },
39
+ onScroll: function(){
40
+ sticky._hasScrolled = true;
41
+ },
42
+ checkScroll: function(){
43
+ if(sticky._hasScrolled === true){
44
+ sticky._hasScrolled = false;
45
+
46
+ var windowVerticalPosition = $(root).scrollTop();
47
+ sticky.$els.each(function(i, el){
48
+ var $el = $(el),
49
+ scrolledFrom = $el.data('scrolled-from');
50
+
51
+ if (scrolledFrom && windowVerticalPosition < scrolledFrom){
52
+ sticky.release($el);
53
+ } else if($(root).width() > 768 && windowVerticalPosition >= $el.offset().top) {
54
+ sticky.stick($el);
55
+ }
56
+ });
57
+ }
58
+ },
59
+ stick: function($el){
60
+ if (!$el.hasClass('content-fixed')) {
61
+ $el.data('scrolled-from', $el.offset().top);
62
+ var height = Math.max($el.height(), 1);
63
+ $el.before('<div class="shim" style="width: '+ $el.width() + 'px; height: ' + height + 'px">&nbsp;</div>');
64
+ $el.css('width', $el.width() + "px").addClass('content-fixed');
65
+ }
66
+ },
67
+ release: function($el){
68
+ if($el.hasClass('content-fixed')){
69
+ $el.data('scrolled-from', false);
70
+ $el.removeClass('content-fixed').css('width', '');
71
+ $el.siblings('.shim').remove();
72
+ }
73
+ }
74
+ }
75
+ root.GOVUK.stickAtTopWhenScrolling = sticky;
76
+ }).call(this);
@@ -0,0 +1,39 @@
1
+ describe("stick-at-top-when-scrolling", function(){
2
+ var $stickyElement,
3
+ $stickyWrapper;
4
+
5
+ beforeEach(function(){
6
+ $stickyElement = $('<div class="stick-at-top-when-scrolling"></div>');
7
+ $stickyWrapper = $('<div>').append($stickyElement);
8
+
9
+ $('body').append($stickyWrapper);
10
+ });
11
+
12
+ afterEach(function(){
13
+ $stickyWrapper.remove();
14
+ });
15
+
16
+ it('should add fixed class on stick', function(){
17
+ expect(!$stickyElement.hasClass('content-fixed')).toBe(true);
18
+ GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
19
+ expect($stickyElement.hasClass('content-fixed')).toBe(true);
20
+ });
21
+
22
+ it('should remove fixed class on release', function(){
23
+ $stickyElement.addClass('content-fixed');
24
+ GOVUK.stickAtTopWhenScrolling.release($stickyElement);
25
+ expect(!$stickyElement.hasClass('content-fixed')).toBe(true);
26
+ });
27
+
28
+ it('should insert shim when sticking content', function(){
29
+ expect($('.shim').length).toBe(0);
30
+ GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
31
+ expect($('.shim').length).toBe(1);
32
+ });
33
+
34
+ it('should insert shim with minimum height', function(){
35
+ GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
36
+ expect($('.shim').height()).toBe(1);
37
+ });
38
+ });
39
+
@@ -1,3 +1,3 @@
1
1
  module GovUKFrontendToolkit
2
- VERSION = "0.44.0"
2
+ VERSION = "0.45.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_frontend_toolkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.44.0
4
+ version: 0.45.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-19 00:00:00.000000000 Z
12
+ date: 2014-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -204,6 +204,7 @@ files:
204
204
  - app/assets/images/player-icon-volume.png
205
205
  - app/assets/javascripts/govuk/multivariate-test.js
206
206
  - app/assets/javascripts/govuk/primary-links.js
207
+ - app/assets/javascripts/govuk/stick-at-top-when-scrolling.js
207
208
  - app/assets/javascripts/govuk/stop-scrolling-at-footer.js
208
209
  - app/assets/javascripts/govuk_toolkit.js
209
210
  - app/assets/javascripts/stageprompt.js
@@ -212,6 +213,7 @@ files:
212
213
  - app/assets/package.json
213
214
  - app/assets/spec/MultivariateTestSpec.js
214
215
  - app/assets/spec/PrimaryLinksSpec.js
216
+ - app/assets/spec/StickAtTopWhenScrollingSpec.js
215
217
  - app/assets/stylesheets/.gitkeep
216
218
  - app/assets/stylesheets/_colours.scss
217
219
  - app/assets/stylesheets/_conditionals.scss
@@ -240,7 +242,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
240
242
  version: '0'
241
243
  segments:
242
244
  - 0
243
- hash: 4141806898857661575
245
+ hash: 623435425171733917
244
246
  required_rubygems_version: !ruby/object:Gem::Requirement
245
247
  none: false
246
248
  requirements:
@@ -249,7 +251,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
251
  version: '0'
250
252
  segments:
251
253
  - 0
252
- hash: 4141806898857661575
254
+ hash: 623435425171733917
253
255
  requirements: []
254
256
  rubyforge_project:
255
257
  rubygems_version: 1.8.23