admin管理员组文章数量:1406926
I'm trying to make an effect similar as used on / , when the user scrolls down to the bottom of the page they reveal the "footer" more and more as the user keeps on scrolling.
I've tried to search both here and on google but haven't been able to find anything that's really useful. Most examples only shows/hide the footer once the user scrolls to the bottom.
So my question is, what's the effect called to reveal an element by scrolling? Are there any good tutorials / blog posts about this? All help I can get is much appreciated!
I'm trying to make an effect similar as used on http://www.t-mobile./ , when the user scrolls down to the bottom of the page they reveal the "footer" more and more as the user keeps on scrolling.
I've tried to search both here and on google but haven't been able to find anything that's really useful. Most examples only shows/hide the footer once the user scrolls to the bottom.
So my question is, what's the effect called to reveal an element by scrolling? Are there any good tutorials / blog posts about this? All help I can get is much appreciated!
Share asked Jul 10, 2014 at 8:18 NordisNordis 7432 gold badges9 silver badges31 bronze badges 4- That is nothing but a fixed element – Mr. Alien Commented Jul 10, 2014 at 8:21
- just looks like a fixed div with the main page content that has a higher z-index and a margin on the bottom the size of the footer. – Novocaine Commented Jul 10, 2014 at 8:22
- looks like a fixed div with with lower z-index ;). No javascript needed, just css. Inspecting can show you how it works: i.imgur./os1FluG.png . Note that to make it work you must set the footer div with the lowest z-index. Also, not important, but he is using bootstrap, lol – briosheje Commented Jul 10, 2014 at 8:22
- Thanks for all ments, that was exactly what I needed! :) – Nordis Commented Jul 10, 2014 at 8:27
1 Answer
Reset to default 8As I mented, you need to make your element fixed, so as explanation goes, I have two elements here, one is a normal position: relative;
element, so nothing fancy about that, I assigned relative
so that I can make the z-index
work
Second element is positioned fixed
and also, make sure you use margin-bottom
which should be equal to the height
of your footer, no need to assign any negative z-index
whatsoever to this element.
Demo
Not much HTML ...
<div></div>
<div>Reveal Me</div>
CSS
/* These are for your main site wrapper */
div:first-child {
height: 800px; /* Even auto is fine, I
used fixed height because I don't have any content here */
background: #eee;
margin-bottom: 200px; /* Equals footer wrappers height */
z-index: 1;
position: relative;
}
/* These are for footer wrapper */
div:last-child {
background: #aaa;
height: 200px;
position: fixed;
bottom: 0;
width: 100%;
}
For Dynamic Sizes
Note that am using a fixed height for the fixed
positioned element, if you have variable height
in footer element, than you need to use JS or jQuery to calculate the height using
$('#wrapperElement').css('margin-bottom', $('#footer').height());
Here, the selectors of #wrapperElement
and #footer
are my assumed ones, you can replace those with the your own selectors.
Something about fixed element - Horizontal Centering (I think it will be helpful to some users)
When you will make your element fixed, it will get out of the document flow, so if you are assigning fixed
to the wrapper of footer element and want to center some content in there, than nest another element inside that wrapper and use width
and margin: auto;
for that...
Demo 2
HTML
<div></div>
<div>
<div>Reveal Me</div>
</div>
CSS
body > div:first-child {
height: 800px;
background: #eee;
margin-bottom: 200px;
z-index: 1;
position: relative;
}
body > div:last-child {
background: #aaa;
height: 200px;
position: fixed;
bottom: 0;
width: 100%;
}
body > div:last-child div {
width: 80%;
margin: auto;
outline: 1px solid red; /* To show that element is horizontally centered */
}
Note: Selectors used in this answer are too general and are good for quick demonstration purposes, in real projects, make sure you use specific selectors
本文标签: javascriptHow to reveal element by scrollingStack Overflow
版权声明:本文标题:javascript - How to reveal element by scrolling? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745056706a2639978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论