页脚动画和 jQuery?
我有一个页脚,我使用 粘性页脚 css 将其保留在页面底部。我希望我的页脚开始折叠,当用户单击按钮时,我希望它展开,然后将“展开”链接与页脚 div 内的不同容器
。div
交换
我如何使用 jQuery、HTML 和 CSS 来实现这一点?
编辑:
我希望页脚的高度约为 20 像素。在页脚中,应该有一个按钮(我猜是一个链接),上面写着“更多”或“关于”。当用户单击时,页脚应该变高并显示更精致的 div,并带有替代内容(而不是“更多”链接)。
I have a footer which I keep o the bottom of my page using sticky footer css. I'd like my footer to start collapsed and when the user clicks on a button, I want it to expand and then swap the "expand" link with a different container div
inside the footer div
.
How would I accomplish this with jQuery, HTML and CSS?
EDIT:
I want the footer to start with a height of about 20px. In the footer, there should be a button (a link, I guess) that says "more" or "about". When the user clicks, the footer should get taller and show a more elaborate div, with alternate content (not the "more" link).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个 jsFiddle 演示了滑入的基本方法。
以下是您将在那里找到的内容的摘要:
构建 css,使初始页脚高度为 0。确保还将依赖于页脚高度的其他 css 值归零(即
padding-bottom< /code> 在 #main 上,
margin-top
在#footer
上)。您还需要在#footer
上设置overflow:hidden
,以确保页脚内容在折叠时不可见。在链接的
click()
处理程序中,使用 jQuery 的animate()
函数来增加#footer
的高度(并同时进行其他必要的填充/边距调整)。animate()
函数采用四个参数(请参阅此处的文档 ),最后一个是动画完成时将触发的回调。您可以从此回调函数中触发链接交换。因此,假设您从 CSSStickyFooter 中的 HTML/CSS 开始,其余代码看起来会是这样像下面这样...
你的CSS(这在粘性页脚CSS之后):
并且你的Javascript将如下所示:
编辑:如果你想让页脚最初可见(在较小的尺寸),然后让它“滑出”到更大的尺寸,只需在我上面显示的CSS中设置你想要的任何高度(而不是0)。
您可以将任何您喜欢的内容放入页脚 div 中 - 因此,如果您想在内容较小时显示一组内容,在内容较大时显示不同的内容,那么只需将这些块放入页脚内的两个单独的 div 中即可。将它们设置为
position:absolute;top:0;
,这样它们就会在页脚内彼此重叠。最初将“展开视图”设置为display:none
,然后使用 jquery 的fadeIn()
和fadeOut()
函数,在点击中处理程序(或动画回调),以交换页脚内展开和折叠视图的可见性。这是 jsFiddle 示例,进行了相应调整
现在,如果你真的想变得更奇特,你可以制作页脚高度取决于两个不同内容视图的高度。 (这可能就是我会做的)。
这里有一个“更高级的”jsFiddle,可以根据内容计算高度
编辑:如果交换两个 animate 调用的顺序(因此
$('#main').animate(...)
位于$('#footer').animate(.. .)
),动画运行会更加流畅,并且滚动条在动画过程中不会闪烁。 (我本来应该这样展示的)。 这里是更新的 jsFiddle,显示了这一微小的变化。This jsFiddle demonstrates the basic approach to take for the slide-in.
Here's the summary of what you'll find there:
Build your css so that the initial footer height is 0. Make sure to also zero out the other css values that depend on footer height (namely
padding-bottom
on #main, andmargin-top
on#footer
). You'll also need to setoverflow:hidden
on#footer
, in order to ensure that the footer contents is invisible when it is collapsed.in the
click()
handler for your link, use jQuery'sanimate()
function to increase the height of#footer
(and to make the other necessary padding/margin adjustments simultaneously).the
animate()
function takes four parameters (see docs here), the last one is a callback that will fire when the animation completes. You can trigger your link swapping from within this callback function.So, assuming that you're starting with the HTML/CSS from CSSStickyFooter, the rest of your code would look something like the following...
Your CSS (this goes after the stickyfooter css):
And your Javascript would look like this:
Edit: if you want to have the footer visible initially (at some smaller size), then have it "slide out" to a larger size, just set whatever height you want (instead of 0) in the css I've shown above.
You can put whatever content you like into the footer div -- so if you want to show one set of content when it's small, and different content when it's big, then just put those blocks into two separate divs, within the footer. set them
position:absolute;top:0;
, so they'll be on-top-of each other within the footer. set the "expanded view" to bedisplay:none
initially, and then use jquery'sfadeIn()
andfadeOut()
functions, within the click handler (or the animate callback), to swap the visibility of the expanded and collapsed views within the footer.Here's the jsFiddle example, adjusted accordingly
Now, if you really want to get fancy, you can make the footer heights dependent on the height of the two different content views. (This is probably what I'd do).
here's a "fancier" jsFiddle that figures the heights from the content
Edit: If you swap the order of the two animate calls (so
$('#main').animate(...)
comes before$('#footer').animate(...)
), the animation will run more smoothly, and the scrollbar won't flash on/off during the animation. (I should have shown it this way originally). here's an updated jsFiddle, that shows this minor change.