user2563256
user2563256

Reputation: 187

window.scrollTo(0, document.body.scrollHeight) Not working on fixed height?

I want to go end of the page when I click on a button .I have data on this div (coming from server). But when I give the div height, it doesn't work. When I remove the height from the div, it works.

<div id="realTimeContents" class ="left realtimeContend_h" style="width:97%; height:850px">
</div>

I'll elaborate: When I get data from server, I append the data with auto scrolling (that's why I need the height of the div). But when I give it height, it stops going to the end of the page

function nativePluginResultHandler (result)
{
    $('#realTimeContents' ).append(result);

    var elem = document.getElementById('realTimeContents');// just to scroll down the line 
    elem.scrollTop = elem.scrollHeight;
}

Here is fiddle . http://jsfiddle.net/ravi1989/s66Ww/

Upvotes: 0

Views: 9726

Answers (4)

douggard
douggard

Reputation: 712

I had an issue where it wasn't going all the way to the end of the page. I had some text that was not breaking and causing my content container to be wider than body. This made scrolltop max out at a height that was less than the scrolltop value.

Might not be the case for everyone overflow-wrap: break-word; fixed it.

Upvotes: 0

Aditya Kresna Permana
Aditya Kresna Permana

Reputation: 12099

Try this method

First check if document.body.scrollHeight got zero value, if yes then try to get document.documentElement.scrollHeight value

var getheight = function() {
  var sh = document.body.scrollHeight;
  return (sh && sh > 0) ? sh : document.documentElement.scrollHeight;
}

var height = getHeight()

Hope it helps

Upvotes: 0

Ankit Gupta
Ankit Gupta

Reputation: 786

For Scroll down in Selenium use below code:

Sometimes "document.body.scrollHeight" does not work on JavaScrip specifically when the website is written on React then use: document.documentElement.scrollHeight

Here is the code for scroll Down and Scroll Up

JavascriptExecutor jse = (JavascriptExecutor) driver;
// (driver is your browser webdriver object) jse.executeScript("window.scrollBy(0,document.body.scrollHeight || document.documentElement.scrollHeight)", "");

For scroll up use below code:

jse.executeScript("window.scrollBy(0,-document.body.scrollHeight || -document.documentElement.scrollHeight)", "");

Upvotes: 0

maverickosama92
maverickosama92

Reputation: 2725

try following:

var $div = $("#realTimeContents");

$("#btnScroll").on("click",function(){    
    $div.scrollTop($div[0].scrollHeight);
});

working fiddle here: http://jsfiddle.net/s66Ww/2/

Hope it helps.

Upvotes: 2

Related Questions