Reputation: 9555
This works well in all browsers except IE. How do I fix this? when I scroll in IE it is very jerky.
//js
$(window).scroll(function () {
if ($(window).scrollTop() > 100) {
$('#scroller').css('top', $(window).scrollTop());
}
});
Upvotes: 4
Views: 1644
Reputation: 2244
Try this (fiddle):
$(window).scroll(function () {
if ($(window).scrollTop() > 100) {
$('#scroller').addClass("top");
}
else {
$('#scroller').removeClass("top");
}
});
And CSS:
#scroller {
position: relative;
top: 100px;
width: 500px;
background: #CCC;
height: 100px;
margin: 0 auto;
}
#scroller.top {
position: fixed;
top: 0;
left: 50%;
margin-left: -250px;
}
EDIT: I added the set width
and margin
to #scroller
, and set left: 50%
and margin-left: -250px;
(Half of the set width) to the .top class
Upvotes: 4
Reputation: 19591
You may try this too (fiddle)
$(window).scroll(function () {
if ($(window).scrollTop() > 100) {
//$('#scroller').css('top', $(window).scrollTop());
$('#scroller').css('top', '0px');
$('#scroller').css('position', 'fixed');
}
else
{
$('#scroller').css('top', '100px');
$('#scroller').css('position', 'absolute');
}
}
);
Upvotes: 1