With the answers I got from Mathias and followed by Tobias Mesquita , before its new edition.
I came up with two similar alternatives, I believe it's Cross-Browser.
1st_Alternative
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style type="text/css">
body {
height: 999px;
}
#Pai {
position: relative;
width: 200px;
height: 200px;
border: 3px solid blue;
text-align: center;
overflow: auto;
}
#Filho {
width: 100px;
height: 100px;
margin-left: -50px;
/* metade da largura */
margin-top: -50px;
/* metade da altura */
position: absolute;
top: 50% ;
left: 50% ;
background: tomato;
z-index: 1;
opacity: .80;
}
</style>
</head>
<body>
<center>
<div id='Pai' onscroll = "rolamento()">
<p>Elemento A</p>
<p>Elemento B</p>
<p>Elemento C</p>
<p>Elemento D</p>
<p>Elemento E</p>
<p>Elemento F</p>
<p>Elemento G</p>
<p>Elemento H</p>
<p>Elemento I</p>
<p>Elemento J</p>
<div id='Filho'></div>
</div>
</center>
<script type="text/javascript">
var A = document.getElementById('Pai');
var B = document.getElementById('Filho');
var rolamento = function() {
var centralizado = A.scrollTop + 100;
B.style.top = centralizado;
}
A.onscroll = rolamento;
</script>
</body>
</html>
Follow Explanation
var A = document.getElementById('Pai'); // Div(primária)
var B = document.getElementById('Filho'); // Div(secundária)
var rolamento = function() {
var centralizado = A.scrollTop + 100; // Aqui definimos o valor 100 para reposicionar a Div(secundária) conforme o valor scrollTop() da Div(primária)
B.style.top = centralizado; // Agora vamos automatizar a Div(secundária), para retomar sempre a mesma posição inicial
}
A.onscroll = rolamento; // Escuta o evento da barra de rolagem lateral e dispara a função
NOTE - "This 1st_Alternative worked perfectly on the local computer, but it did not have the same mismatch when I tried to add as a sample in link and link . However the Script itself works, being below </body>
. Anything that comes to be the cause, is of a nature unknown to me. "
2nd_Alternative
var A = document.getElementById('Pai'); // Div(primária)
var B = document.getElementById('Filho'); // Div(secundária)
var P = B.scrollHeight / 2;
A.onscroll = new Function("if(B.style.top = A.scrollTop + P + 'px') return true");
body {
height: 999px;
}
#Pai {
position: relative;
width: 200px;
height: 200px;
border: 3px solid blue;
text-align: center;
overflow: auto;
}
#Filho {
position: absolute;
width: 100px;
height: 100px;
top: 25%;
left: 25%;
margin: 0 auto;
background: tomato;
}
<center>
<div id='Pai'>
<p>Elemento A</p>
<p>Elemento B</p>
<p>Elemento C</p>
<p>Elemento D</p>
<p>Elemento E</p>
<p>Elemento F</p>
<p>Elemento G</p>
<p>Elemento H</p>
<p>Elemento I</p>
<p>Elemento J</p>
<div id='Filho'></div>
</div>
</center>
JSbin - External Sample
Explaining
var P = B.scrollHeight / 2;
I am dividing the div
(secondary) by its Actual Size, that is:
Size: 100 ÷ 2 = 50
We saved this value (50) in the P variable and launched it in the if
condition.
A.onscroll = new Function("
if(B.style.top = A.scrollTop + P) return true
");
Then the scroll
that is attached to the element div
(primary), is slid
Each "1px" will be called new Function()
for execution
That followed by the condition if
, it takes action by its position
And in the blink of an eye, returns true
(repositioning done).
Ready!