Felipe Mosso
Felipe Mosso

Reputation: 3927

How can I get how much a div is horizontally scrolled jquery

I have a div with this configurations:

<div class="divImagem" style="position:relative; width: 1424px; height: 790px; overflow:hidden;">

Since the div is pretty large, it is not completely shown in the browser screen. So it creates a horizontal scroll bar for this unique div (not for the whole screen page).

How can I get how much of the div has scrolled to the left? I have tried this:

$("div.divImagem").scrollLeft();

But it does not work.. it always return me 0, even if the div is horizontally scrolled.

Upvotes: 1

Views: 1490

Answers (1)

Matt Harrison
Matt Harrison

Reputation: 13567

It's the window that you're scrolling, not the div.

Here's an interactive example: http://jsfiddle.net/pjWt7/1/

HTML

<p>Scrolled: <span>0</span>px</p>
<div class="divImagem" style="position:relative; width: 1424px; height: 790px; overflow:hidden;">

Javascript/jQuery

$(window).scroll(function () {
    console.log("scrolling");
    var scrollLeft = $(this).scrollLeft();
    $("span").html(scrollLeft);

})

Upvotes: 1

Related Questions