JSFiddle - React, Tailwind, and code Playground
HTML
<button id="down">down</button>
<button id="down2">down2</button>
<div id="outer">
<div id="container">
<div id="inner">
<div id="animated">
</div>
</div>
</div>
<hr id="max" />
</div>
CSS
#outer {
position: relative;
width: 300px;
height: 300px;
background-color: #eee;
}
#container {
width: inherit;
height: inherit;
overflow-y: scroll;
overflow-x: hidden;
}
#inner {
width: 300px;
height: 2000px;
background-color: inherit;
}
#animated {
margin: 50px;
background-color: #33aa66;
width: 50px;
height: 50px;
}
#max {
position: absolute;
top: 200px;
background-color: #aa3333;
width: 100%;
height: 3px;
}
JavaScript
var new_margin;
$('#down').click(function() {
new_margin = 75 + parseInt($('#animated').css('margin-top'));
$("#animated").animate({
marginTop: new_margin
}, 1000);
});
$('#down2').click(function() {
var anim = $("#animated");
var hrOff = $("#max").offset();
var thOff = anim.offset();
new_margin = Math.min(hrOff.top - thOff.top - anim.height(), 75);
console.log(new_margin, hrOff.top, thOff.top);
var st = 0;
if (new_margin < 75) {
st = 75 - new_margin;
//have container scroll by this much?
}
anim.animate({
marginTop: "+=" + new_margin
}, 1000);
});