FreewayTalk

11 replies to this thread. Most Recent

grantsymon

3 Mar 2019, 12:08 pm

[Pro] Shrink fixed header on scroll

I’ve been trying (and failing) to add some code to make a fixed header part shrink on scroll. (I haven’t been able to find an action that can do this either. Is there one?).

What I want to achieve ideally, is for the fixed header to shrink and so consequently, for the floating elements it contains to shrink and realign from vertical to horizontal. 2 of these contain text, which would need to change point size too.

I found this code, which achieves the shrink and text size parts, but I can’t get it to actually do anything. I expect I’m putting it in the wrong place. :( (https://www.w3schools.com/howto/howto_js_shrink_header_scroll.asp)

Grant

waltd

3 Mar 2019, 3:23 pm

Show me a page that contains the full-size element you want to shrink, and I’ll help you out. This example code is very straightforward, and does not rely on any libraries like jQuery or Prototype, which means it will be compatible with anything.

The way that they have done this is to make the size of the header depend entirely on the font size, that way they can change one element and the whole layout shifts to accommodate that. In Freeway, you’d have to ensure that you didn’t allow the height of your header to be fixed, so you’d set the height to Flexible, and make sure that the content had its padding and margin set in ems. That way the font size will govern the size of the whitespace, too.

This is an artificially contrived example, and I’m sure you’ll want to have more functional content in yours, so let’s see what you’d really like to start with, and I’ll have a think about how to get the layout to flex, based on that.

Walter

On Mar 3, 2019, at 7:08 AM, grantsymon <[email protected]> wrote:

I’ve been trying (and failing) to add some code to make a fixed header part shrink on scroll. (I haven’t been able to find an action that can do this either. Is there one?).

What I want to achieve ideally, is for the fixed header to shrink and so consequently, for the floating elements it contains to shrink and realign from vertical to horizontal. 2 of these contain text, which would need to change point size too.

I found this code, which achieves the shrink and text size parts, but I can’t get it to actually do anything. I expect I’m putting it in the wrong place. :( (https://www.w3schools.com/howto/howto_js_shrink_header_scroll.asp)

Freeway user since 1997

http://www.walterdavisstudio.com

grantsymon

4 Mar 2019, 1:34 pm

Thanks Walter!

If FW was still happily chugging along, I think that this functionality would be a great subject for a FWAction. Actually … if I was designing web-software, I’d build this in.

People are now accessing the web, more and more via phones and tablets and a scrolling single-page is fast becoming the de facto. Many news sites are already single page. It’s easy to understand why and the argument for compelling content becomes increasingly stronger. If you can keep them scrolling, because each flick of the finger shows them something worth seeing, then you have solved the problem of getting them to click on a button offering unknown results.

My next update to my site will most likely be scrolling, but most probably with some sort of ‘touch to expand’ button that will extent specific content downwards. However, with limited real-estate, especially on phones, the need to recover space from a generous landing layout, becomes a primary objective. Hence the desire for a shrinking header. :)

And … since you ask … :) this is what I think would be really useful in many many cases. However, I can see that it might be quite challenging. I have absolutely no idea how you could even go about doing this, but FWIW, I’ve uploaded a 2 page demo, which shows what I think would be fab. The full header on landing, shrinking to a re-arranged narrow header on scroll.

The grey blob is a button:

http://test-2.grantsymon.com/shrinkheader-1.html

Grant

grantsymon

4 Mar 2019, 1:38 pm

With great irony …

I should add that I didn’t make necessary viewport changes to the demo-page, for it work on mobiles!! So view on a desktop. :)

Grant

grantsymon

8 Mar 2019, 1:22 pm

With staggering simplicity, I’ve just managed to stumble across a method to achieve what I want. I took quite a lot of hours of googling before I realised, chewing on a piece of bread and watching the birds out the window, that all I had to do was use layers to hide an element. Sigh … You’d think that a photographer who’s been thinking in layers since his days in the darkroom, 40 years ago, would have thought of it sooner.

However!

There is a slight hiccup, in that because browsers have a sort of ‘bounce’ these days, where you can drag the content further down than the top and it ‘bounces back up’ when you let go … I can see the hidden content behind, when this happens. So I suppose this brings me to wondering if I can hide that element (the narrow header) until the page has scrolled a certain distance?

Updated to show the new smoke and mirrors: http://test-2.grantsymon.com/shrinkheader-1.html

Grant

waltd

8 Mar 2019, 2:02 pm

There are ways to do this showing and hiding in JavaScript, but not that I know of in CSS.

// with Prototype.js in the page
var big_header = $('id_of_your_big_header');
var small_header = $('id_of_your_small_header');

Event.observe(window, 'scroll', function(){
  if(document.viewport.getScrollOffsets()['top'] >= big_header.getHeight()){
    big_header.hide();
    small_header.show();
  }else{
    small_header.hide();
    big_header.show();
  }
});

That code would go either in an observer wrapper, such as the ones written by Protaculous 2, or in a <script> block at the bottom of the page (using the Before </body> block in Page / HTML Markup). That ensures that the elements you want to modify are “there” from the JavaScript perspective.

Another way that you could do this using only CSS would be to put the small header underneath (in layer order) the large header, and set the small header’s position to fixed or sticky, and the large header to absolute. That way the large header would scroll away with the rest of the page, leaving the sticky header behind. But that may not fix the snap-back issue you’re describing. It’s also going to be a lot of fiddly work in a responsive layout, since you’re dealing with layers that don’t repel the other content of the page.

Walter

On Mar 8, 2019, at 8:22 AM, grantsymon <[email protected]> wrote:

With staggering simplicity, I’ve just managed to stumble across a method to achieve what I want. I took quite a lot of hours of googling before I realised, chewing on a piece of bread and watching the birds out the window, that all I had to do was use layers to hide an element. Sigh … You’d think that a photographer who’s been thinking in layers since his days in the darkroom, 40 years ago, would have thought of it sooner.

However!

There is a slight hiccup, in that because browsers have a sort of ‘bounce’ these days, where you can drag the content further down than the top and it ‘bounces back up’ when you let go … I can see the hidden content behind, when this happens. So I suppose this brings me to wondering if I can hide that element (the narrow header) until the page has scrolled a certain distance?

Updated to show the new smoke and mirrors: http://test-2.grantsymon.com/shrinkheader-1.html

Freeway user since 1997

http://www.walterdavisstudio.com

grantsymon

8 Mar 2019, 2:57 pm

On 3 Mar 2019, 3:23 pm, waltd wrote:

Another way that you could do this using only CSS would be to put the small header underneath (in layer order) the large header, and set the small header’s position to fixed or sticky, and the large header to absolute. That way the large header would scroll away with the rest of the page, leaving the sticky header behind. But that may not fix the snap-back issue you’re describing. It’s also going to be a lot of fiddly work in a responsive layout, since you’re dealing with layers that don’t repel the other content of the page.

This is exactly what I’ve done for the test-2 page I just posted. It seems to work fine for responsive, although I haven’t fine tuned it for the smallest viewports.

I’ve also thought of another way of solving the ‘snap-back’ problem (when you don’t know how to code, you just use a hammer) which is by having a 3rd layer - absolute, which extends off the page at the top. I’d appreciate if you could take a look Walter and tell me if you think it will break with some browsers … it works ok in Safari on my MBPro, iPad Pro 13 and iPhone 6.

I do get an issue where if I ‘accidentally move’ the page with 2 fingers instead of scrolling, the page content moves around sideways and I catch glimpses of the small header. Not sure what to do about this, or if it’s really a problem.

Grant

grantsymon

8 Mar 2019, 2:58 pm

Forgot to say that the page is updated with the element extending off the top. http://test-2.grantsymon.com/shrinkheader-1.html

Grant

waltd

8 Mar 2019, 3:50 pm

This appears to be seamless to me. Nice job!

Walter

On Mar 8, 2019, at 9:58 AM, grantsymon <[email protected]> wrote:

Forgot to say that the page is updated with the element extending off the top. http://test-2.grantsymon.com/shrinkheader-1.html

Freeway user since 1997

http://www.walterdavisstudio.com

grantsymon

9 Mar 2019, 9:33 am

Thanks Walter. :)

In the interests of understanding, I’ve also tried doing the same with the snippet of code you provided, but I can’t get it to work. The element doesn’t hide. Any help would be much appreciated:

http://test-2.grantsymon.com/2/hideheader.html

Grant

waltd

9 Mar 2019, 2:31 pm

You’re missing Prototype.js in the head of the page. Just apply Protaculous 2 to the page and publish again, and this should work. Prototype will be added to the page from the Google Ajax repository automatically.

Walter

On Mar 9, 2019, at 4:33 AM, grantsymon <[email protected]> wrote:

Thanks Walter. :)

In the interests of understanding, I’ve also tried doing the same with the snippet of code you provided, but I can’t get it to work. The element doesn’t hide. Any help would be much appreciated:

http://test-2.grantsymon.com/2/hideheader.html

Freeway user since 1997

http://www.walterdavisstudio.com

Back to Top

Claas Von rabenau

23 May 2019, 9:02 am

Hello grantsymon, would you mind posting the freeway file? Claas