There is a block, div
, it contains a number of elements of different sizes. When adding one more, the scrolling should be in the lowest position.
The first solution was to calculate the height of the newly added element, store the sum of all the previous ones and read the "correct" value for the HTMLElement.scrollTop
. Warning tips to wrap all the elements in a div
and take its height, do not consider it out of sports interest.
It seemed to me tedious, I remembered that when assigning a value greater than the maximum in the scrollTop
, it becomes the maximum itself, and, without hesitation, I wrote the value Number.MAX_SAFE_INTEGER
. But the scrolling became the highest position. In this regard, the question is, what is the maximum value that can be written to this property and why is it? This behavior is repeated in Mozille and EJE, everything is normal in Chrome.
It turns out that the scrolling of an element cannot be equal to 9007199254740991
, but, for example, Number.MAX_SAFE_INTEGER - 1000000000000 = 9006199254740991
works perfectly in Mozille, but it turns out, not at all, but in EJE it is observed experimentally that even less!
addMessage('ΠΡΠΎΠ½Ρ', 'ΠΠΎΠΉΠΌΠ°ΡΡ ΠΏΠΎΠΊΠ΅ΠΌΠΎΠ½ΠΎΠ² Π² ΠΏΠΎΠΏΡΠ»ΡΡΠ½ΠΎΠΉ ΠΈΠ³ΡΠ΅ Pokemon Go ΠΌΠΎΠΆΠ½ΠΎ ΠΈ Π² Π·Π΄Π°Π½ΠΈΠΈ Π¦Π΅Π½ΡΡΠΎΠ±Π°Π½ΠΊΠ°, ΡΠ°ΡΡΠΊΠ°Π·Π°Π» Π΄ΠΈΡΠ΅ΠΊΡΠΎΡ Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ Β«ΠΠΎΠ»ΠΎΠ΄ΡΠ΅ ΠΏΡΠΎΡΠ΅ΡΡΠΈΠΎΠ½Π°Π»ΡΒ» ΠΠΌΠΈΡΡΠΈΠΉ ΠΠ΅ΡΠΊΠΎΠ² Π² Ρ
ΠΎΠ΄Π΅ ΡΠΎΡΡΠΌΠ° ΠΠ³Π΅Π½ΡΡΡΠ²Π° ΡΡΡΠ°ΡΠ΅Π³ΠΈΡΠ΅ΡΠΊΠΈΡ
ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΈΠ². Β«ΠΠ° ΡΠΎΠ²Π΅ΡΠ°Π½ΠΈΠΈ Π² Π¦Π Ρ ΠΏΠΎΠΉΠΌΠ°Π» ΠΏΠΎΠΊΠ΅ΠΌΠΎΠ½Π° ΠΏΡΡΠΌΠΎ Π½Π° ΠΎΠ΄Π΅ΠΆΠ΄Π΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ· ΡΡΠΊΠΎΠ²ΠΎΠ΄ΠΈΡΠ΅Π»Π΅ΠΉ Π¦Π΅Π½ΡΡΠΎΠ±Π°Π½ΠΊΠ°. ΠΠ½Π΅ ΡΠΊΠ°Π·Π°Π»ΠΈ, ΡΡΠΎ Ρ Π·Π°Π½ΠΈΠΌΠ°ΡΡΡ Π΅ΡΡΠ½Π΄ΠΎΠΉ. ΠΠ° ΡΡΠΎ Ρ ΠΎΡΠ²Π΅ΡΠΈΠ»: ΠΊΠΎΠ»Π»Π΅Π³ΠΈ, Nintendo Π·Π°ΡΠ°Π±ΠΎΡΠ°Π»Π° Π·Π° Π½Π΅Π΄Π΅Π»Ρ Π΄Π΅Π½Π΅Π³ Π±ΠΎΠ»ΡΡΠ΅, ΡΠ΅ΠΌ Π²Ρ ΠΏΠ΅ΡΠ°ΡΠ°Π΅ΡΠ΅ Π·Π° Π³ΠΎΠ΄Β», β ΡΠΊΠ°Π·Π°Π» ΠΠ΅ΡΠΊΠΎΠ².'); function addMessage(nickname, msg) { var container = document.querySelector('.ui7--main_messages'); var message = document.createElement('div'); message.classList.add('ui7--main_message'); var name = document.createElement('span'); name.classList.add('ui7--main_message_name'); name.innerHTML = nickname; var text = document.createElement('span'); text.classList.add('ui7--main_message_text'); text.innerHTML = msg; message.appendChild(name); message.appendChild(text); container.appendChild(message); container.scrollTop = Number.MAX_SAFE_INTEGER; }
.ui7--main_messages { height: 250px; width: 300px; overflow: auto; font-size: 0.9em; } .ui7--main_message { width: 100%; margin: 15px 0; text-align: justify; } .ui7--main_message:first-child { margin-top: 0; } .ui7--main_message_name { font-weight: 700; } .ui7--main_message_name:after { content: ": " }
<div class="ui7--main_messages"> <div class="ui7--main_message"> <span class="ui7--main_message_name" style="color:red">Yurik</span> <span class="ui7--main_message_text">azazaza azazuli?</span> </div> <div class="ui7--main_message"> <span class="ui7--main_message_name" style="color:green">Vanik</span> <span class="ui7--main_message_text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Provident deserunt porro rerum eveniet eaque aperiam ab doloribus! Ratione repudiandae, molestiae numquam libero, dignissimos, asperiores pariatur modi ad sunt harum at?</span> </div> </div>