Angularize Angularize - 7 months ago 7
HTML Question

Scroll to the top relative to clicked element

I have a list of

div
and I'd like that, when I click on one of them, the page scrolls until the clicked element is on the top of the view.
My code: http://jsfiddle.net/rcxnogmx/

Js code:

$(document).ready(function (){
$("#click").click(function (){
$('html, body').scrollTop( $(this).offset().top );
});
});


I've tried scrollTop -> this, but it doesn't work

Answer

Try this..

Yes, id should be unique for each element.

Rather than by ID, you can do it by class. You can give class name to DIV "scrolling".

element that has "scrolling" class click event scrolltop to its' top offset

$(document).ready(function (){
    $(".scrolling").click(function (){
        $('html, body').scrollTop( $(this).offset().top );
    });
});

Demo