Bạn đang xem: How to synchronize two scrollbars for divs
Bạn đã xem: Scroll khổng lồ bottom of a div in javascript
Here is the correct version:
$("#div1").scrollTop($("#div1").scrollHeight);or jQuery 1.6+ version:
var d = $("#div1");d.scrollTop(d.prop("scrollHeight"));Or animated:
$("#div1").animate( scrollTop: $("#div1").prop("scrollHeight"), 1000);
scrollTop = scrollHeightWhereas the correct approach is to set:
scrollTop = scrollHeight - clientHeightIn other words:
$("#div1").scrollTop($("#div1").scrollHeight - $("#div1").clientHeight);Or animated:
$("#div1").animate( scrollTop: $("#div1").scrollHeight - $("#div1").clientHeight, 1000);
$("#div1").animate( scrollTop: $("#div1").height(), 1000);if you want it to be animated (over 1000 milliseconds).
$("#div1").scrollTop($("#div1").height())if you want it instantaneous.
This worked a treat, basic Javascript.
window.onload=function () var objDiv = document.getElementById("MyDivElement"); objDiv.scrollTop = objDiv.scrollHeight; The following will work. Please note & scrollHeight
$("#myDiv").animate( scrollTop: $("#myDiv").scrollHeight , 1000); Scroll window khổng lồ the bottom of target div.
function scrollToBottom(id) div_height = $("#"+id).height(); div_offset = $("#"+id).offset().top; window_height = $(window).height(); $("html,body").animate( scrollTop: div_offset-window_height+div_height ,"slow");scrollToBottom("call_div_id"); for animate in jquery (version > 2.0)
In my specific situation (scrollable div wrapped in a bootstrap modal), a v-if showed new content, which I wanted the page lớn scroll down to. In order to lớn get this behaviour to work, I had to wait for vue to lớn finish re-rendering, and then use jQuery to lớn scroll lớn the bottom of the modal.
Xem thêm: Hàng Lcl Là Gì Và Trách Nhiệm Của Các Bên Khi Gửi Lcl Shipment?
So...
this.$nextTick(function() $("#thing").scrollTop = $("#thing").scrollHeight;) You can use below code to lớn scroll to lớn bottom of div on page load.
$(document).ready(function() $("div").scrollTop($("div").scrollHeight);); You can kiểm tra scrollHeight & clientHeight with scrollTop to lớn scroll to lớn bottom of div like code below.
$("#div").scroll(function (event) if ((parseInt($("#div").scrollHeight) - parseInt(this.clientHeight)) == parseInt($("#div").scrollTop())) console.log("this is scroll bottom of div"); );$(document).ready(function() let width = $(window).width(); let element = $("#YourId"); let positionFromTop = element.offset().top + element.prop("scrollHeight"); $("html, body").animate( scrollTop: Math.abs($(window).height() - positionFromTop) , 500);); When page is load then scroll is max value .
This is message box when user send message then always show latest chat in down so that scroll value is always is maxium.
$("#message").scrollTop($("#message").scrollHeight);see image
Thanks for contributing an answer khổng lồ Stack Overflow!
Please be sure lớn answer the question. Provide details và share your research!But avoid …
Asking for help, clarification, or responding khổng lồ other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service, privacy policy & cookie policy
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.