Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring an element to top of the page even if the page does not scroll

Background:

Let's say you have a simple page which has a logo and a heading only and one paragraph

<img src="http://blog.stackoverflow.com/wp-content/uploads/StackExchangeLogo1.png">
<h1>Foo Bar</h1>
<p>ABC12345</p>

This is how that looks like

enter image description here

That page, obviously would not have vertical overflow / scroll bar for almost even tiny scale mobile devices, let alone computers.

Question

How can you bring that heading to the top left of the screen and move the logo out of focus unless someone scrolls up? Open to using any JavaScript library and any CSS framework

Attempts:

  1. Tried using anchors but they only work if the page already had a scroll bar and anchor was out of focus.
  2. Tried window.scrollTo but that also requires the page to have scroll already
  3. Tried $("html, body").animate({ scrollTop: 90}, 100); but that also doesn't work when the page doesn't have overflow

Expected outcome:

Notes:

Please note that adding some extra <br/> to induce an overflow is not the way to go, it can be done that way but that's a very ordinary workaround

Why is it needed?

Its for a form for mobile devices, simple requirement is to take the first field of the form to top of the page and hide the logo (one can scroll up if they wish to see it) so it doesn't take attention away. Not using jQueryMobile for this particular task.

like image 912
Hanky Panky Avatar asked Apr 21 '14 11:04

Hanky Panky


People also ask

How do I get to the top of the page without scrolling?

Try using Command key + arrow up (or down). The reason those work is because adding the Fn key turns those keys into Home, End, Page Up, and Page Down. On extended keyboards, you can get to top of page by pressing just the Home key, and the bottom of a page by pressing End.

How do I jump to the top of the page in HTML?

window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.

How do I make my element not scroll on pages?

As for preventing scrolling on the div, all you need to apply on the footer is overflow: hidden; and that should do the trick. Actually margin:0 auto; is not going to help with position:absolute;.

How do I stick a div to the top of the page?

The vertical position of the element to be stuck can also be modified with the help of the 'top' property. It can be given a value of '0px' to make the element leave no space from the top of the viewport, or increased further to leave space from the top of the viewport.


2 Answers

If you want the user to be able to scroll up and see the logo, then the logo must be within the top boundary of the body tag, because anything outside of that tag will not be viewable. This means you cannot use negative margins or offsetting like that. The only way to achieve this is to have the page scroll to the desired location that is within the top boundary of the body tag. You can set the time for this event to one millisecond, but there will still be a 'jump' in the page when it is loaded. So, the logic is: first make sure the page is long enough to scroll to the right place, then scroll there.

//Change the jQuery selectors accordingly

//The minimum height of the page must be 100% plus the height of the image
$('body').css('min-height',$(document).height() + $('img').height());

//Then scroll to that location with a one millisecond interval
$('html, body').animate({scrollTop: $('img').height() + 'px'}, 1);

View it here.

Alternatively, you can load the page without the image in the first place. Then your form field will be flush with the top of the document. Then you could create the element at the top and similarly scroll the page again. This is a round-a-bout way of doing the same thing though. And the page will still 'jump,' there is no way around that.

like image 99
smilebomb Avatar answered Oct 25 '22 03:10

smilebomb


Only CSS and anchor link solution

With a pseudo element :

--- DEMO ---

First :

set : html,body{height:100%;}

Second :

Choose one of your existing tags. This tag mustn't have a relatively positioned parent (except if it is the body tag). Preferably the first element in the markup displayed after the logo. For your example it would be the h1 tag. And give it this CSS :

h1:before {
    content:"";
    position:absolute;
    height:100%;
    width:1px;
}

This creates an element as heigh as the viewport area. As it is displayed under the logo, the vertical scroll lenght is the same as the logo height.

Third :

Give the first element after logo an id (for this example I gave id="anchor").

Then you can use a link like this your_page_link#anchor and you will automaticaly scroll to the anchor (logo outside/above the viewport).

This works whatever height the logo is.

link to editable fiddle

Full code :

HTML

<img src="http://blog.stackoverflow.com/wp-content/uploads/StackExchangeLogo1.png">
 <h1 id="anchor">Foo Bar</h1>

<p>ABC12345</p> <a href="#anchor">Anchor link</a>

CSS

html, body {
    height:100%;
    margin:0;
    padding:0;
}
h1:before {
    content:"";
    position:absolute;
    width:1px;
    left:0;
    height:100%;
}
like image 25
web-tiki Avatar answered Oct 25 '22 03:10

web-tiki