Scroll the page when the footer dynamically grows
I have links within my footer, which when they are clicked, reveal hidden content.
So they're just hidden <ul>
elements which are shown using jQuery slideToggle();
The problem I'm having is when I do this, I then physically have to scroll the page. How can I force the window to scroll on its own?
I tried using scrollTo but that didn't seem to work.
Is it possibly the div that the footer resides in that is causing this?
<div id="navigation">
<ul>
<li>
<p onclick="showdivs('news');">
+ News</p>
<ul id="news" class="subitems" style="width: 94px;">
<li>Item #1</li>
<li>开发者_StackOverflow;Item #2</li>
</ul>
</li>
</ul>
</div>
Javascript - I know I can just do this with a .click event:
function showdivs(id) {
jQuery('.subitems').hide();
jQuery('#' + id).slideToggle('slow');
}
CSS:
#navigation
{
width: 100%;
height: 30px;
background: #f2f2f2;
}
#navigation ul
{
text-decoration: none;
display: block;
padding: 5px 8px;
background-color: #e5e5e5;
font-family: arial;
font-size: 10px;
font-weight: bold;
color: #808080;
width: auto;
cursor: pointer;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
background: #f2f2f2;
}
#navigation ul li
{
display: inline;
height: 30px;
float: left;
list-style: none;
margin-left: 15px;
position: relative;
background: #f2f2f2;
}
#navigation li p
{
background-color: #f2f2f2;
color: #009;
text-decoration: none;
}
#navigation li p:hover
{
text-decoration: underline;
}
#navigation li a
{
text-decoration: none;
background: #f2f2f2;
}
#navigation li a:hover
{
text-decoration: underline;
}
#navigation li ul
{
margin: 0px;
padding: 0px;
display: none;
position: absolute;
left: 0px;
top: 25px;
background-color: #f2f2f2;
}
#navigation li li
{
list-style: none;
display: list-item;
}
#navigation li li a
{
text-align: left;
font-size: 9px;
max-width: 120px;
background-color: #f2f2f2;
background-repeat: no-repeat;
background-position: 50% 100%;
background-image: url(../images/line.png);
}
#navigation li li a:hover
{
background-color: #f2f2f2;
color: #000;
-moz-border-radius: 5px;
}
You need to use jQuery's .scrollTop(...)
function with $(document)
, passing it the top of the element that's been toggled on:
jQuery('#' + id).slideToggle('slow', function () {
$(document).scrollTop($(this).offset().top);
});
Use the callback
parameter of .slideToggle()
to ensure that the page is scrolled down after the animation has completed (this may have been your problem originally).
Here's an example: http://jsfiddle.net/JU3xj/
If you want to animate the scrolling, you can do that too:
$("html, body").animate({
'scrollTop': $(this).offset().top
}, 500); // Adjust as needed.
Which you would use instead of the .scrollTop()
call:
function showdivs(id) {
jQuery('.subitems').hide();
jQuery('#' + id).slideToggle('slow', function () {
$("html, body").animate({
'scrollTop': $(this).offset().top
}, 500);
});
}
Example: http://jsfiddle.net/uktmR/
navigation.scrollTop = document.body.scrollHeight is what you need, right ?
Try this: http://jsfiddle.net/AlienWebguy/JU3xj/3/
First we create a plugin called scrollTo:
jQuery.fn.extend({
scrollTo : function(speed, easing) {
return this.each(function() {
var targetOffset = $(this).offset().top;
$('html,body').animate({scrollTop: targetOffset}, speed, easing);
});
}
});
Then, we just append it to whatever element we want to scroll to:
$('.return').click(function(){
$('#navigation').scrollTo(300,'linear');
});
精彩评论