I have a jquery function to move some div up and down, and the page scroll here is the code →
$(window).scroll(function() {
$(".mydiv").css({
"margin-top": ($(window).scrollTop()) + "px",
"margin-left": ($(window).scrollLeft()) + "px"
});
});
This code above only works on one div, for example →
<div class="mydiv">This div works</div>
<div class="mydiv">This div takes a high distance from above div and goes down</div>
$(window).scroll(function() {
$(".mydiv").css({
"margin-top": ($(window).scrollTop()) + "px",
"margin-left": ($(window).scrollLeft()) + "px"
});
});
body {
height: 1000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mydiv">This div works</div>
<div class="mydiv">This div takes a high distance from above div and goes down</div>
Run codeHide result
source
share