admin管理员组文章数量:1279244
I wrote this sticky div effect in Jquery but was wondering a way to do the same thing with vanilla javascript
.stick {
position: fixed;
top: 0;
}
$(document).ready(function () {
var top = $('#a8').offset().top;
$(window).scroll(function (event) {
var y = $(this).scrollTop();
if (y >= top)
$('#a8').addClass('stick');
else
$('#a8').removeClass('stick');
});
});
I wrote this sticky div effect in Jquery but was wondering a way to do the same thing with vanilla javascript
.stick {
position: fixed;
top: 0;
}
$(document).ready(function () {
var top = $('#a8').offset().top;
$(window).scroll(function (event) {
var y = $(this).scrollTop();
if (y >= top)
$('#a8').addClass('stick');
else
$('#a8').removeClass('stick');
});
});
Share
Improve this question
asked Aug 28, 2014 at 16:00
Alex BorsodyAlex Borsody
2,0609 gold badges45 silver badges78 bronze badges
3 Answers
Reset to default 5Sure you can do the same in pure JS. Here is simple example:
var top = document.getElementById('a8').offsetTop;
window.onscroll = function() {
var y = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
if (y >= top) {
a8.className = 'stick';
}
else {
a8.className = '';
}
};
Demo: http://jsfiddle/hd3uyf68/1/
Note, that in this simple example I don't actually implement addClass/removeClass
functionality. If you need it it's quite easy to add.
In vanilla JavaScript:
function ready() {
var box = document.getElementById('box'),
top = box.offsetTop;
function scroll(event) {
var y = document['documentElement' || 'body'].scrollTop;
if (y >= top) box.classList.add('stick');
else box.classList.remove('stick');
}
window.addEventListener('scroll', scroll);
}
if (document.readyState == 'plete' || document.readyState == 'loaded') {
ready();
} else {
window.addEventListener('DOMContentLoaded', ready);
}
JSFiddle example:
http://jsfiddle/869fqgds/4/
Here is an adapted version from dfsq's answer, which is the most elegant and simple solution I've found so far:
var myTop = document.getElementById('freeze').offsetTop;
var origClass = freeze.className;
window.onscroll = function() {
var y = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
if (y >= myTop) {
freeze.className = origClass +' stick';
}
else {
freeze.className = origClass;
}
};
// Note that I barely have javascript knowledge, so the modification, even if it works, might be far from optimal. I'm open to improvements.
Main differences are:
- variable name changed to avoid ambiguity (see ments of that answer).
- additional variable and modification inside
if/else
in order to allow using it with tags with an existing class already assigned.
The only issue remaining is that at the moment when the div is fixed, there is a kind of 'jump' in the flow. This is easily seen in the original jsfiddle if adding any text (for instance <p>See how I jump!</p>
) a few lines below the div that is sticked.
本文标签: jquerySticky div on scroll with plain javascriptStack Overflow
版权声明:本文标题:jquery - Sticky div on scroll with plain javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741240066a2363798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论